Search code examples
javajsf-2internationalizationlocale

Internationalization JSF 2.0


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?


Solution

  • 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}">
    

    See also: