英文:
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("user.country"));
System.out.println(System.getProperty("user.language"));
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.country
、user.language
和user.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
anduser.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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论