有没有办法从 Windows 属性中获取国家的 ISO-2 代码,而不是从 JVM 中获取?

huangapple go评论78阅读模式
英文:

Is there any way to get country ISO-2 code but from windows properties, NOT from JVM

问题

基本上如标题所述,我已经尝试过:

```lang-java
Locale currentLocale = Locale.getDefault();
System.out.println(currentLocale.getDisplayLanguage());
System.out.println(currentLocale.getDisplayCountry());

System.out.println(currentLocale.getLanguage());
System.out.println(currentLocale.getCountry());

System.out.println(System.getProperty("user.country"));
System.out.println(System.getProperty("user.language"));

但是无论我在Windows设置中设置了其他内容,这些代码都给我返回美国或英语。请给我一些建议。所以我期望能够获取到国家代码,例如如果我在系统中设置了波兰,我期望得到"PL",但实际上得到的是"US"。


<details>
<summary>英文:</summary>

Basicly as title states, ive already tried:

```lang-java
Locale currentLocale = Locale.getDefault();
System.out.println(currentLocale.getDisplayLanguage());
System.out.println(currentLocale.getDisplayCountry());

System.out.println(currentLocale.getLanguage());
System.out.println(currentLocale.getCountry());

System.out.println(System.getProperty(&quot;user.country&quot;));
System.out.println(System.getProperty(&quot;user.language&quot;));

and it all gives me US or english when i have set something totally else in widows settings.
Please give me any advice.
So i would expect to get Contry Code, for example if i have set Poland in system, id expect "PL" but im getting "US"

答案1

得分: 2

以下是您要翻译的内容:

默认的Java区域设置不一定与系统区域设置相同,因为它可能会被覆盖:

  • 使用 user.countryuser.languageuser.variant 系统属性;
  • 通过调用 Locale.setDefault() 方法。

操作系统提供了一种非标准的未记录API,用于获取区域设置信息。该API在Windows Vista或更高版本上实现。请尝试以下代码片段:

import sun.util.locale.provider.HostLocaleProviderAdapter;

...

static String getHostCountry() {
    for (Locale locale : new HostLocaleProviderAdapter().getAvailableLocales()) {
        if (!locale.getCountry().isEmpty()) {
            return locale.getCountry();
        }
    }
    return null;
}
英文:

Default Java locale is not necessary the same as system locale, as it can be overridden

  • with user.country, user.language and user.variant system properties;
  • by calling Locale.setDefault().

There is non-standard undocumented API for getting locale information provided by the OS. It is implemented on Windows Vista or later. Try the following snippet.

import sun.util.locale.provider.HostLocaleProviderAdapter;

...

    static String getHostCountry() {
        for (Locale locale : new HostLocaleProviderAdapter().getAvailableLocales()) {
            if (!locale.getCountry().isEmpty()) {
                return locale.getCountry();
            }
        }
        return null;
    }

huangapple
  • 本文由 发表于 2020年10月7日 19:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64243363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定