blo0p3r
blo0p3r

Reputation: 6850

Changing local does not affect f:convertDateTime

So I have a site that I have internationalized. On my header I have 2 h:commandLinks where only one shows depending on the current locale.

<h:commandLink action="#{language.languageToFrench}" value="Français" rendered="#{language.language ne 'fr'}"/>
<h:commandLink action="#{language.languageToEnglish}" value="English" rendered="#{language.language eq 'fr'}"/>

This is connected to a backing bean that looks like this :

private static final long serialVersionUID = 1L;
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

public Locale getLocale() {
    return locale;
}

public String getLanguage() {
    return locale.getLanguage();
}

//value change event listener
public void languageToFrench(){
    locale = Locale.CANADA_FRENCH;
    FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.CANADA_FRENCH);
}

public void languageToEnglish(){
    locale = Locale.ENGLISH;
    FacesContext.getCurrentInstance().getViewRoot().setLocale(Locale.ENGLISH);
}

These buttons work fine. EXCEPT when I have a date that is converted before displayed, such as :

<h:outputText value="#{events.date}">
    <f:convertDateTime locale="#{language.language}" type="date" dateStyle="medium" timeZone="EDT" />
</h:outputText>

EDIT : This h:outputText is nested in a h:dataTable. Thought I'd have to mention this.

When I change language, all labels are switched no problem. What isn't changed are the dates. If I then refresh the page, the dates change to the proper locale. It is as if the conversion is laggin when changing locales. I'm assuming this is to do with the life cycle or something of the sort, but can't seem to figure this out. I am under the impression that the locale is changed before the page reloaded... obviously not 100% correct.

Using JSF 2.0

Upvotes: 0

Views: 1067

Answers (1)

Luca
Luca

Reputation: 4273

If you call FacesContext.getCurrentInstance().getViewRoot().setLocale you doesn't need

<f:convertDateTime locale="#{language.language}" type="date" dateStyle="medium" timeZone="EDT" />

try with

<f:convertDateTime type="date" dateStyle="medium" timeZone="EDT" />

Upvotes: 1

Related Questions