Search code examples
springinternationalizationspring-mvcresourcebundle

spring local-sensitive data


I have some problems with making my web-app adapted for remote user browseres language settings. I used ResourceBundleMessageSource for it. It looks like this:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="resources/messages" /> </bean>

And I have two .properties files:

  • messages.properties

  • messages_ru.properties

They are located in src/resources/messages.

I want to configure my app such way, that when remote users browser language settings contain russian, my app should use russian messages (messages_ru), otherwise (if language is not configured or it's not russian), it should use meessages.properties (it contains english messages).

When I setup russian in my browser, it works fine. When I erase all settings, it's also shows russian (I think it depends on system locale settings). And when I set some another language, it also show me russian messages.

Only way to make my app to show english messages is to rename messages.properties to messages_en.properties and set in browser english lang. But I want to make my app adopted for all language settings (when it use russian, if it needs, and english for any other settings and any other countries).

Any ideas?


Solution

  • You need to set

    <bean id="messageSource" 
         class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
        <property name="basename" value="resources/messages" />
        <property name="fallbackToSystemLocale" value="false" />
    </bean> 
    

    In this case Spring will use messages.properties as a fallback, so it should contain messages in the "default" locale for your application.