您可以在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