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

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

问题描述

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

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,并使用 Locale 对象上的方法,例如 getCountry(), getLanguage() 获取详细信息.这些信息可使用 ISO 代码和人类可读/可显示的名称获得.

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 改变了 getDefault() 使用的区域设置信息在 Windows 上的确定方式;见 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天全站免登陆