Question

I have been trying to change the locale of my JSF application dynamically using a select box. But, the locale is getting changed only if I change the settings in the faces-config.xml. Help me with this..enter code here

<application>
           <locale-config>
                <default-locale>en</default-locale>
           </locale-config>
       <resource-bundle>
        <base-name>com.messages.messages_en</base-name>
        <var>msg</var>
       </resource-bundle>
     </application>

My value Change listener is,

public void countryLocaleCodeChanged(ValueChangeEvent e){
        String newLocaleValue = e.getNewValue().toString();

        //loop country map to compare the locale code
                for (Map.Entry<String, Object> entry : countries.entrySet()) {

               if(entry.getValue().toString().equals(newLocaleValue)){

                FacesContext.getCurrentInstance()
                    .getViewRoot().setLocale((Locale)entry.getValue());

              }
               }
Was it helpful?

Solution

I have found the mistake. The problem is with the settings in faces-config.xml file. The correct setting are as follows..

<application>
           <locale-config>
            <supported-locale>en</supported-locale>                 
           <supported-locale>fr</supported-locale>
           </locale-config>
       <resource-bundle>
        <base-name>com.messages.messages</base-name>

        <var>msg</var>

       </resource-bundle>
     </application>

In the above code the base-name is the location of the properties file. In my example the package name is com.messages and the properties file is messages. Here I have taken two locales of english and french. The default locale I selected is English(en). According to the locale selected by the user it picks up either the messages_en.properties or messages_fr.properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top