If you have the ISO country code `US`, `FR`, how do you get the Locale code (`Locale.US`, `Locale.FRANCE`)?(如果您有 ISO 国家代码 `US`、`FR`,如何获得区域设置代码(`Locale.US`、`Locale.FRANCE`)?)
问题描述
如果你有国家代码US
、FR
(ISO-3166-1 alpha-2国家代码),你如何获得Locale代码(Locale.US
, Locale.FRANCE
) 来做这样的事情:
If you have the country code US
, FR
(ISO-3166-1 alpha-2 country code), how do you get the Locale code (Locale.US
, Locale.FRANCE
) to do something like this:
System.out.println(DecimalFormat.getCurrencyInstance(Locale.US).format(12.34));
System.out.println(DecimalFormat.getCurrencyInstance(Locale.FRANCE).format(12.34));
$12.34
12,34 €
推荐答案
你不能,因为 Locale 用于保存一种语言,而不是一个国家.它可以包含特定国家的语言,以及该国家的特定变体,但它首先是一种语言.语言和国家之间没有一对一的关系.大多数语言在不同的国家/地区使用,许多国家/地区有多种语言.
You can't, because a Locale is used to hold a language, not a country. It can hold a language for a specific country, and for a specific variant in this country, but it's a language first. And there is no one-to-one relationship between a language and a country. Most languages are spoken in various countries, and many countries have several languages.
如果您有一种语言的国家/地区代码,则可以使用 new Locale(code)
.但是有了国家代码,你所能做的就是调用getAvailableLocales
,循环遍历结果,找到一个有你国家代码的.但可能有几个.
If you had the country code for a language, you could use new Locale(code)
. But with a country code, all you can do is call getAvailableLocales
, loop through the results, and find one which has your country code. But there might be several ones.
这篇关于如果您有 ISO 国家代码 `US`、`FR`,如何获得区域设置代码(`Locale.US`、`Locale.FRANCE`)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果您有 ISO 国家代码 `US`、`FR`,如何获得区域设置代码(`Locale.US`、`Locale.FRANCE`)?


- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01