从“fr_FR”转换将语言代码键入 ISO 639-2 语言代码

发布于 2024-10-19 12:07:13 字数 473 浏览 9 评论 0 原文

我需要在 Java 中将 fr_FRen_GBja_JP(表示法语、英语和日语)等字符串转换为 ISO 639 -2 种表示形式:fre/fraengjpn

您知道表示法 fr_FR 是否符合某个标准吗?我还没有找到这方面的任何东西。

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

多谢!

更新:我知道 getISO3Language() 方法。我还知道,我可以通过迭代可用的语言环境来构建像 fr_FR 这样的字符串,然后使用 ISO 639-2 3 字母代码进行映射 - 因此,每当我搜索 3-我可以在我构建的地图中找到字母代码。问题是,直接解决方案更适合我。抱歉我没有从一开始就解释这一点。

I need to convert in Java from strings like fr_FR, en_GB, ja_JP (meaning the French, English, and Japanese language) to their ISO 639-2 representations: fre/fra, eng, jpn.

Do you know if the notation style fr_FR complies to a certain standard? I haven't found anything in this regard.

Do you know how can I make the conversion from this notation to ISO 639-2 (3-letter) language codes?

Thanks a lot!

Update: I know the method getISO3Language(). And I also know that I could construct, by iterating the available locales, strings like fr_FR and then make a mapping with the ISO 639-2 3-letter code - thus, whenever I search for a 3-letter code I can find in the map I constructed. The thing is that I would fit me much better a direct solution. Sorry that I didn't explained this from the beginning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〃安静 2024-10-26 12:07:13

您可以在 {language}_{country} /ResourceBundle.html#getBundle%28java.lang.String,%20java.util.Locale,%20java.lang.ClassLoader%29" rel="noreferrer">java.util.ResourceBundle.getBundle(String, Locale, ClassLoader)< /a>,所以使用符号风格不会那么糟糕。另一方面,还应该注意语言标签具有{语言}-{国家/地区}样式(不是下划线'_'而是连字符'-')。详细说明可以在 java.util 的 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);
}

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

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.1</version>
</dependency>

或从 Maven 中央存储库直接。

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

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

You can see the notation style {language}_{country} in the javadoc of java.util.ResourceBundle.getBundle(String, Locale, ClassLoader), so it won't be so bad to use the notation style. On the other hand, it also should be noted that language tags have {language}-{country} style (not underscore '_' but hyphen '-'). Detailed description can be found in the javadoc of java.util.Locale.

A simple way to convert {language}_{country} to ISO 639-2 (3-letter) code is new Locale(str.substring(0,2)).getISO3Language(), but it seems you are looking for another way like the following:

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

The example above can be run with nv-i18n internationalization package. If you are using Maven, try to add the dependency below to your pom.xml,

<dependency>
    <groupId>com.neovisionaries</groupId>
    <artifactId>nv-i18n</artifactId>
    <version>1.1</version>
</dependency>

or download nv-i18n's jar from Maven Central Repository directly.

nv-i18n source code and javadoc are hosted on GitHub.

Source: https://github.com/TakahikoKawasaki/nv-i18n
Javadoc: http://takahikokawasaki.github.com/nv-i18n/

痴情换悲伤 2024-10-26 12:07:13

评论太长了,所以...

你知道符号样式吗
fr_FR 符合某个标准吗?
我在这没有找到任何东西
尊重。

前两个字母是语言代码:

语言参数是有效的 ISO
语言代码。这些代码是
小写、两个字母的代码为
由 ISO-639 定义。

最后两个字母是国家代码:

国家参数是有效的 ISO
国家/地区代码。这些代码是
大写、两个字母的代码为
由 ISO-3166 定义。

现在您已经在这方面找到了一些东西,可以解释该表示法对应的标准。

This too long for a comment so...

Do you know if the notation style
fr_FR complies to a certain standard?
I haven't found anything in this
regard.

The first two letters are the language code:

The language argument is a valid ISO
Language Code. These codes are the
lower-case, two-letter codes as
defined by ISO-639.

The last two letters are the country code:

The country argument is a valid ISO
Country Code. These codes are the
upper-case, two-letter codes as
defined by ISO-3166.

So now you have found something in this regard, that explains to which standards this notation corresponds.

把时间冻结 2024-10-26 12:07:13

创建一个 Locale 对象,然后使用 getISO3Language()。
http://download.oracle.com /javase/1.5.0/docs/api/java/util/Locale.html#getISO3Language()

String lang="fr", country="FR", convertedLang;
Locale l = new Locale(lang, country);
convertedLang= l.getISO3Language(); // should be what you're after

Create a Locale object then use getISO3Language().
http://download.oracle.com/javase/1.5.0/docs/api/java/util/Locale.html#getISO3Language()

String lang="fr", country="FR", convertedLang;
Locale l = new Locale(lang, country);
convertedLang= l.getISO3Language(); // should be what you're after
迷途知返 2024-10-26 12:07:13

java.util.Locale 的文档中对此进行了全部讨论。两个字母的代码也来自 ISO 标准,并且 Locale 有一个方法 getISO3Language() 可以完成您想要的操作。

This is all discussed in the documentation of java.util.Locale. The two-letter codes are also from the ISO standards, and Locale has a method getISO3Language() that does what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文