java标签文字方向怎么设置,如何在java中找出与语言环境相关的文本方向?

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());

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值