Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the English Country names to German language using Locale

Tags:

java

locale

I have this requirements that I need to convert the equivalent country codes to German language country name. I am using the Locale builder for my codes.

Here are my codes:

The first one I did is this:

Locale locale = new Locale("GERMANY", "AT");

Generated output: German (Austria)

The second one:

Locale aLocale = new Locale.Builder().setLanguage("de").setRegion("AT").build();

Generated output: German (Austria)

What I need is to Convert the countryCode "AT" which is Austria to German Language, and it should generate Österreich.

like image 664
pinkpanther Avatar asked Jul 01 '15 04:07

pinkpanther


People also ask

What is the use of locale?

A Locale is the mechanism for identifying the kind of object ( NumberFormat ) that you would like to get. The locale is just a mechanism for identifying objects, not a container for the objects themselves.

What is the German version of my name?

Another way to introduce yourself is to use the German equivalent of “my name is __”: Mein Name ist __”.

What is the default locale?

The default locale determines the format. The number may be a primitive (such as int or double ) or a wrapper object (such as Integer or Double ). The language is english and the country is Great Britain. Locales specify both language and country.


1 Answers

The method getDisplayCountry() of Locale can take a Locale argument for which language to try to display it in. For example:

Locale locale = new Locale("de", "AT");
System.out.println(locale.getDisplayCountry(locale));

prints for me

Österreich

If you want the whole locale's name, just use getDisplayName() with a parameter, instead of getDisplayCountry().

This isn't guaranteed to work for every combination of locales. According to the docs:

If the name returned cannot be localized according to [the provided Locale parameter] (say, we don't have a Japanese name for Croatia), this function falls back on the English name, and finally on the ISO code as a last-resort value.

Also note that the language code for German is "de", not "GERMANY".

like image 121
Dan Getz Avatar answered Oct 17 '22 05:10

Dan Getz