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.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.