Problem w/ ResourceBundle and default locale

I have a couple of resource bundles:

- Messages.properties (default, in English)

- Messages_nl.properties

- Messages_fr.properties

Windows' regional settings are set to Dutch (Belgium) which makes the Java default locale 'nl_BE'.

I want to get the resource bundle using the locale 'en'.

I expect to get 'Messages.properties' but I get 'Messages_nl.properties' instead.

Apparently, the lookup algorithm goes like this:

look for Messages_en.properties -> not found

look for Messages_'default locale'.properties = Messages_nl_BE.properties -> not found

look for Messages_nl.properties ->found

I have a solution, but it isn't a very good one:Locale locale =new Locale("en");

...

Locale defaultLocale = Locale.getDefault();

Locale.setDefault(locale);

ResourceBundle resourceBundle = ResourceBundle.getBundle("Messages", locale);

Locale.setDefault(defaultLocale);

Do you see a more elegant solution?

Thanks.

[1157 byte] By [NickDGa] at [2007-11-27 1:29:05]
# 1

I don't know that it's more elegant, but it's probably the one I would use:

Include a Messages_en.properties resource bundle. I believe that this resource bundle can actually simply be empty (the lookup will not find any entries in the _en version, and it will then look in the base version - the default locale will not be used in this scenario, where a bundle with the _en ID is found). Otherwise, If the bundle cannot be empty, it will be a copy of the base bundle.

one_danea at 2007-7-12 0:27:54 > top of Java-index,Desktop,I18N...
# 2

If you are using Java 6, you can specify fine grained resource loading strategy with ResourceBundle.Control class. In your case, I think you simply don't want to fallback to the default locale, right? If that's the case, getBundle("Messages", Locale.ENGLISH, Control.getNoFallbackControl(fmt)) will do. This call will not return the bundle for nl, but returns the base bundle.

HTH,

Naoto

naotoa at 2007-7-12 0:27:54 > top of Java-index,Desktop,I18N...
# 3
For now we're stuck with 1.5 so I'll keep my solution, but in the near future we will go with 6 and then I'll use your approach.Thanks.
NickDGa at 2007-7-12 0:27:54 > top of Java-index,Desktop,I18N...