java 为 en-US 和 en_US 创建语言环境有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632885/
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-10-30 07:18:17  来源:igfitidea点击:

What is the difference between creating locale for en-US and en_US?

javainternationalization

提问by Jothi

I am having all my resourcebundle values in table and formatted as per requirement.i have to change the languages in the website based on User selection in drop down in top of the page. If i use language code as en_US then its working fine. if i Use en-Us as Language Code then its not working. What might be the problem. Which is correct to follow?

我的所有资源包值都在表格中,并按照要求进行了格式化。我必须根据页面顶部下拉菜单中的用户选择更改网站中的语言。如果我使用语言代码作为 en_US 那么它工作正常。如果我使用 en-Us 作为语言代码,则它不起作用。可能是什么问题。遵循哪个正确?

采纳答案by Naved

"en" is the language code specified by ISO 639. while US is country code specified by 3166.
In Java, the Locale object recognizes the language as languageCode_countryCode(e.g. en_US) and not as languageCode-countryCode.

“en”是 ISO 639 指定的语言代码。而 US 是 3166 指定的国家/地区代码。
在 Java 中,Locale 对象将语言识别为languageCode_countryCode(例如 en_US)而不是languageCode-countryCode

回答by Laurence Gonsalves

"en-US" is an IETF language tag. While Java'a Localeclass was clearly based on IETF language tags, it uses underscores in place of hyphens when separating language codes from country codes (and also variants), so calling toString()on the equivalent Localewill give you en_US.

“en-US”是一个IETF 语言标签。虽然 Java'a Locale类显然基于 IETF 语言标签,但在将语言代码与国家/地区代码(以及变体)分开时,它使用下划线代替连字符,因此调用toString()等效项Locale将为您提供en_US.

As of Java 7 you can use Locale.forLanguageTag(String)and toLanguageTag()to convert between language tags and Localeobjects.

从 Java 7 开始,您可以使用Locale.forLanguageTag(String)toLanguageTag()在语言标记和Locale对象之间进行转换。

When converting strings to Localeobjects it's a good idea to normalize by splitting components on hyphens and underscores, lowercasing the first component (the language code) and upper-casing the second component (the country code).

将字符串转换为Locale对象时,最好通过在连字符和下划线上拆分组件来规范化,将第一个组件(语言代码)小写并大写第二个组件(国家/地区代码)。

回答by user3763100

Or you could use Locale us = Locale.forLanguageTag("en-US")and us.toLanguageTag(), and that will do the conversion for you without having to create your own error-prone implementation.

或者您可以使用 Locale us = Locale.forLanguageTag("en-US")and us.toLanguageTag(),这将为您进行转换,而无需创建自己的容易出错的实现。

回答by Sai Chaitanya

As of Java8, Initializing the locale should be done using the language tag "en-US" Locale.forLanguageTag("en-US").toString(); returns the output: en_US

从 Java8 开始,应该使用语言标签“en-US”Locale.forLanguageTag("en-US").toString(); 来初始化语言环境。返回输出:en_US

Where as Locale.forLanguageTag("en_US") does not create the required locale. It will default to the system locale. Locale.forLanguageTag("en_US").toString() returns null

因为 Locale.forLanguageTag("en_US") 不会创建所需的语言环境。它将默认为系统区域设置。Locale.forLanguageTag("en_US").toString() 返回 null