I'm trying to implement internationalization in JSF 2. I've tried many solutions, but I'm not able to make it to change the view locale. Here is my code for faces-config.xml
file:
<application>
<locale-config>
<default-locale>es</default-locale>
<supported-locale>en_GB</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>malagaAcoge.configuracion.Messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
I'm using 3 properties that are located into my source package.
This is my view:
<f:view locale="#{idiomaBean.locale}">
<div id="contenedor">
<div id="cabecera"
style="background: url('../images/logo/fondoCabecera.png') no-repeat">
</div>
<div id="cuerpo">
<div id="center">
<h:form prependId="false" id="form_login">
<h:selectOneMenu value="#{idiomaBean.locale}"
onchange="submit()"
valueChangeListener="#{idiomaBean.changeLanguage}">
<f:selectItems value="#{idiomaBean.countriesInMap}" />
</h:selectOneMenu>
<h:panelGrid columns="3">
<p:commandLink title="SP" action="#{idiomaBean.changeLanguage}">
<p:graphicImage value="../images/paises/sp.png"></p:graphicImage>
</p:commandLink>
<p:commandLink title="EN" action="#{idiomaBean.changeLanguage}">
<p:graphicImage value="../images/paises/en.png">
</p:graphicImage>
</p:commandLink>
<p:commandLink title="FR" action="#{idiomaBean.changeLanguage}">
<p:graphicImage value="../images/paises/fr.png">
</p:graphicImage>
<f:setPropertyActionListener target="#{idiomaBean.locale}"
value="fr"></f:setPropertyActionListener>
</p:commandLink>
</h:panelGrid>
This is the IdiomaBean
I'm using to perform the change:
private static final long serialVersionUID = 1L;
private static String locale = FacesContext.getCurrentInstance()
.getViewRoot().getLocale().getLanguage();
private static Map<String, Object> countries;
static {
countries = new LinkedHashMap<String, Object>();
countries.put("Español", new Locale("es")); // label, value
countries.put("English", Locale.ENGLISH);
countries.put("Français", Locale.FRENCH);
}
public Map<String, Object> getCountriesInMap() {
return countries;
}
public String getLocale() {
return locale;
}
@SuppressWarnings("static-access")
public void setLocale(String locale) {
this.locale = locale;
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(locale));
}
public void changeLanguage(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());
}
}
I'm using PrimeFaces 3.0 on Tomcat server.