views:

179

answers:

2

Why is the spring "ReloadableResourceBundleMessageSource" unable to find the proper message associated with a code when Locale.getDefault() returns en, but able to find the proper message when it returns en_US

Default Locale: en_US

key: CODE_1, Locale: Locale.US           = Hit
key: CODE_1, Locale: Locale.CANADA       = Hit
key: CODE_1, Locale: fr_CA               = Hit
key: CODE_1, Locale: null (use default)  = Hit

Default Locale: en

key: CODE_1, Locale: Locale.US               = Hit
key: CODE_1, Locale: Locale.CANADA           = CODE_1
key: CODE_1, Locale: fr_CA                   = CODE_1
key: CODE_1, Locale: null (use default)      = CODE_1

I have only one bundle that has the CODE_1 in the classpath which is message_en_US.properties

+1  A: 

It's because it cannot find less country/language-specific properties files messages_en.properties and messages.properties.

If you'd like to have a default bundle for all en languages regardless of the country, then you should have a messages_en.properties. If you'd like to have a default bundle for all languages, then you should have a messages.properties.

The messages_en_US.properties is in fact only useful whenever you have multiple English language dialects like en_UK and en_US, but then you should really also supply the another one for the other English "dialect". One of them should be named messages_en.properties which can then be used as "default" English bundle for visitors which are not specifying the country.

BalusC
+3  A: 

The message is only defined for the en_US locale, since it's defined in the message_en_US.properties file. The reason that you get different behaviour when switching from locale en_US (Locale.US) to en is that ReloadableResourceBundleMessageSource by default does a fallback to the system locale if a message is not found in the requested locale.

In the first case, when en_US is the default, then the message is always found, since the the requested local is en_US or when not, the message source does a fallback is to en_US, where the message is defined.

When the default locale is en, only the en_US query works, since that is looking in the correct properties file. For all others, the message is not found in the corresponding properties file, and it is also not found in the fallback locale en.

mdma