我读了这篇文章:JDK 8 and JRE 8 Supported Locales ,它表示:
Numbering systems can be specified by a language tag with a numbering system ID
╔═════════════════════╦══════════════════════╦══════════════════╗ ║ Numbering System ID ║ Numbering System ║ Digit Zero Value ║ ╠═════════════════════╬══════════════════════╬══════════════════╣ ║ arab ║ Arabic-Indic Digits ║ \u0660 ║ ╚═════════════════════╩══════════════════════╩══════════════════╝
现在,为了证明这一点,我编写了以下代码:
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;
public class Main
{
public static void main(String[] args)
{
Locale locale = new Locale("ar", "sa", "arab");
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
System.out.println(dfs.getZeroDigit());
System.out.println(numberFormat.format(123));
}
}
我期望输出是这样的:
٠
١٢٣
但是,输出如下:
0
123
这样做的主要目的是使 JavaFX GUI 显示阿拉伯数字而不是英语数字,因为它使用默认语言环境(我可以使用 Locale.setDefault(...)
进行设置)。
所以我的问题是,如何使用语言环境中的编号系统在 Java 中显示本地化数字?那么,是否可以将它应用到JavaFX上呢?
最佳答案
是的,我做到了!看完Locale
's JavaDoc仔细地,我能够生成所需的语言环境:
Locale arabicLocale = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build();
相当于:
Locale arabicLocale = new Locale.Builder().setLanguage("ar").setRegion("SA")
.setExtension(Locale.UNICODE_LOCALE_EXTENSION, "nu-arab").build();
请注意,我使用的是(Unicode 区域设置/语言扩展):
UTS#35, "Unicode Locale Data Markup Language" defines optional attributes and keywords to override or refine the default behavior associated with a locale. A keyword is represented by a pair of key and type.
The keywords are mapped to a BCP 47 extension value using the extension key 'u' (UNICODE_LOCALE_EXTENSION).
数字的扩展键是 (nu
),我使用的值是 (arab
)。
您可以看到所有扩展键的列表here .
关于java - 设置阿拉伯数字系统区域设置不显示阿拉伯数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29154887/