How to get the script from a Locale object on Android?

夙愿已清 提交于 2019-12-01 06:02:22

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/19153384/how-to-get-the-script-from-a-locale-object-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!