简体   繁体   English

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

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

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. 请帮忙。

The Windows XP systeminfo command displays lots of stuff, but the relevant information is this: Windows XP systeminfo命令显示了很多东西,但相关信息如下:

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. 要在Java中获取等效信息,请使用Locale.getDefault()获取Java正在使用的Locale,并使用Locale对象上的方法getCountry()getCountry()getLanguage()来获取详细信息。 The information is available using ISO codes and as human readable/displayable names. 该信息可使用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. 请注意, Locale.getDefault()为您提供Java在启动时从环境中获取的语言环境,这可能与“系统”语言环境相同或不同。 To definitively get the "system" locale in Java you would need to do platform specific things. 要明确获得Java中的“系统”语言环境,您需要执行特定于平台的事情。 IMO, it is simpler to make sure that Java gets started with the system locale ... if you really need that information. IMO,确保Java开始使用系统区域设置更简单...如果您真的需要这些信息。


UPDATE: Apparently, Java 7 has changed the way that the locale information used by getDefault() is determined on Windows; 更新:显然,Java 7改变了在Windows上确定getDefault()使用的语言环境信息的方式; see https://stackoverflow.com/a/8319889/139985 请参阅https://stackoverflow.com/a/8319889/139985

What about 关于什么

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

Returns in my case 在我的情况下返回

user.country=DE user.country = DE

user.language=de user.language = DE

You easily can generate the locale from this information. 您可以轻松地从此信息生成区域设置。 Local is 'language'_'country' so in my case de_DE 本地是'语言'_'国家'所以在我的情况下de_DE

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 systeminfo command. 这将根据Windows systeminfo命令返回我当前的语言和国家/地区。 Is this what you're looking for? 这是你在找什么? (If you want the 2-character codes for language/country, you can just use getLanguage() or getCountry() ). (如果你想要语言/国家的2个字符的代码,你可以使用getLanguage()getCountry() )。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM