mmaceachran
mmaceachran

Reputation: 3348

What's the point of a default ResourceBundleMessageSource?

I am trying to set up some message bundling. I have my 2 files:

message.properties
message_en_US.properties

Beans:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en_US"/>
</bean>

Implementation:

 msgSrc.getMessage(key, null, Locale.getDefault());

Everything works fine, but I am confused as to why alot of tutorials out there have that message.properties file because if I get rid of the localeResolver and set the implementation to say

 msgSrc.getMessage(key, null, Locale.ENGLISH);

It still looks at messages_en_US, if get rid of the localResolver and still user Locale.getDefault, it uses my computers local, which is again en_US.

I can set message.properties specifically, but then the Locals don't work.

So under what situation would the message.properties ever get used?

I feel i am missing a concept.

(and is there any way to set it up so that if Spring does not find the key in messages_en_US.properties it would look in messages.properties?)

Upvotes: 1

Views: 1259

Answers (2)

Ralph
Ralph

Reputation: 120781

but I am confused as to why alot of tutorials out there have that message.properties

First of all, this is basic Java functionalatiy, provides by java.util.ResourceBundle.

Its simple: if you request a message property value for some locale (en_US), and that property does not exist in the en_US property file, ResourceBundle will look for the property in next more common locale (en), if this does not exist there, then it will look for the property in the "default" property file.


In your Spring example you have two steps, - first determine the local that is requested by the client (localeResolver), - second obtain the message for the key (and locale)

So if you want that the values from message.properties are used, then you need to set the local stored in the users session to something that is different from en_US, for example: RequestContext.changeLocale(new Locale("de"), and then of course you need to use the spring message tag, instead of msgSrc.getMessage(key, null, new Locale("en", "US")); An other example is that you direcly request the property for an "not existing" languag: msgSrc.getMessage(key, null, Locale.GERMAN);

Upvotes: 0

Mavlarn
Mavlarn

Reputation: 3883

I think if spring doesn't find the properties with the specified locale(with Locale.getDefault or get from system), then the 'message.properties' will be used. So, you can just remove messages_en_US.properties file, and use messages.properties as default for en_us and other locales.

Upvotes: 1

Related Questions