Java Cookbook
Get localized display names
Problem:
You want to retrieve a translated (localized) display name for a language, region, or locale.
Solution:
The Locale class contains methods to retrieve the localized display names for languages and countries. To retrieve a display name use the getDisplayLanguage, getDisplayCountry, and getDisplayName methods.
To get the display name for the language, country or locale using the system locale:
Locale[] locales = Locale.getAvailableLocales();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage() + " - " + locales[x].getDisplayCountry() + " - " + locales[x].getDisplayName());
}
To retrieve a localized version of the display names you can specify a Locale as an argument to the method. This Locale is used to retrieve the appropriate display name.
Locale[] locales = Locale.getAvailableLocales();
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage(locales[x]) + " - " + locales[x].getDisplayCountry(locales[x]) + " - " + locales[x].getDisplayName(locales[x]));
}
To loop through all the locales again, but get a Japanese display name:
Locale[] locales = Locale.getAvailableLocales();
Locale japanese = Locale.JAPANESE;
for(int x = 0; x < locales.length; x++){
System.out.println(locales[x].getDisplayLanguage(japanese) + " - " + locales[x].getDisplayCountry(japanese) + " - " + locales[x].getDisplayName(japanese));
}
If you are testing any of these recipes in Eclipse and the characters are not displaying correctly in your console visit http://i18ncookbook.com/eclipse_settings.
This site is ad supported. I hope you find something among our sponsors worth clicking. ;)
i18n search