2

does somebody know whats wrong with the next method?

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);
}

In a Primefaces/JSF environment, I want to retrieve a key from the proper i18n properties file.

But it always take it from the messages_es_ES.properties file.

Following invocations return same value (='Inicio'):

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

messages_es_ES.properties:
home=Inicio

messages_uk_UK.properties:
home=Home

Thanks

Here some of my faces-config.xml content:

<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>
JLLMNCHR
  • 1,551
  • 5
  • 24
  • 50
  • Have you declared your language files into faces-config.xml? Take a look to this post [What is the use of faces-config.xml in JSF 2?](http://stackoverflow.com/questions/7583038/what-is-the-use-of-faces-config-xml-in-jsf-2) – Aritz Jan 03 '13 at 12:25
  • Yes, I have some stuff declared in the faces-config.xml – JLLMNCHR Jan 03 '13 at 12:55

2 Answers2

1

This variant works (separating country and language):

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);
}
JLLMNCHR
  • 1,551
  • 5
  • 24
  • 50
0

Did you try this variation?

getMessageBundleString("home", "es")
getMessageBundleString("home", "en")
messages_es.properties:
home=Inicio
messages_en.properties:
home=Home
Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53