如何从 Java 代码中检测操作系统语言(语言环境)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2469435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:06:51  来源:igfitidea点击:

how to detect operating system language (locale) from java code

javawindowspropertiessystemlocale

提问by Sudhir

What is the correct way of knowing operating system language (locale) from java code?

从 Java 代码了解操作系统语言(语言环境)的正确方法是什么?

I have tried

我试过了

Locale.getDefault()
System.getProperties("user.language")

etc.

等等。

but they are not correct nothing actually displays the "System Locale" which is available by the command "systeminfo" in windows.

但它们不正确实际上没有显示“系统区域设置”,它可以通过 Windows 中的“systeminfo”命令获得。

Please help.

请帮忙。

采纳答案by Stephen C

The Windows XP systeminfocommand displays lots of stuff, but the relevant information is this:

Windows XPsysteminfo命令显示了很多东西,但相关信息是这样的:

System Locale:             en-us;English (United States)
Input Locale:              en-us;English (United States)

To get equivalent information in Java, use Locale.getDefault()to get the Locale that Java is using, and use methods on the Locale object such as getCountry(), getLanguage()to get details. The information is available using ISO codes and as human readable/displayable names.

要获取 Java 中的等效信息,请使用Locale.getDefault()获取 Java 正在使用的 Locale,并使用 Locale 对象上的方法(例如getCountry()getLanguage()来获取详细信息。这些信息可以使用 ISO 代码和人类可读/可显示的名称获得。

Note that Locale.getDefault()gives you the locale that Java picks up from the environment when it starts, this may or may not be the same as the "system" locale. To definitively get the "system" locale in Java you would need to do platform specific things. IMO, it is simpler to make sure that Java gets started with the system locale ... if you really need that information.

请注意,它Locale.getDefault()为您提供了 Java 在启动时从环境中获取的语言环境,这可能与“系统”语言环境相同,也可能不同。要最终获得 Java 中的“系统”语言环境,您需要执行特定于平台的操作。IMO,确保 Java 开始使用系统语言环境会更简单……如果您确实需要该信息。



UPDATE:Apparently, Java 7 has changed the way that the locale information used by getDefault()is determined on Windows; see https://stackoverflow.com/a/8319889/139985

更新:显然,Java 7 改变了getDefault()在 Windows 上确定使用的区域设置信息的方式;见https://stackoverflow.com/a/8319889/139985

回答by Ash

How about using the default locale?

使用默认语言环境怎么样?

Locale locale = Locale.getDefault();
String lang = locale.getDisplayLanguage();
String country = locale.getDisplayCountry();

This returns me my current language and country as per the Windows systeminfocommand. Is this what you're looking for? (If you want the 2-character codes for language/country, you can just use getLanguage()or getCountry()).

这将根据 Windowssysteminfo命令返回我当前的语言和国家/地区。这是你要找的吗?(如果您想要语言/国家/地区的 2 个字符代码,您可以使用getLanguage()getCountry())。

回答by Markus Lausberg

What about

关于什么

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

Returns in my case

在我的情况下返回

user.country=DE

user.language=de

用户.国家= DE

用户.语言=de

You easily can generate the locale from this information. Local is 'language'_'country' so in my case de_DE

您可以轻松地根据此信息生成语言环境。本地是 'language'_'country' 所以在我的情况下 de_DE

回答by Chinmoy

To be precise, you can try following code:

确切地说,您可以尝试以下代码:

public Locale getLocale() {
    if (this.locale == null) {
        this.locale = new Locale(System.getProperty("user.language"), System.getProperty("user.country"));
    }
    return this.locale;
}