如何在java中找出依赖于语言环境的文本方向?
在编写格式和解析引擎时,我想知道 - 给定 j.u.Locale,如果一般文本方向是从右到左(字母字符).我看到的是:
While I am writing a format and parse engine, I would like to find out - given a j.u.Locale, if the general text orientation is right-to-left (letter chars). What I have seen is:
1.) Character#getDirectionality(char) 这需要了解要解析的具体字符,由于可能存在填充字符,这并不总是容易的.
1.) Character#getDirectionality(char) This requires the knowledge of the concrete char to be parsed which is not always easy due to possible fill chars.
2.) java.awt.ComponentOrientation#getOrientation(Locale) 这个方法对我来说有两个问题.首先,我不想依赖 awt 或其他 gui 库(预计 Java 以后可能会模块化).第二个也是更重要的:这个方法在 OpenJDK 和 Android 中的实现只是使用了一种非常简单的方法,即:如果语言是 (ar, fa, iw, ur) 之一,那么方法就是 RIGHT_TO_LEFT.但是看看 Wikipedia-ISO-639 似乎有更多的语言有权利-to-left-orientation,例如 ISO-639 代码he"(希伯来语).
2.) java.awt.ComponentOrientation#getOrientation(Locale) This method has two problems for me. First I would not like to be dependent on awt or other gui libraries (anticipating a possible later modularization of Java). Second and more important: The implementation of this method in OpenJDK and Android just use a very simple approach, namely: If the language is one of (ar, fa, iw, ur) then the method says RIGHT_TO_LEFT. But looking at Wikipedia-ISO-639 it appears that there are more languages which have a right-to-left-orientation, for example the ISO-639-code 'he' (hebrew).
所以我现在的问题是,你知道更好的方法吗?目前我倾向于只使用 Wikipedia 列表,即使用更好的语言列表来改进 java.awt.ComponentOrientation 方法.
So my question now is, do you know a better approach? At the moment I tend just to use the Wikipedia list, that is, to refine the java.awt.ComponentOrientation approach with a better language list.
推荐答案
经过进一步研究,我发现语言代码'iw'只是'he'的旧形式.而表达式 new Locale("he").getLanguage()
将提供结果iw"!
After some further research I have found out that the language code 'iw' is just the old form for 'he'. And the expression new Locale("he").getLanguage()
would provide the result "iw"!
尽管如此,ComponentOrientation 方法显然不是最新的,因为根据 Wikipedia,它缺少以下语言:
Nevertheless the ComponentOrientation-method is obviously not up to date, since it is missing the following languages according to Wikipedia:
yi(意第绪语加旧形式 ji)、dv(马尔代夫语)、ps(普什图语)和 ha(豪萨语)
yi (yiddish plus old form ji), dv (maldivian), ps (pashto) and ha (hausa)
所以我改进了 ComponentOrientation 的方法并添加了这些缺失的语言.最终代码如下所示:
So I have refined the approach of ComponentOrientation and added these missing languages. The final code looks like this:
private static final Set<String> RTL;
static {
Set<String> lang = new HashSet<String>();
lang.add("ar");
lang.add("dv");
lang.add("fa");
lang.add("ha");
lang.add("he");
lang.add("iw");
lang.add("ji");
lang.add("ps");
lang.add("sd");
lang.add("ug");
lang.add("ur");
lang.add("yi");
RTL = Collections.unmodifiableSet(lang);
}
public static boolean isTextRTL(Locale locale) {
return RTL.contains(locale.getLanguage());
}
相关文章