如何从Android上的Locale对象获取脚本? [英] How to get the script from a Locale object on Android?
问题描述
从我看到的 Android Locale类未提及关于脚本的任何内容,只有语言,国家和变体。
From what I see the Android Locale class doesn't mention anything about the script, only the language, country and variant.
而 Java SE Locale类提到语言,国家/地区,脚本,变体和扩展,当然还有一个方法< a href =http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#getScript%28%29 =nofollow> getScript()到得到脚本。
Whereas the Java SE Locale class mentions language, country, script, variant and extensions, and of course has a method getScript() to get the script.
所以,我的问题是:如何从Android上的Locale对象获取脚本?
还是有另一种获取脚本的方式吗?
or is there another way to get the script?
我需要这个能够区分繁体中文(zho-Hant)和简体中文(zho-Hans),因为我需要能够从Locale获取脚本,例如汉语或汉语语言zho
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个脚本代码。您可以在IANA语言子标签注册表中找到有效
脚本代码的完整列表(搜索类型:
脚本)。脚本字段不区分大小写,但Locale总是
标准化为标题大小写(第一个字母是大写字母,而
其余字母是小写字母)。
格式良好的脚本值的格式为[a-zA-Z] {4}
示例:Latn(拉丁语),Cyrl(西里尔语)
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)
推荐答案
此功能未内置于 Android SDK 中。相反,我写了一个你可以使用的版本,这里。基本上,它采用上面答案中的表并将其移植到包含有用信息的 Map< String,Map< String,String>>
,然后是一个简单的查找方法用来。要在项目中使用此类,只需调用:
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.
这篇关于如何从Android上的Locale对象获取脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!