从'fr_FR'类型语言代码转换为ISO 639-2语言代码

ovd*_*srn 3 java iso locale localization

我需要像串在Java中转换fr_FR,en_GB,ja_JP(意思是法语,英语和日语),以他们的ISO 639-2表示:fre/fra,eng,jpn.

你知道符号样式fr_FR是否符合某个标准吗?我没有在这方面找到任何东西.

您知道如何将此符号转换为ISO 639-2(3个字母)语言代码吗?

非常感谢!

更新:我知道方法getISO3Language().而且我也知道我可以通过迭代可用的语言环境来构造类似的字符串fr_FR然后使用ISO 639-2 3字母代码进行映射 - 因此,每当我搜索3字母代码时,我都可以在地图中找到我建造了.问题是我会更好地为我提供直接的解决方案.对不起,我从一开始就没有解释过这个.

Tak*_*aki 5

您可以{language}_{country}java.util.ResourceBundle.getBundle(String,Locale,ClassLoader)的javadoc中看到符号样式,因此使用符号样式不会那么糟糕.另一方面,还应该注意语言标签具有{language}-{country}样式(不是下划线'_'而是连字符' - ').可以在java.util.Locale的javadoc中找到详细描述.

转换{language}_{country}为ISO 639-2(3个字母)代码的简单方法是new Locale(str.substring(0,2)).getISO3Language(),但似乎您正在寻找另一种方式,如下所示:

String locale = "fr_FR";

try
{
    // LanguageAlpha3Code is a Java enum that represents ISO 639-2 codes.
    LanguageAlpha3Code alpha3;

    // LocaleCode.getByCode(String) [static method] accepts a string
    // whose format is {language}, {language}_{country}, or
    // {language}-{country} where {language} is IS0 639-1 (2-letter)
    // and {country} is ISO 3166-1 alpha2 code (2-letter) and returns
    // a LocaleCode enum. LocaleCode.getLanguage() [instance method]
    // returns a LanguageCode enum. Finally, LanguageCode.getAlpha3()
    // returns a LanguageAlpha3Code enum.
    alpha3 = LocaleCode.getByCode(locale).getLanguage().getAlpha3();

    // French has two ISO 639-2 codes. One is "terminology" code
    // (ISO 639-2/T) and the other is "bibliographic" code
    // (ISO 639-2/B). 2 lines below prints "fra" for ISO 639-2/T
    // and "fre" for ISO 639-2/B.
    System.out.println("ISO 639-2/T: " + alpha3.getAlpha3T());
    System.out.println("ISO 639-2/B: " + alpha3.getAlpha3B());
}
catch (NullPointerException e)
{
    System.out.println("Unknown locale: " + locale);
}
Run Code Online (Sandbox Code Playgroud)

上面的示例可以使用nv-i18n国际化包运行.如果您使用的是Maven,请尝试将以下依赖项添加到您的pom.xml中,

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

或直接从Maven Central Repository下载nv-i18n的jar .

nv-i18n源代码和javadoc托管在GitHub上.

资料来源:https://github.com/TakahikoKawasaki/nv-i18n
Javadoc:http://takahikokawasaki.github.com/nv-i18n/