2

有人知道下一种方法有什么问题吗?

public static String getMessageBundleString(String key, String localeAcronym) throws MissingResourceException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String messageBundleName = facesContext.getApplication().getMessageBundle();
    ResourceBundle bundle = ResourceBundle.getBundle(messageBundleName, new Locale(localeAcronym));
        //THE LOCALE OF THIS BUNDLE IS ALWAYS 'es_ES' !!!
    return bundle.getString(key);
}

Primefaces/JSF环境中,我想从正确的 i18n 属性文件中检索密钥。

但它总是从messages_es_ES.properties文件中获取它。

以下调用返回相同的值(='Inicio'):

getMessageBundleString("home", "es_ES")
getMessageBundleString("home", "uk_UK")

messages_es_ES.properties:
home=Inicio

messages_uk_UK.properties:
home=Home

谢谢

这是我的一些 faces-config.xml 内容:

<application>
    <locale-config>
        <supported-locale>es_ES</supported-locale>
        <supported-locale>uk_UK</supported-locale>
    </locale-config>    
    <message-bundle>cfg.i18n.messages</message-bundle>
    <resource-bundle>
        <base-name>cfg.i18n.messages</base-name>
        <var>msgs</var>
    </resource-bundle>
</application>
4

2 回答 2

1

此变体有效(分离国家和语言):

public static String getMessageBundleString(String key, String language, String country) throws MissingResourceException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    String messageBundleName = facesContext.getApplication().getMessageBundle();
    ResourceBundle bundle = ResourceBundle.getBundle(messageBundleName, new Locale(language, country));
        //Valid ones: "es","ES"; "en","GB"!!!
    return bundle.getString(key);
}
于 2013-01-03T14:55:21.307 回答
0

你试过这个变化吗?

getMessageBundleString("home", "es")
getMessageBundleString("home", "en")
messages_es.properties:
home=Inicio
messages_en.properties:
home=Home
于 2013-01-03T12:25:55.927 回答