为什么在使用芬兰语的情况下Java无法正确显示ISO 639-2语言代码?使用ISO 639代码时,它会以芬兰语提供正确的语言名称.
public static void main(String[] args) {
Locale fi = new Locale("fi");
Locale fin = new Locale("fin");
Locale en = new Locale("en", "EN");
Locale sv = new Locale("sv");
System.out.println("fi: " +fi.getDisplayLanguage(fi));
System.out.println("fin: " +fin.getDisplayLanguage(fi));
System.out.println("sv: " +fin.getDisplayLanguage(sv));
System.out.println("en: " +fin.getDisplayLanguage(en));
}
fi: suomi
fin: Finnish***?? Why this is in English, not in Finnish?
sv: Finska
en: Finnish
解决方法:
这是因为“ fin”不是Locale(String)构造函数的language参数的有效值.
根据documentation,语言参数应为(强调我的):
ISO 639 alpha-2 or alpha-3 language code, or registered language subtags up to 8 alpha letters (for future enhancements). When a language has both an alpha-2 code and an alpha-3 code, the alpha-2 code must be used. You can find a full list of valid language codes in the IANA Language Subtag Registry (search for “Type: language”). The language field is case insensitive, but Locale always canonicalizes to lower case.
因为芬兰语具有alpha-2代码(“ fi”),所以您应该使用它,而不是“ fin”.否则,您实际上是在为某种默认语言环境(英语?)创建一个语言环境对象.