bespectacled
bespectacled

Reputation: 2901

Detect windows system locale in java

Java Locale.getDefault() always returns en-US on windows even though the system locale is set to a different language (through Region and Language control panel). Running a script such as systeminfo | findstr /C:\"System Locale\ inside Java Runtime.exec() takes lot of time.

Is there a quicker way to detect system locale on (java) application startup? Running the above cmd once and writing to a property file for subsequent reads maybe one solution. But what if the system locale is changed and system restarted? What is a more reliable way to find system locale in jdk ?

Upvotes: 0

Views: 1754

Answers (2)

jwenting
jwenting

Reputation: 5663

Locale.getDefault() returns the default Locale for the current JVM, NOT for the operating system. The default Locale for the JVM defaults to en-US for most JVMs (there might be localised ones out there, I've never seen one).

It is possible to set it to something else using Locale.setDefault(Locale newDefault) after which Locale.getDefault() will return that Locale instead.

So one solution for your problem might be to get the default system Locale using the command you have once, set that as the new JVM default using Locale.setDefault() when starting the application, then retrieve that again using Locale.getDefault().

Upvotes: 2

Mayank Sharma
Mayank Sharma

Reputation: 423

Locale.getDefault();           // 
locale.getDisplayLanguage();   // Return current language
locale.getDisplayCountry();    // Return current country

Upvotes: -1

Related Questions