Setting Arabic numbering system locale doesn't

2019-01-20 09:24发布

问题:

I read this article: JDK 8 and JRE 8 Supported Locales, it stated that:

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 ║ ╚═════════════════════╩══════════════════════╩══════════════════╝

Now, to demonstrate this, I wrote the following codes:

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));
    }
}

I was expecting the output to be something like:

٠
١٢٣

However, the output is as follows:

0
123

The main purpose of this is to make JavaFX GUI showing Arabic numbers instead of English numbers as it uses the default locale (and I can set it with Locale.setDefault(...)).

So my question is, how to use the numbering system in the locale to show localized numbers in Java? Then, is it possible to apply it on JavaFX?

回答1:

YES, I did it! After reading Locale's JavaDoc carefully, I was able to produce the required locale:

Locale arabicLocale = new Locale.Builder().setLanguageTag("ar-SA-u-nu-arab").build();

which is equivalent to:

Locale arabicLocale = new Locale.Builder().setLanguage("ar").setRegion("SA")
                   .setExtension(Locale.UNICODE_LOCALE_EXTENSION, "nu-arab").build();

Note that I am using something called (Unicode locale/language extension):

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).

The extension key for numbers is (nu) and the value I used is (arab).


You can see a list of all extension keys here.