0

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 ?

bespectacled
  • 2,801
  • 4
  • 25
  • 35
  • 1
    Possible duplicate of https://stackoverflow.com/questions/2469435/how-to-detect-operating-system-language-locale-from-java-code – JSN Aug 01 '17 at 04:57
  • 1
    https://stackoverflow.com/questions/7107972/java-7-default-locale/8319889#8319889 Same issue – Ognjen Mišić Aug 01 '17 at 05:10

2 Answers2

2

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

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • agreed, that is what we do presently. But like noted, the Runtime exec() takes lot of time to execute. Is there a way to shorten this execution time of external processed run within jvm? Provided the application should be localized before the home page is shown, doing this asynchronously does not seem an option. Is that right ? – bespectacled Aug 10 '17 at 13:29
-1
Locale.getDefault();           // 
locale.getDisplayLanguage();   // Return current language
locale.getDisplayCountry();    // Return current country
Mayank Sharma
  • 403
  • 2
  • 2
  • as noted, locale.getdefault always returns en-US. The other two do the same or return empty string. – bespectacled Aug 01 '17 at 05:31
  • 1
    @SuprajaJayakumar it doesn't have to return en-US. BUT it will return en-US unless another locale has been set as the default by an earlier call to Locale.setDefault(Locale). – jwenting Aug 01 '17 at 07:20