Search code examples
javainternationalizationresourcestranslationlocale

Wrong Java resource bundle loaded


In my application, I'm using java resource bundle for the translation of its labels. I currently have two files:

  • resources.properties with labels in English (default language)
  • resources_fr.properties with labels in French

Then, I load the bundle properties with the following command:

ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, locale);

... where locale is the user's locale.

When I'm working with a French web browser, that's ok, I'm getting all my messages in French, as my locale is correctly set to fr. But when I switch my browser to English US, they're still in French!

The locale variable is correctly set with the en_US locale, but the getBundle method returns me a bundle with the fr locale instead of just returning the default bundle...

Is it a normal behaviour? I'm very surprised, as I was expecting the English values of resources.properties to be used when the locale has no specific resource bundle attached to it (like French)...


Solution

  • This might help to clarify your question:

    http://docs.oracle.com/javase/tutorial/i18n/resbundle/propfile.html

    These Locale objects should match the properties files created in the previous two steps. For example, the Locale.FRENCH object corresponds to the LabelsBundle_fr.properties file. The Locale.ENGLISH has no matching LabelsBundle_en.properties file, so the default file will be used.