2

I want to get my system locale using Java 8's java.locale.providers property. My system locale is UK but this code returns US every time. What am I doing wrong?

String localeProvidersList = System.getProperty("java.locale.providers",  "HOST");      
System.out.println(Locale.getDefault().getCountry());
umairaslam
  • 367
  • 3
  • 22

1 Answers1

1

Reading the documentation, the following seems relevant:

Retrieve the default locale using the following method:

public static Locale getDefault()

The default locale of your application is determined in three ways. First, unless you have explicitly changed the default, the getDefault() method returns the locale that was initially determined by the Java Virtual Machine (JVM) when it first loaded. That is, the JVM determines the default locale from the host environment. The host environment's locale is determined by the host operating system and the user preferences established on that system.

Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties.

⋮

Third, your application can call the setDefault(Locale aLocale) method. The setDefault(Locale aLocale) method lets your application set a systemwide resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.

So assuming there are no calls to Locale.setDefault(), I'd rule out anything overriding the "hosts default locale" by using something like the following:

public class Default { 
    public static void main(String[] args) { 
        System.out.println(System.getProperty("user.language")); 
        System.out.println(System.getProperty("user.country")); 
        System.out.println(System.getProperty("user.variant")); 
    } 
} 

And if that doesn't provide any suggestion as to why you're getting a US locale, I'd have to presume that somehow your computer doesn't have its locale set as you expect, or else it's failing to tell the JVM correctly.

Edd
  • 3,724
  • 3
  • 26
  • 33