简体   繁体   English

如何为区域设置名称创建本地化字符串?

[英]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. 首先,我意识到您可以通过Locale.getAllLocales()方法获取Java中的所有语言环境。 I'm using Spring MVC. 我正在使用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. 我正在尝试将所有这些Locale的描述性名称(“语言(国家/地区)”)放入我网站上的选择下拉框中。 However, I need these names to be localized strings so that they change depending on your browser's Locale. 但是,我需要将这些名称作为本地化字符串,以便它们根据浏览器的Locale进行更改。 Is there already a quick way to do this using Java's Locale class? 是否已经使用Java的Locale类快速完成此操作? 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? 或者我是否需要在属性文件中使用's'来实际创建每一个并在枚举类中定义每个?

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功能:

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. 不知道我在文档中是如何错过的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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