PDA

View Full Version : Session localization


3lite
Dec 10th, 2007, 09:08 AM
I would like to localization without forcing the user to enable cookies. I thought that allowing a request parameter like ?lang=en would be nice because it would also allow the users to bookmark the page to display in their desired language.
However, I cannot find any reasonable documentation on how to do this on either the online Spring documentation or in the book Pro Spring, both of which have a mere 1-sentence to describe that SessionLocaleResolver exists, but without actually saying how to use it.
As far as I can make it out, I have added the following entries to the dispatcher-servlet.xml file:

<bean
class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="interceptors">
<list>
<ref bean="langChangeInterceptor" />
</list>
</property>
<property name="mappings">
<value>
/**/*.html=defaultController
/=defaultController
</value>
</property>
</bean>

<bean id="langChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeI nterceptor">
<property name="paramName" value="lang" />
</bean>

<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocale Resolver" />

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


I also have a pair of messages files:

messages.properties
messages_de.properties


I have done nothing else. Is this really all that is needed for session localization in Spring or do I also need to programatically do something to enable it?

Strangely enough, I get by default the German messages despite the fact that I am running on en_US Windows XP with a local en_US installation of Tomcat 6, an en_US installation of Java 6 and calling the webapp in an en_US version of Firefox 2! I have no idea where it is getting the German language/locale from and have even placed debugging output in my controller class where request.getLocale() returns en_US as expected.

So... why is it localizing to German by default and why doesn't it change the localization even when I give ?lang=en in the request as a parameter in the URL?

3lite
Dec 10th, 2007, 09:36 AM
Further research with firebug shows that the generated HTML from the JSP Document contains: lang="en" xml:lang="en" in the <html> tag.
And that the request header contains: Accept-Language en-US,en;q=0.8,en-us;q=0.6,de-de;q=0.4,hu;q=0.2
And that the response header contains: Content-Language en

This seems to make it even more baffling why the German localization is being displayed...

3lite
Dec 10th, 2007, 09:53 AM
I copied the default messages.properties file (which contains the English messages) in to a new file called messages_en.properties.
Now when I call the webpage with the parameter ?lang=en, the output is in English. If I use ?lang=en_US or ?lang=en-US or in fact any other language value (legal or otherwise), the German messages are displayed.
Why is my messages_de.properties file being used as the default and not the messages.properties file?

3lite
Dec 10th, 2007, 10:46 AM
Now I'm really lost where Spring is getting it's localization information from. If I call the page with the parameter ?lang=hu then the debug output in the controller shows that the request locale is en_US, but the response Content Language is hu and the messages are displayed from the messages_de.properties file! Ugh...

3lite
Dec 11th, 2007, 03:48 AM
This thread could be useful for others with a similar problem to me:
http://forum.springframework.org/showthread.php?t=36339

Unfortunately, this is not the problem that I am having. Although I do have a JSP Document, it is not accessed directly and passes through the Dispatcher Servlet. I can also happily change languages for the JSP as long as the value of the ?lang parameter exactly matches the language suffix of a resource file. Otherwise it defaults to German (de), even though the language in my default messages file is English and the default Locale is en_US.

3lite
Dec 11th, 2007, 04:27 AM
I found the solution in this thread:
http://forum.springframework.org/showthread.php?t=46421

The key is to set the following property in the messageSource bean:

<property name="fallbackToSystemLocale" value="false" />


It is not necessary to set any cache timeout nor to place the language resource files outside of the classpath. The caching is a separate issue to this one.

What I failed to do was read the detailed javadoc for each and every method in the class. Are you really surprised that I didn't? The only place that this behaviour is mentioned is in the detailed javadoc for the method setFallbackToSystemLocale. It's not mentioned in the online Spring documentation or tutorials nor in the Pro Spring book I bought.

3lite
Dec 11th, 2007, 04:40 AM
That must mean that my Tomcat installation has a default language/locale of de. I wonder how that happened?
Maybe this is an example of why you shouldn't confuse locale with language. My company proxy server sits in Austria, so maybe (though I don't know this for sure), the Apache website detected this locale and gave me a German version of Tomcat, even though I was viewing the page in English.