如何查找多种语言的国家/地区代码 [英] How to find a country code in multiple languages
问题描述
我的问题是我得到了使用不同语言的国家/地区列表.全部在文本文件中.例如:
My problem is that I got a list of countries in different languages. All in text file. E.g:
pl_PL Japonia
en_GB Spain
en_EN Portugal
我正在寻找一种查找国家/地区代码的方法,因为它知道不同的语言名称,所以我可以用其他语言打印出来.例如:
I am seeking a way to find the code of the country, knowing name in different languages, so i could print it in another languages. E.g:
pl_PL Japonia -- translate to english --> en_EN Japan
en_EN Portugal -- translate to polish --> pl_PL Portugalia
我当时正在考虑为每种语言制作一个哈希图,在其中放置所有可用的国家和代码,但我不知道该如何制作.我已经制作了一张这样的地图,但是我不知道如何制作多种语言.
I was thinking to make a hashmap for each language where I would put all available countries and codes, but I don't have idea how to make it. I made already one map like this, but I don't know how to make it for multiple languages.
Locale[] allLocs = Locale.getAvailableLocales();
Map map = new HashMap();
String country = null;
for (int j=0; j<allLocs.length; j++) {
Locale.setDefault(new Locale(langArray[0]));
String countryCode = allLocs[j].getCountry();
if (countryCode.equals("")) continue;
kraj = allLocs[j].getDisplayCountry();
map.put(country , allLocs[j]);
}
推荐答案
也许您尝试的Locale构造出错.您需要从输入文件中检索以下内容:
Maybe the Locale construction went wrong in your attempts. You need to retrieve the following from the input file:
final Locale locale1 = new Locale("pl", "PL");
final String country1 = "Japonia";
然后指定目标语言环境:
Then given the target locale:
final Locale locale2 = new Locale("en", "US");
一个人可以像您一样找到国家.在Java 8中:
One can find the country as you did. In java 8:
final Locale[] locales = Locale.getAvailableLocales();
Optional<String> country2 = Stream.of(locales)
.filter((loc) -> country1.equals(loc.getDisplayCountry(locale1)))
.map((cloc) -> cloc.getDisplayCountry(locale2))
.filter((s) -> !s.isEmpty())
.findAny();
System.out.println(country2.orElse("not found"));
- 这会以第一语言搜索显示国家/地区的每个位置.
- 然后将相应国家/地区的cloc用于第二语言的显示国家/地区.
- 非空.
这篇关于如何查找多种语言的国家/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!