作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
实例化 Locale
时具有以下任一语言代码的对象:he
、yi
和 id
它不会保留它们的值。
例如:
Locale locale = new Locale("he", "il");
locale.getLanguage(); // -> "iw"
是什么原因造成的,有什么办法可以解决这个问题吗?
最佳答案
Locale 类不会对您输入的内容进行任何检查,但会将某些语言代码换成它们的旧值。来自 the documentation :
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.
这是构造函数:
public Locale(String language, String country, String variant) {
this.language = convertOldISOCodes(language);
this.country = toUpperCase(country).intern();
this.variant = variant.intern();
}
这是神奇的方法:
private String convertOldISOCodes(String language) {
// we accept both the old and the new ISO codes for the languages whose ISO
// codes have changed, but we always store the OLD code, for backward compatibility
language = toLowerCase(language).intern();
if (language == "he") {
return "iw";
} else if (language == "yi") {
return "ji";
} else if (language == "id") {
return "in";
} else {
return language;
}
}
它创建的对象是不可变的,因此无法解决这个问题。该类也是 final
,因此您不能扩展它,也没有要实现的特定接口(interface)。使其保留这些语言代码的一种方法是围绕此类创建包装器并使用它。
关于locale - JDK Locale 类处理希伯来语 (he)、意第绪语 (yi) 和印度尼西亚语 (id) 的 ISO 语言代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974169/
我是一名优秀的程序员,十分优秀!