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) 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) 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).
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.
解决方案
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"!
Nevertheless the ComponentOrientation-method is obviously not up to date, since it is missing the following languages according to Wikipedia:
yi (yiddish plus old form ji), dv (maldivian), ps (pashto) and ha (hausa)
So I have refined the approach of ComponentOrientation and added these missing languages. The final code looks like this:
private static final Set RTL;
static {
Set lang = new HashSet();
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());
}