Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting OS language in java

I'm running into a kind of problem here.

I'm French and working on an English version of Windows XP. Therefore, I set the regional options to French, but still have an English language UI.

I'm working on a small Java SE application, and decided to internationalize it using resources bundle.

To display the proper language, I create the bundle with this function :

private static ResourceBundle bundle = ResourceBundle
          .getBundle("locale.Strings", Locale.getDefault());

But the Locale.getDefault() function returns the regional settings (meaning : French) and not the system UI language. As a result, my UI defaults to French, in an English environment. And well, that's not really what I expected...

Does anyone knows of a platform-independent way to recover the system UI language ? Thanks in advance !

Edit : fixed Local to Locale, thanks.

like image 764
Redwarp Avatar asked Jun 02 '12 13:06

Redwarp


People also ask

What are locales in Java?

A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the Locale to tailor information for the user.

How do I change Applocale to Japanese?

Under the Language for non-Unicode programs section, click Change system locale and select the desired language. Restart the computer to apply the change.


2 Answers

This is a misconfiguration in Windows. The Locale#getDefault() returns the system locale, not the date/time formatting region or location.

In the below Windows XP specific screenshot, you could just set the Regional Options and Language to French or whatever you like. The dropdown in the Advanced menu actually sets the system locale and should in your case be set to English.

enter image description here

Admittedly, this is in Windows XP poorly explained, Windows 7 does it somewhat better:

enter image description here

like image 155
BalusC Avatar answered Sep 21 '22 10:09

BalusC


I have no means to try it out (as I tend to avoid anything made by Microsoft), but take a look at these:

Java 7 required:

Locale uiLocale = Locale.getDefault(Locale.Category.DISPLAY);

That's what should be used for getting translations (starting from Java 7), anyway.

If this was not very helpful, I'd try:

System.out.println(System.getenv("LC_MESSAGES"));       
System.out.println(System.getenv("LANG"));
System.out.println(System.getenv("LANGUAGE"));

However, in this case I would expect some similarities to default Locale...

like image 21
Paweł Dyda Avatar answered Sep 22 '22 10:09

Paweł Dyda