Reputation: 23413
I have an application in witch I am trying to set a internationalization availability.
This is my faces-config.xml:
<application>
<locale-config>
<default-locale>lt</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>de</supported-locale>
</locale-config>
<resource-bundle>
<base-name>application</base-name>
<var>msg</var>
</resource-bundle>
</application>
I have three property files:
application_lt.properties
application_en.properties
application_de.properties
The bean class:
@ManagedBean(name = Beans.LOCALE_BEAN)
@SessionScoped
public class LocaleBean extends BaseBean implements Serializable {
private String lang;
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}
The action class:
@ManagedBean(name = "localeAction")
@SessionScoped
public class LocaleAction extends BaseAction implements Serializable {
public void changeLocale() {
LocaleBean localeBean = getBean(Beans.LOCALE_BEAN);
String language = localeBean.getLang();
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(language));
}
}
To change locale I am using commandLink:
<h:commandLink action="#{localeAction.changeLocale}">
<f:setPropertyActionListener target="#{localeBean.lang}" value="en"/>
English
</h:commandLink>
<h:commandLink action="#{localeAction.changeLocale}">
<f:setPropertyActionListener target="#{localeBean.lang}" value="lt"/>
Lithuanian
</h:commandLink>
First problem:
I have defined that my default locale is "lt": lt. Why when I start up my application text values is loaded from application_en.properties and not from application_lt.properties?
Second problem:
When I execute commandLink action, the locale changes depending on which locale I have selected. But executing the action was one click, the second click on any other link of application is OK as well and when I click on any link of application for a third time, the text values are locaded from application_en.properties. It seems that locale changes somehow...
Any ideas?
Upvotes: 2
Views: 3486
Reputation: 22692
First problem When it comes to locale, JSF is reading client browser HTTP Accept-Language header and uses the most preferred locale. If it is not supported by application (faces-config settings), then it uses the second most preferred locale from HTTP header, etc. If none of the client preferred locales are supported by application, then the default locale from faces-config.xml is used. As a result, it may happen that default locale from faces-config.xml is simply ignored.
So it seems that your client browser may send English locale in HTTP Accept-Language header.
Second problem. This happens because by default locale is set per request, not per user session. Have a look at this question to see how to overcome this.
Upvotes: 1
Reputation: 1109735
I have defined that my default locale is "lt": lt. Why when I start up my application text values is loaded from application_en.properties and not from application_lt.properties?
Apparently because your browser identifies itself with locale en
as preferred locale by the Accept-Language
request header. JSF will then automatically use it because it's among the supported languages. You'd need to change the preferred language in the browser's settings.
When I execute commandLink action, the locale changes depending on which locale I have selected. But executing the action was one click, the second click on any other link of application is OK as well and when I click on any link of application for a third time, the text values are locaded from application_en.properties. It seems that locale changes somehow.
Apparently you changed the view. Your action method only changes the locale of the current view. You need to make sure that you set the locale form the LocaleBean
in the <f:view>
of the master template or at least all views.
<f:view locale="#{localeBean.lang}">
Upvotes: 4