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
.
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.
Another way to introduce yourself is to use the German equivalent of “my name is __”: Mein Name ist __”.
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.
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"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With