• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting the current Locale.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to get the current locale in java? Locale.getDefault() only gets the default locale, not the current one.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Locale returned by Locale#getDefault() is the current default one determined by the JVM at startup. It will be the Locale used unless you explicitly change this to another Locale using Locale#setDefault(), or pass a Locale explicitly to a method that supports it. An example of the latter would be the String#toLowerCase() / String#toUpperCase() methods. In the context of Servlets, the ServletRequest class has methods to determine a client's preferred Locale. Is that perhaps what you are after?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, there's no JVM-wide "current locale" - only a default locale.
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My system has 2 installed locales, the default is en-US and the other is he-IL. When I start my JVM the Locale is toggled to the default one (en-US), then I can change the Locale by using alt-shift (in Windows) , my computer's Locale is now he-IL. So now I want some method that will get me the Locale the computer is toggled to (in this case he-IL). Locale.getDefault() does not work because it always returns the default Locale (in this case en-US) regardless of which Locale the computer is toggled to.
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:In other words, there's no JVM-wide "current locale" - only a default locale.


So does this mean that what I am trying to accomplish is impossible?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know there is no method in the standard Java library to get the current setting of Windows, so this is not something that is very easy to do. If you really want to do this, you will probably have to write some native code (in another language than Java - probably in C or C++) that calls a Windows-specific API to get this information.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark King wrote:the other is he-IL.


Don't you mean iw-IL...

Now, I know that he has officially replaced iw. However, Locale doesn't like new codes he, yi and id, but sticks to the old ones (iw, ji and in respectively). This is well documented in the Locale API, but it's still a pain in the behind. I had to work on an application recently that required support for he_IL. Unfortunately, when we tried to get the language code back from the Locale we kept on getting iw instead of he, and because of that we got English translations and settings instead of Hebrew. We still have to fix that...
 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have found a partial workaround, I used a robot to press the 'a' key, then I check the input to see if I got the Hebrew letter Shin (Which would be typed if the locale is set to he-IL) . This should work for any Locale that uses special characters.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting workaround, Mark... maybe not ideal, but at least it doesn't require you to call native code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic