Domanda

From what I see the Android Locale class doesn't mention anything about the script, only the language, country and variant.

Whereas the Java SE Locale class mentions language, country, script, variant and extensions, and of course has a method getScript() to get the script.

So, my question is: How to get the script from a Locale object on Android?

or is there another way to get the script?

I need this to be able to differentiate between Chinese Traditional (zho-Hant) and Chinese Simplified (zho-Hans), for that I need to be able to get the script from the Locale, e.g. Hant or Hans for the 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)

È stato utile?

Soluzione

This feature is not built into the Android SDK. Instead, I wrote a version you can use, available here. Basically, it takes the table in the above answer and ports it to a Map<String, Map<String, String>> containing the useful information, then a simple lookup method is used. To use this class in your project, just call:

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

to get the script for the default locale.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top