语言环境对象创建问题
Locale object creation issue
我有一个语言环境字符串 "fi_FI"
。当我像这样创建一个新的 java.util.Locale
对象时 -
String myLocaleStr = "fi_FI";
Locale locale = new Locale(myLocaleStr);
-并使用 sysout
语句检查 locale
信息值,我观察到显示的 Locale 值是 "fi_fi"
而不是 "fi_FI"
.
有什么方法可以保留语言环境值吗?
Locale constructor you use considers the unique argument you pass is the language. Not a combination of language and country. Split the string, and use the other constructor,接受语言和国家:
String[] languageAndCountry = myLocaleStr.split("_");
Locale locale = new Locale(languageAndCountry[0], languageAndCountry[1]);
我不确定 Java 的较新版本,但构造函数如下所示:
/**
* Construct a locale from language, country, variant.
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines
* (specifically iw, ji, and in) have changed. This constructor accepts both the
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
* API on Locale will return only the OLD codes.
* @param language lowercase two-letter ISO-639 code.
* @param country uppercase two-letter ISO-3166 code.
* @param variant vendor and browser specific code. See class description.
* @exception NullPointerException thrown if any argument is null.
*/
public Locale(String language, String country, String variant) {
this.language = convertOldISOCodes(language);
this.country = toUpperCase(country).intern();
this.variant = variant.intern();
}
你看到国家代码将被设置为大写了吗?这是根据 ISO 标准。
我有一个语言环境字符串 "fi_FI"
。当我像这样创建一个新的 java.util.Locale
对象时 -
String myLocaleStr = "fi_FI";
Locale locale = new Locale(myLocaleStr);
-并使用 sysout
语句检查 locale
信息值,我观察到显示的 Locale 值是 "fi_fi"
而不是 "fi_FI"
.
有什么方法可以保留语言环境值吗?
Locale constructor you use considers the unique argument you pass is the language. Not a combination of language and country. Split the string, and use the other constructor,接受语言和国家:
String[] languageAndCountry = myLocaleStr.split("_");
Locale locale = new Locale(languageAndCountry[0], languageAndCountry[1]);
我不确定 Java 的较新版本,但构造函数如下所示:
/**
* Construct a locale from language, country, variant.
* NOTE: ISO 639 is not a stable standard; some of the language codes it defines
* (specifically iw, ji, and in) have changed. This constructor accepts both the
* old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
* API on Locale will return only the OLD codes.
* @param language lowercase two-letter ISO-639 code.
* @param country uppercase two-letter ISO-3166 code.
* @param variant vendor and browser specific code. See class description.
* @exception NullPointerException thrown if any argument is null.
*/
public Locale(String language, String country, String variant) {
this.language = convertOldISOCodes(language);
this.country = toUpperCase(country).intern();
this.variant = variant.intern();
}
你看到国家代码将被设置为大写了吗?这是根据 ISO 标准。