如何从Java代码中检测操作系统语言(语言环境) [英] how to detect operating system language (locale) from java code

查看:198
本文介绍了如何从Java代码中检测操作系统语言(语言环境)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试过

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

但它们不正确,实际上什么也没有显示Windows中可通过命令"systeminfo"使用的系统区域设置".

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

请帮助.

推荐答案

Windows XP systeminfo命令显示了很多内容,但是相关信息是这样的:

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

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

要获取Java中的等效信息,请使用Locale.getDefault()获取Java正在使用的语言环境,并在Locale对象上使用方法,例如

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.

请注意,Locale.getDefault()为您提供Java启动时从环境中选取的语言环境,这可能与系统"语言环境相同或不同.为了最终获得Java中的系统"语言环境,您需要做特定于平台的事情. IMO,如果您确实需要该信息,那么确保Java从系统区域设置开始就更容易了.

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.

更新:显然,Java 7改变了在Windows上确定getDefault()所使用的语言环境信息的方式.参见 https://stackoverflow.com/a/8319889/139985

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代码中检测操作系统语言(语言环境)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆