有没有一种优雅的方法可以将 ISO 639-2(3 个字母)语言代码转换为 Java 语言环境?

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

Is there an elegant way to convert ISO 639-2 (3 letter) language codes to Java Locales?

javalocalizationlocale

提问by PeterP

E.g. eng, spa, ita, ger

例如 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 :)

非常感谢您的任何提示:)

回答by Powerlord

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:

我不知道是否有一种简单的方法可以将 3 个字母的版本转换为 2 个字母的版本,但在更糟糕的情况下,您可以创建它们的 Map,如下所示:

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);
}

Now you can look up locales using things like localeMap.get("eng");

现在,您可以使用诸如以下内容查找语言环境localeMap.get("eng")

Edit: Modified the way the map is created. Now there should be one object per language.

编辑:修改了地图的创建方式。现在每种语言应该有一个对象。

Edit 2: It's been a while, but changed the code to use the actual length of the languages array when initializing the Map.

编辑 2:已经有一段时间了,但是在初始化 Map 时更改了代码以使用语言数组的实际长度。

回答by vartec

You can use constructor Locale(String language), where language is the 2 letter ISO-639-1 code. I think the easiest way to convert ISO-639-2 to ISO-639-1 would be to create HashMap<String,String>constant.

您可以使用 constructor Locale(String language),其中语言是 2 个字母的 ISO-639-1 代码。我认为将 ISO-639-2 转换为 ISO-639-1 的最简单方法是创建HashMap<String,String>常量。

回答by slim

Some modified code from my project, which has a similar requirement. We have our own historical timezone format so we can't use standard libraries.

我的项目中的一些修改代码,具有类似的要求。我们有自己的历史时区格式,所以我们不能使用标准库。

public class MyProjectTimeZoneFactory  {

   private static Map timeZoneDb;

   /**
   * Set up our timezone id mappings; call this from any constructor
   * or static method that needs it.
   */
   private static void init() {
      if(null == TimeZoneDb) {
         timeZoneDb = new HashMap();   // Maybe a TreeMap would be more appropriate
         timeZoneDb.put("     ","GMT+00");
         timeZoneDb.put("EAD  ","GMT+10");
         timeZoneDb.put("JST  ","GMT+9");
         // etc.
       }
   }

   public static TimeZone getTimeZone(String id) 
                          throws CommandFormatException {
       init();

       TimeZone tz;
       if(timeZoneDb.containsKey(id)) {
           tz = TimeZone.getTimeZone((String)timeZoneDb.get(id));
       } else {
           throw new CommandFormatException("Invalid Timezone value");
       }

       return tz;
   }

 }

You could argue that it would be better to have the map in configuration rather than code - perhaps in a properties file. That may be true - but do remember the Pragmatic Programmers' rule 'Your not going to need it'.

您可能会争辩说,将地图放在配置中而不是代码中会更好 - 也许在属性文件中。这可能是真的 - 但请记住实用程序员的规则“你不需要它”。