问题描述
我搜索了一个解决方案,但没有找到任何解决方法.
I searched for a solution, but I have not found any.
我有这种信息:
String locale = "en_GB";
String country = "Japonia"; //It means Japan in polish
我需要将国家名称"Japonia"翻译成字符串语言环境中指示的语言,即"Japan".有什么办法吗?
I need to translate the country name "Japonia" into language indicated in string locale, so "Japan". Is there any way to do it?
推荐答案
假定您同时知道输入语言和所需的输出语言,另一种方法-使用 Locale.getAvailableLocales()
,测试国家/地区名称与所需的 in Locale
中的名称匹配-如果是这样,则使用 getDisplayCountry(Locale)
Assuming you know both the input language and the desired output language, an alternative approach - iterate the Locale
(s) on the system using Locale.getAvailableLocales()
, test if the country name matches from the desired in Locale
- if so display it in the desired output Locale
using getDisplayCountry(Locale)
String country = "Japonia";
Locale outLocale = Locale.forLanguageTag("en_GB");
Locale inLocale = Locale.forLanguageTag("pl-PL");
for (Locale l : Locale.getAvailableLocales()) {
if (l.getDisplayCountry(inLocale).equals(country)) {
System.out.println(l.getDisplayCountry(outLocale));
break;
}
}
输出
Japan
如果您将outLocale
修改为
Locale outLocale = Locale.forLanguageTag("es-SP");
你得到
Japón
这篇关于将国家/地区名称翻译成其他语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!