0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
amal
  • 3
  • 1
  • 2

1 Answers1

4

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 Locale argument to String type 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

<f:view locale="#{idiomaBean.locale}">
    <h:form>
        <h:selectOneMenu value="#{idiomaBean.language}" onchange="submit()">
            <f:selectItems value="#{idiomaBean.availableLanguages}" />
        </h:selectOneMenu>

        <h:panelGrid columns="3">
            <p:commandLink title="SP" action="#{idiomaBean.setLanguage('sp')}" update="@all">
                <p:graphicImage value="../images/paises/sp.png" />
            </p:commandLink>
            <p:commandLink title="EN" action="#{idiomaBean.setLanguage('en')}" update="@all">
                <p:graphicImage value="../images/paises/en.png" />
            </p:commandLink>
            <p:commandLink title="FR" action="#{idiomaBean.setLanguage('fr')}" update="@all">
                <p:graphicImage value="../images/paises/fr.png" />
            </p:commandLink>
        </h:panelGrid>
    </h:form>
</f:view>

with

@ManagedBean
@SessionScoped
public class IdiomaBean implements Serializable {

    private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

    private static Map<String, String> availableLanguages;

    static {
        availableLanguages = new LinkedHashMap<String, String>();
        availableLanguages.put("Español", "es");
        availableLanguages.put("English", "en");
        availableLanguages.put("Français", "fr");
    }

    public Map<String, String> getAvailableLanguages() {
        return availableLanguages;
    }

    public Locale getLocale() {
        return locale;
    }

    public String getLanguage() {
        return locale.getLanguage();
    }

    public void setLanguage(String language) {
        locale = new Locale(language);
        FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
    }

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for your quick annswer. I did exactly what you told me, but still not working. When I debug, I can see that the valur of locale have changed, but no language change is made. I've tried many solutions but nothing seems to work. – amal Dec 05 '11 at 13:05
  • What exactly does not work? The dropdown list or the links, or both? The `@ManagedBean` and `@SessionScoped` annotations are both from `javax.faces.bean` package, right? – BalusC Dec 05 '11 at 13:08
  • Nothing works. Well, I'm not using annotations for backing beans, I'm declaring them on faces-config file. I've tried also with annotations. Still not working. The backing bean is correctly called, and variable set, but no language change. I'm really confused, cause I've tried many tutoriales. The Messages files have to be in a specified package or something? – amal Dec 05 '11 at 13:22
  • No, they just have to be in the classpath. Okay, the old faces-config way should work fine. The action is invoked, that's fine. Only the view doesn't pickup the locale change, right? Do you have multiple `` in your template? Are you using template clients (as in `` etc)? The `` has to go in the master template, not in the template client. There should be **one** `` throughout the entire tree. – BalusC Dec 05 '11 at 13:28
  • Yes, that's exaclty what's happend. I'm not using template for this page. It's a simple page. The f:view tag is placed just after the h:body tag. – amal Dec 05 '11 at 13:33
  • it works. Dont' know why, changing the map to map, and specifying the country, it works. Thank you for your help. – amal Dec 05 '11 at 15:12