0

I just tried to implement JSF Internationalization based on this article - "Internationalization in JSF with UTF-8 encoded properties files" and found something weird. Is it right way to change locale by using code in this bean?

@ManagedBean
@SessionScoped
public class LocaleBean {

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    public Locale getLocale() {
        return locale;
    }

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

    public void setLanguage(String language) {
        this.locale = new Locale(language);
    }

}

As I understand Java private Locale locale must be pointer to actual Locale object from viewRoot object but this method didn't work at me. Instead, when I changed setLanguage(String language) method to this

public void setLanguage(String language) {
        FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language));
    }

it began to work. Now I wonder where is mistake? What's wrong with @BulusC code? Maybe I did something wrong, maybe I forget something? When I debugged I seen that private Locale locale and locale object from viewRoot are different objects.

Anatoly
  • 5,056
  • 9
  • 62
  • 136

1 Answers1

1

Indeed, the code was missing the line you've posted. From this question: Localization in JSF, how to remember selected locale per session instead of per request/view, answered by BalusC, you can check the code for setLanguage (code taken from BalusC's answer, not mine):

public void setLanguage(String language) {
    locale = new Locale(language);
    //this is the line you added
    FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332