简体   繁体   中英

How to Make Localized Strings for Locale Names?

First of all, I realize that you can get all of the locales in Java via the Locale.getAllLocales() method. I'm using Spring MVC.

I'm trying to put all of these Locale's descriptive names ("Language (Country)") into a select drop-down box on my website. However, I need these names to be localized strings so that they change depending on your browser's Locale. Is there already a quick way to do this using Java's Locale class? Or will I need to actually create every single one myself using 's in a property file and define each one in an enum class?

Or maybe an there's an easier way to manually create them?

Thanks!

You can try doing the following

System.out.println(Locale.GERMANY.getDisplayCountry(Locale.GERMANY));

prints

Deutschland

I guess this will work for built-in locales, but probably not for custom ones.

You can use the Locale functions:

Locale.getDisplayCountry(Locale locale)

Locale.getDisplayLanguage(Locale locale)

And others. It will Localize automatically depending on the Locale you input.

No idea how I missed that in the documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM