[英]Converting language names to ISO 639 language codes
I need to convert language names like 'Hungarian', 'English' to ISO 639 codes. 我需要将“匈牙利语”,“英语”等语言名称转换为ISO 639代码。 ISO 639-6 would be the best but ISO 639-2 is good enough. ISO 639-6将是最好的,但ISO 639-2已经足够好了。 What's the best way to achieve this? 实现这一目标的最佳方法是什么?
I should convert the English to locale and get the language with getLanguage()? 我应该将英语转换为语言环境并使用getLanguage()获取语言? If thats the only way how can I convert a string like 'English' to a java locale? 如果这是唯一的方法我如何将像'英语'这样的字符串转换为java语言环境?
My goal is to store book language info using the ISO 639 codes. 我的目标是使用ISO 639代码存储书籍语言信息。
for (Locale locale : Locale.getAvailableLocales()) {
System.out.println("" + locale
+ "; display: " + locale.getDisplayLanguage()
+ "; name: " + locale.getDisplayName()
+ "; lang: " + locale.getLanguage()
+ "; iso3: " + locale.getISO3Language());
}
This will find some 150 locales, where ISO3 is the three letter variant, as opposed to the older two letter getLanguage. 这将找到大约150个语言环境,其中ISO3是三个字母的变体,而不是旧的两个字母getLanguage。
The display language is the bare language name, whereas the display name is embellished with the country "German (Austria)." 显示语言是裸语言名称,而显示名称则用国家“德语(奥地利)”进行装饰。
So 所以
public String toISO3(String name) {
for (Locale locale : Locale.getAvailableLocales()) {
if (name.equals(locale.getDisplayLanguage()) {
return locale.getISO3Language();
}
}
throw new IllegalArgumentException("No language found: " + name);
}
For the display methods there is an optional Locale parameter, to explicitly set to Locale.ENGLISH. 对于显示方法,有一个可选的Locale参数,用于显式设置为Locale.ENGLISH。
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). 您可以通过将语言名称的正则表达式传递给LanguageAlpha3Code.findByName(String) (在nv-i18n库中)来获取ISO 639-2代码列表。
The following example code is a command-line tool that converts given language names into corresponding ISO 639-2 codes. 以下示例代码是一个命令行工具,可将给定的语言名称转换为相应的ISO 639-2代码。
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) : "");
}
}
}
A sample execution: 示例执行:
$ java -cp nv-i18n-1.14.jar:. To639_2 Hungarian English Hungarian => hun English => eng
/**
* This method is to get the language code from given language name
* as locale can't be instantiate from a language name.
*
* You can specify which language you are at : Locale loc=new Locale("en") use whatever your language is
*
* @param lng -> given language name eg.: English
* @return -> will return "eng"
*
* Wilson M Penha Jr.
*/
private String getLanguageCode(String lng){
Locale loc = new Locale("en");
String[] name = loc.getISOLanguages(); // list of language codes
for (int i = 0; i < name.length; i++) {
Locale locale = new Locale(name[i],"US");
// get the language name in english for comparison
String langLocal = locale.getDisplayLanguage(loc).toLowerCase();
if (lng.equals(langLocal)){
return locale.getISO3Language();
}
}
return "unknown";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.