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.
You’re not clear in your problem description. That dropdown list look like as it will work (although the value change listener is entirely superfluous), but those links indeed won’t work at all. I’m assuming that you’re actually asking how to implement the links properly.
First, get rid of the value change listener method in both the dropdown list and the image links. You are not interested in the old value, you are only interested in the new value, so you don’t need it at all. As you’re using PrimeFaces 3.0 which in turn requires Servlet 3.0 which in turn comes along with EL 2.2, you should be able to just pass method arguments in EL. You only need to change
Localeargument toStringtype so that it can also be used to pass as link method argument.Also, you should absolutely not make the user-selected locale
static. It would going to be shared among all users in the same application. So if one user changes the locale, then every other visitor would see the last user-selected locale! Make it a non-static property of a session scoped bean instead.So, this should do
with