I searched for a solution, but I have not found any.
I have this kind of information:
String locale = "en_GB";
String country = "Japonia"; //It means Japan in polish
I need to translate the country name "Japonia" into language indicated in string locale, so "Japan". Is there any way to do it?
Yes, you can use a translation API. Like Microsoft's or Google's.
(Answer based on comment by Elliott Frisch)
The Java Runtime Library doesn't have a translation API, but the
Locale
class can be used to get the name of any country in any language, as long as you know the ISO 3166 alpha-2 country code, and the ISO 639 alpha-2 or alpha-3 language code.Example for country
Japan
:Output
Assuming you know both the input language and the desired output language, an alternative approach - iterate the
Locale
(s) on the system usingLocale.getAvailableLocales()
, test if the country name matches from the desired inLocale
- if so display it in the desired outputLocale
usinggetDisplayCountry(Locale)
Outputs
And if you modify the
outLocale
likeyou get