gpt4 book ai didi

java - 使用 java.locale.providers 获取系统语言环境

转载 作者:行者123 更新时间:2023-11-30 11:11:29 25 4
gpt4 key购买 nike

我想使用 Java 8 的 java.locale.providers 属性获取我的系统区域设置。我的系统区域设置是英国,但此代码每次都返回美国。我做错了什么?

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

最佳答案

阅读 the documentation ,以下内容似乎相关:

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.

因此,假设没有调用 Locale.setDefault(),我会排除任何覆盖“主机默认语言环境”的情况,方法如下:

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"));
}
}

如果这没有提供任何关于为什么您获得美国语言环境的建议,我不得不假设您的计算机以某种方式没有按照您期望的方式设置其语言环境,否则它无法告诉JVM 正确。

关于java - 使用 java.locale.providers 获取系统语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27406712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com