在JSF中更改页面的语言

时间:2015-05-20 16:08:47

标签: jsf internationalization

我有问题。我按照tutorial中的说明进行操作。但我有一个问题。当我从selectOneMenu改变语言时,没有任何事情发生。但默认语言负载很好。这是我的xhtml文件:

   <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
            <title>Letecký systém</title>
            <h:outputStylesheet library="css" name="login.css"  />
        </h:head>
        <h:body>
            <div id="nadpis">Letecký systém</div>
            <h:panelGrid columns="2">

                Language : 
                <h:selectOneMenu value="#{mc.localeCode}" onchange="submit()"
                                 valueChangeListener="#{mc.countryLocaleCodeChanged}">
                    <f:selectItems value="#{mc.countriesInMap}" /> 
                </h:selectOneMenu>
            </h:panelGrid> 
            <div id="mainForm">
            .
            .
            .
            .
            </div>
        </h:body> </html>

这是我的控制者:

@ManagedBean(name = "mc")
@SessionScoped
public class MainController {

    private static final long serialVersionUID = 1L;

                .
                .
                .
                .

    //Translate
    private String localeCode;

    private static Map<String, Object> countries;

    static {
        countries = new LinkedHashMap<String, Object>();
        countries.put("English", Locale.ENGLISH); //label, value
        countries.put("French", Locale.FRENCH);
    }

    public Map<String, Object> getCountriesInMap() {
        return countries;
    }

    public String getLocaleCode() {
        return localeCode;
    }

    public void setLocaleCode(String localeCode) {
        this.localeCode = localeCode;
    }

    public void countryLocaleCodeChanged(ValueChangeEvent e) {

        String newLocaleValue = e.getNewValue().toString();

        for (Map.Entry<String, Object> entry : countries.entrySet()) {

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

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

            }
        }

    }

最后这是我的faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <locale-config>
            <default-locale>en</default-locale>
            <supported-locale>fr</supported-locale>
        </locale-config>
        <resource-bundle>
            <base-name>resources.messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
</faces-config>

我的程序有结构

  • 网页 - &gt;的index.xhtml
  • 源包
    • 控制器 - &gt; MainController.java
    • 资源 - &gt; messages.properties           - &GT; messages_fr.properties
  • 配置文件    - &GT;面对-config.xml中

哪里有问题?

0 个答案:

没有答案