Java – How do I get a script from the Locale object on Android?

How do I get a script from the Locale object on Android?… here is a solution to the problem.

How do I get a script from the Locale object on Android?

As far as I can see< a href="https://developer.android.com/reference/java/util/Locale.html" rel="noreferrer noopener nofollow"> Android Locale class doesn’t mention anything about scripts, only language, Countries and variants.

While Java SE locale class refers to languages, countries, scripts, variants, and extensions , and of course there is a way getScript() to get the script.

So, my question is: how do I get the script from the Locale object on Android?

Or is there another way to get the script?

I need it to be able to distinguish between Chinese Traditional (zho-hant

) and Chinese Simplified (zho-hans), and for this I need to be able to get scripts from the locale, such as hant or hans for language zho

Script =

ISO 15924 alpha-4 script code. You can find a full list of valid
script codes in the IANA Language Subtag Registry (search for “Type:
script”). The script field is case insensitive, but Locale always
canonicalizes to title case (the first letter is upper case and the
rest of the letters are lower case).
Well-formed script values have the form [a-zA-Z]{4}
Example: “Latn” (Latin), “Cyrl” (Cyrillic)

Solution

This feature is not built into the Android SDK. Instead, I wrote a version that you can use, using <a href=”https://gist.github.com/phil-brown/8056700″ rel=”noreferrer noopener nofollow”>here<< String, String>> contains useful information and then uses simple lookup methods. To use this class in your project, simply call:

String script = LocaleUtilities.getScript(Locale.getDefault());

Gets the script for the default locale.

Related Problems and Solutions