将语言名称转换为ISO 639语言代码 [英] Converting language names to ISO 639 language codes

查看:165
本文介绍了将语言名称转换为ISO 639语言代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将匈牙利语,英语等语言名称转换为ISO 639代码。 ISO 639-6将是最好的,但ISO 639-2已经足够好了。实现这一目标的最佳方法是什么?

I need to convert language names like 'Hungarian', 'English' to ISO 639 codes. ISO 639-6 would be the best but ISO 639-2 is good enough. What's the best way to achieve this?

我应该将英语转换为语言环境并使用getLanguage()获取语言?如果这是唯一的方法如何将像'英语'这样的字符串转换为java语言环境?

I should convert the English to locale and get the language with getLanguage()? If thats the only way how can I convert a string like 'English' to a java locale?

我的目标是使用ISO 639代码存储书籍语言信息。

My goal is to store book language info using the ISO 639 codes.

推荐答案

您可以通过将语言名称的正则表达式传递给 LanguageAlpha3Code.findByName(String)(in nv-i18n 图书馆。)

You can get a list of ISO 639-2 codes by passing a regular expression of language names to LanguageAlpha3Code.findByName(String) (in nv-i18n library).

以下示例代码是一个命令行工具,可将给定的语言名称转换为相应的ISO 639-2代码。

The following example code is a command-line tool that converts given language names into corresponding ISO 639-2 codes.

import java.util.List;
import com.neovisionaries.i18n.LanguageAlpha3Code;

public class To639_2
{
    public static void main(String[] args)
    {
        // For each language name given on the command line.
        for (String languageName : args)
        {
            // Get a list of ISO 639-2 codes (alpha-3 codes)
            // whose language name matches the given pattern.
            List<LanguageAlpha3Code> list
                = LanguageAlpha3Code.findByName(languageName);

            // Print the language and the ISO 639-2 code.
            System.out.format("%s => %s\n", languageName,
                (list.size() != 0) ? list.get(0) : "");
        }
    }
}

执行样本:


$ java -cp nv-i18n-1.14.jar:. To639_2 Hungarian English
Hungarian => hun
English => eng

这篇关于将语言名称转换为ISO 639语言代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆