为什么我不能更改我的语言环境ResourceBundle - JSF?

时间:2012-11-29 21:44:51

标签: jsf-2 internationalization locale

我怀疑我只是在做一些愚蠢的事情,但我无法弄清楚为什么我不能在我的JSF应用程序中更改属性文件。我看了几个例子,看不出我哪里出错了。

我有3个文件“localizedMessages_en_US.properties”,“localizedMessages_en_CA.properties”和“localizedMessages_es_MX.properties”。所有文件都有两个login_username和login_password项,每个文件中都有不同的值供测试。

我已将它们添加到我的faces-config.xml文件中,如下所示:

    <application>
    <resource-bundle>
        <base-name>/properties/localizedMessages</base-name>
        <var>localeMsg</var>
    </resource-bundle>
    <locale-config>
        <default-locale>en_US</default-locale>
        <supported-locale>en_CA</supported-locale>
        <supported-locale>es_MX</supported-locale>
    </locale-config>
</application>

我已将语言环境添加到我的f:view组件:

<f:view id="loginPage" contentType="text/html" locale="#{loginController.locale}"

我在我的xhtml代码中将它们称为#{localeMsg.login_username} #{localeMsg.login_password

对于我的默认“en_US”文件,一切似乎都运行正常,但我想更改为其他文件。 我有3个组件,如:

  <h:commandButton id="locUs" image="/resources/Icons/Locale/us.png" action="#{loginController.setNewLocale('en_US')}" />
  <h:commandButton id="locCa" image="/resources/Icons/Locale/ca.png" action="#{loginController.setNewLocale('en_CA')}" />
  <h:commandButton id="locMx" image="/resources/Icons/Locale/mx.png" action="#{loginController.setNewLocale('es_MX')}" />

对象是在不同的语言环境之间切换。我的setNewLocale()方法如下所示:

    public String setNewLocale( String loc ) {
    System.out.println( "<LoginController.setNewLocale> Locale = "+loc );
    this.locale=new Locale(loc);
    FacesContext.getCurrentInstance().getViewRoot().setLocale(this.locale);
    String language = locale.getLanguage();
    resourceBundle = ResourceBundle.getBundle("properties.localizedMessages",locale);
    String s = resourceBundle.getString( "login_username" );
    System.out.println( "<LoginController.setNewLocale> locale = "+locale+" resourceBundle = "+resourceBundle+", username = "+s+", language = "+language);
    return "LoginPage.xhtml";
}
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
public Locale getLocale() { return this.locale; }
public void setLocale( Locale l ) { this.locale = l; }

日志显示我得到了正确的loc值:

INFO: <LoginController.setNewLocale> Locale = es_MX
INFO: <LoginController.setNewLocale> locale = es_mx resourceBundle = java.util.PropertyResourceBundle@48812829, username = Username, eh?:, language = es_mx
INFO: <AuthorizationListener>...testing /LoginPage.xhtml
INFO: <LoginController.setNewLocale> Locale = en_US
INFO: <LoginController.setNewLocale> locale = en_us resourceBundle = java.util.PropertyResourceBundle@48812829, username = Username, eh?:, language = en_us
INFO: <AuthorizationListener>...testing /LoginPage.xhtml
INFO: <LoginController.setNewLocale> Locale = en_CA
INFO: <LoginController.setNewLocale> locale = en_ca resourceBundle = java.util.PropertyResourceBundle@48812829, username = Username, eh?:, language = en_ca

我正在尝试重置资源包并使用以下命令读取login_username参数:

        resourceBundle = ResourceBundle.getBundle("properties.localizedMessages",locale);
    String s = resourceBundle.getString( "login_username" );

但我只是在默认属性文件中获取值。我怀疑文件被正确引用,它们都在同一个目录中,我可以毫无问题地引用默认文件。

有谁能让我知道我做错了什么?我已经尝试了我能想到的这组代码的所有变体,但无济于事。

由于

0 个答案:

没有答案
相关问题