Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring internationalization: How to dynamically set locale value

I am trying to implement internationalization through spring.Following are the configurations I have done

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

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />`

now I have three properties file - message_en.properties,message_fr.properties,message_sp.properties. and use it in my jsp with JSTL tag. My question is how can I pass locale value so that it can pick up right properties file? One way of doing is to pass in request url but my application is too huge to include this request parameter in every url. Is there any other way I can set locale value? I have locale value stored in DB from which I have to fetch and set. How can I achieve this with the best approach?

Able to change the locale using below RequestContextUtils.getLocaleResolver(request).setLocale(request, response, Locale.FRANCE);

But this will require to write a filter to execute on each request and firing a query in DB to get the value. Is there some better appraoach to do so?

like image 595
SCoder Avatar asked May 29 '13 09:05

SCoder


1 Answers

I'm guessing you've probably found a solution by now, but for future people who stumble on the question, perhaps you need to get rid of the LocaleResolver?

According to Mkyong:

If you do not register any “localeResolver”, the default AcceptHeaderLocaleResolver will be used, which resolves the locale by checking the accept-language header in the HTTP request.

Allowing a user's browser to automatically say which language it prefers makes a lot of sense, and it's easy to test in Chrome by changing your preferred language (and probably other browsers too).

(Mkyong's tutorial is pretty handy by the way and worth running through)

like image 183
StackExchange What The Heck Avatar answered Nov 11 '22 23:11

StackExchange What The Heck