Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect operating system language (locale) from java code

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.

like image 643
Sudhir Avatar asked Mar 18 '10 11:03

Sudhir


People also ask

How does JVM determine default locale?

That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

What is operating system locale?

The operating system locale defines the language and region of users. Based on the business requirements, set the same operating system locale for the Hub Server, the Hub Store, and the Hub Console.


2 Answers

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.


UPDATE: Apparently, Java 7 has changed the way that the locale information used by getDefault() is determined on Windows; see https://stackoverflow.com/a/8319889/139985

like image 172
Stephen C Avatar answered Oct 01 '22 12:10

Stephen C


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

like image 27
Markus Lausberg Avatar answered Oct 01 '22 12:10

Markus Lausberg