views:

716

answers:

4

What is the correct way of knowing operating system language (locale) from java code?

I have tried

Locale.getDefault()
System.getProperties("user.language")

etc.

but they are not correct nothing actually displays the "System Locale" which is available by the command "systeminfo" in windows.

Please help.

A: 

How about using the default locale?

Locale locale = Locale.getDefault();
String lang = locale.getDisplayLanguage();
String country = locale.getDisplayCountry();

This returns me my current language and country as per the Windows systeminfo command. Is this what you're looking for? (If you want the 2-character codes for language/country, you can just use getLanguage() or getCountry()).

Ash
Sorry, i forgot to mention that, i changed my language setting to "french" through control panel->regional and language options. and when i ran above code it gives correct (changed) locale as "fr" but when i run "systeminfo" it still displays "en-US" as System and Input Locale.
Sudhir
So my question is, Is that because of difference between "user locale" and "system locale", and what we change through control panel is "user locale" and not the "system locale", and above code returns "user locale"?then what is the method to get the "system locale"?
Sudhir
A: 

The Windows XP systeminfo command displays lots of stuff, but the relevant information is this:

System Locale:             en-us;English (United States)
Input Locale:              en-us;English (United States)

To get equivalent information in Java, use Locale.getDefault() to get the Locale that Java is using, and use methods on the Locale object such as getCountry(), getLanguage() to get details. The information is available using ISO codes and as human readable/displayable names.

Note that Locale.getDefault() gives you the locale that Java picks up from the environment when it starts, this may or may not be the same as the "system" locale. To definitively get the "system" locale in Java you would need to do platform specific things. IMO, it is simpler to make sure that Java gets started with the system locale ... if you really need that information.

Stephen C
Sorry, i forgot to mention that, i changed my language setting to "french" through control panel->regional and language options.and when i ran above code it gives correct (changed) locale as "fr" but when i run "systeminfo" it still displays "en-US" as System and Input Locale.
Sudhir
You may need to close your command prompt and open it again. As with many things Windows, it is not entirely clear where "systeminfo" is getting the information that it is displaying.
Stephen C
A: 

system locale cannot be get through java directly... for that some native code have to be written.

sreejith
A: 

What about

System.getProperty("user.country"); 
System.getProperty("user.language");

Returns in my case

user.country=DE

user.language=de

You easily can generate the locale from this information. Local is 'language'_'country' so in my case de_DE

Markus Lausberg