您可以在java.util.ResourceBundle.getBundle(String, Locale, ClassLoader)的javadoc中看到符号样式{language}_{country}
,因此使用符号样式不会太差。另一方面,还应该注意的是,语言标记具有{language}-{country}
样式(不是下划线'_',而是连字符' - ')。详细说明可在java.util.Locale的javadoc中找到。
一个简单的方法来{language}_{country}
转换为ISO 639-2(三字母)代码为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 Central Repository直接下载NV-国际化的罐子。
nv-i18n源代码和javadoc托管在GitHub上。
来源:https://github.com/TakahikoKawasaki/nv-i18n
的Javadoc:http://takahikokawasaki.github.com/nv-i18n/
为了加以说明,en_GB表示在英国使用的英语,fr_FR表示在法国使用的法语。 en_US或en_EN(我永远不会记得哪一个,甚至可能因供应商而异)意味着在美国使用的英语,fr_CA意味着在加拿大使用的法语。 – Rich 2011-03-01 15:14:21
@Rich:用2K +代表你可以修改我的答案,如果你想编辑它:) – Gugussee 2011-03-01 15:21:02
谢谢你们!事实上,该符号是由连接2个符合2个标准的字符串组成的。这个连接肯定不是它自己的标准。 – ovdsrn 2011-03-01 15:24:08