java - 有没有办法获得国家 ISO-2 代码,但从 Windows 属性,而不是从 JVM
问题描述
基本上如标题所述,我已经尝试过:
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"));
当我在寡妇设置中设置了完全不同的东西时,这一切都给了我美国或英语。请给我任何建议。所以我希望得到 Contry 代码,例如,如果我在系统中设置了波兰,我期望“PL”但我得到“US”
解决方案
默认 Java 语言环境不必与系统语言环境相同,因为它可以被覆盖
- 具有
user.country
和系统属性user.language
;user.variant
- 通过调用
Locale.setDefault()
.
有用于获取操作系统提供的语言环境信息的非标准未记录 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;
}
推荐阅读
- kubernetes - 调用 webhook“v1.vseldondeployment.kb.io”失败。x509: 由未知机构签署的证书
- python - 混合视频流最佳实践
- api - { "statusCode": 405, "error": { "type": "NOT_ALLOWED", "description": "Method not allowed. must be one: OPTIONS" } }
- html - 如何在flask html中向下滚动到页面的一部分?
- python - cPanel App 在 Python2 而不是 Python3 上运行
- vuejs2 - Vue对非冻结数据的冻结列表进行排序
- webpack - 来自 webpack-dev-server 的访问模式
- flutter - 如何比较 2 TextFormField 中的值
- python - Dask Dataframe GroupBy.size() 返回 memoryError
- angular - 离子离子返回按钮仅导航到 defaultHref