Java – use locale to change the English country name to German

I have this requirement. I need to convert the equivalent country / region code to German country / region name I'm using the locale builder to get the code

This is my code:

The first thing I did was:

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

Generate output: German (Austria)

the second:

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

Generate output: German (Austria)

What I need is to convert the Austrian country code "at" to German, which should generate Ö sterreich

Solution

The locale method getdisplaycountry () can take the locale parameter to try to display it in which language For example:

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

Print it to me

Österreich

If you want the name of the entire locale, just use getdisplayname () with parameters instead of getdisplaycountry ()

This is not guaranteed to apply to every locale combination According to the document:

Please also note that the language code of German is "de", not "Germany"

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>