是否存在将ISO 639-2(3个字母)语言代码转换为Java语言环境的优美方法? [英] Is there an elegant way to convert ISO 639-2 (3 letter) language codes to Java Locales?
问题描述
例如eng,spa,ita,ger
E.g. eng, spa, ita, ger
我可以遍历所有语言环境并比较代码,但是我想知道是否还有更优雅的&高效的方法来实现这一目标....
I could iterate all locales and compare the codes, but I wonder whether there is a more elegant & performant way to achieve this....
非常感谢您提供任何提示:)
Thanks a lot for any hints :)
推荐答案
我不知道是否存在将3字母转换为2字母版本的简便方法,但是在更坏的情况下,您可以创建他们的地图,就像这样:
I don't know if there's an easy way to convert the 3-letter to the 2-letter versions, but in a worse case scenario, you could create a Map of them, like so:
String[] languages = Locale.getISOLanguages();
Map<String, Locale> localeMap = new HashMap<String, Locale>(languages.length);
for (String language : languages) {
Locale locale = new Locale(language);
localeMap.put(locale.getISO3Language(), locale);
}
现在您可以使用localeMap.get("eng")
;
修改了地图的创建方式.现在每种语言应该有一个对象.
Modified the way the map is created. Now there should be one object per language.
已经有一段时间了,但是在初始化Map时更改了代码以使用languages数组的实际长度.
Edit 2: It's been a while, but changed the code to use the actual length of the languages array when initializing the Map.
这篇关于是否存在将ISO 639-2(3个字母)语言代码转换为Java语言环境的优美方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!