Getting OS language in java
Asked Answered
H

3

7

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.

Hufnagel answered 2/6, 2012 at 13:1 Comment(7)
Well, it seems to me that what Java does is the right thing. If you configured your OS with French preferences and the OS keeps displaying everything in English, but not Java, the OS isn't right, but Java is, isn't it? If you want everything in English, why don't you configure it with English preferences?Golgotha
shouldn't it be Locale.getDefault() and not Local.getDefault() ?Sailer
@JBNizet it's not such an unusual requirement. I'm Dutch and, like OP, I prefer my GUI language to be English, while still having number formats and date formats show up the way I'm used to from my own Locale (for example, American month-first dates are quite confusing to me, requiring extra parse-time in my brain). Software translations are often abysmal, doubly so in domains like programming where the jargon is English.Institutionalize
@Institutionalize Garvelink : exactly ! Thanks for understanding.Hufnagel
I'm a Belgian, French-speaking, living in France guy, and I can more or less understand your argument. But I would also find it strange to have my OS say something like: Hello, it's vendredi 13 décembre today.Golgotha
@JB Austrian with English GUI hhere. And you can configure Windows to still display "June 2012", but use german number formatting, metric measurements, the usual date and time formats, etc. If programs wouldn't often enough query the wrong data and then default to german for my language I really wouldn't notice any split. Sadly it's still not such a common configuration (although I'm sure many programmers in non-english speaking countries do have it) so it often slips through.Schizogony
My os says dd/MM/yyyy, and just for that I use french locale. (MM/dd/yyyy makes no sense to me but that's another debate :P)Hufnagel
D
7

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

Diann answered 2/6, 2012 at 13:41 Comment(1)
It is not that obvious. There is a difference between Java 7 and prior versions. Also, there might be difference between localized and MUI version of Windows, but in this case I would expect English rather than French. It's complicated :)Abrasion
L
3

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...

Lading answered 2/6, 2012 at 20:19 Comment(0)
H
0

I tried a few things thanks to your suggestions, and here is my observation :

  • If you are using Java 6, and not Java 7, you are f***ed.
  • If you are using Java 7, you should do as BalusC tells : change the setting in Region and Language Settings. After that, Locale.getDefault() will by default return the display language, meaning English if you set it to English. To make sure of it, you can create you locale by calling Locale.getDefault(Locale.Category.DISPLAY)
Hufnagel answered 3/6, 2012 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.