PDA

View Full Version : LocaleResolver and JVM default locale


janning
Aug 9th, 2007, 10:38 AM
Hi,

i need my webapp in different languages, but my base language is german, so i have two files:


messages.properties [german]
messages_en.properties


no i tested it on a server with a default locale of "en". No matter what locale is set via LocaleResolver i only got english messages.

The reason is how ResourceBundle.getBundle (http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html) looks for a file match:

baseName + "_" + language1 + "_" + country1 + "_" + variant1
baseName + "_" + language1 + "_" + country1
baseName + "_" + language1
baseName + "_" + language2 + "_" + country2 + "_" + variant2
baseName + "_" + language2 + "_" + country2
baseName + "_" + language2
baseName

where language1 is the specified locale from my LocaleResolver and language2 is the default Locale from the JVM (Locale.getDefault)

So if my specified locale is "de" i still got english messages because i don't have any messages_de.properties. It first checks messages_de.properties, then messages_en.properties and stops here.

I was looking how to fix it and came up with the following solutions:
* changing OS default locale
* starting JVM with -Duser.language=de
* subclassing a LocaleResolver and setting JVM locale at runtime (below)

public class MyCookieLocaleResolver extends CookieLocaleResolver {

private static Locale DEFAULT_JVM_LOCALE = new Locale("de");

public DefaultCookieLocaleResolver () {
setDefaultLocale(DEFAULT_JVM_LOCALE);
}

public void setDefaultLocale(Locale defaultLocale) {
Locale.setDefault(defaultLocale);
}
}

BTW: The defaultLocale attribute from CookieLocaleResolver doesn't help because it just sets the "specified locale" if no other locale could be resolved, but it does not set the JVM default Locale.

I do understand that falling back to OS default locale is a good idea. and you could always override OS default locale with JVM option, but i would rather like to set my "system default Locale" at runtime to be sure, no matter how or where my war is run: my app is shown in the language i want.

Just reporting this for other people having the same problem or did i miss a way to set the JVM default locale the-spring-way?

kind regards
janning