如何在java中找出与语言环境相关的文本方向? [英] How to find out locale-dependent text orientation in java?

查看:161
本文介绍了如何在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)这需要了解要解析的具体char,由于可能的填充,这并不总是很容易chars。

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。但是看看维基百科-ISO-639 ,似乎有更多的语言有权利从左到右,例如ISO-639代码'他'(希伯来语)。

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).

所以现在我的问题是,你知道更好的方法吗?目前我倾向于使用维基百科列表,即用更好的语言列表来优化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'只是'他'的旧形式。表达式新的Locale(他)。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方法显然不是最新的,因为它根据维基百科缺少以下语言:

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(普什图语)和ha(hausa)

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

这篇关于如何在java中找出与语言环境相关的文本方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆