Reputation: 117579
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?
Upvotes: 11
Views: 4665
Reputation: 2218
While the Locale
in the accepted answer works well with NumberFormat
; DateTimeFormatter
(and prolly all other java.time
formatters) fail to use it properly, at least before the introduction of localizedBy()
in JDK 10.
So for folks who can't use JDK 10+ and are looking to get a fully localized string representation of the date, one alternative is to do (Kotlin):
// code in the oh-so-sweet-and-sugary Kotlin
val locale = Locale.Builder()
.setLanguageTag("ar-SA-u-nu-arab")
.build()
val numFormat = NumberFormat.getNumberInstance(locale)
val formatter = DateTimeFormatter.ofPattern("yyyy MM dd", locale)
val str = formatter.format(OffsetDateTime.now())
// String.replace(Regex, noninline (MatchResult) -> CharSequence)
// this method is gold
str.replace(Regex("\\d"), {numFormat.format(it.value.toInt())}))
println(str)
I feel like this could be done in a better/more elegant way. If anyone has it, please do share.
Upvotes: 1
Reputation: 117579
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.
Upvotes: 20