有没有办法在Java中检测RTL语言?

有没有办法在Java中检测RTL语言?,java,internationalization,right-to-left,Java,Internationalization,Right To Left,我需要能够检测我的用户正在查看的当前语言是否是类似阿拉伯语的RTL(从右到左)语言 目前,我只是根据系统属性user.language中的语言代码来检测这一点,但肯定有更好的方法 ComponentOrientation.getOrientation(new Locale(System.getProperty("user.language"))).isLeftToRight(); 我觉得依赖AWT类有点脏,因为AWT类已经过时,我正在处理BCP-47语言代码,所以我最终从Google闭

我需要能够检测我的用户正在查看的当前语言是否是类似阿拉伯语的RTL(从右到左)语言

目前,我只是根据系统属性user.language中的语言代码来检测这一点,但肯定有更好的方法

ComponentOrientation.getOrientation(new Locale(System.getProperty("user.language"))).isLeftToRight();  

我觉得依赖AWT类有点脏,因为AWT类已经过时,我正在处理BCP-47语言代码,所以我最终从Google闭包模板复制了这段代码:

/**
 * A regular expression for matching right-to-left language codes.
 * See {@link #isRtlLanguage} for the design.
 */
private static final Pattern RtlLocalesRe = Pattern.compile(
    "^(ar|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))" +
    "(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)");
/**
 * Check if a BCP 47 / III language code indicates an RTL language, i.e. either:
 * - a language code explicitly specifying one of the right-to-left scripts,
 *   e.g. "az-Arab", or<p>
 * - a language code specifying one of the languages normally written in a
 *   right-to-left script, e.g. "fa" (Farsi), except ones explicitly specifying
 *   Latin or Cyrillic script (which are the usual LTR alternatives).<p>
 * The list of right-to-left scripts appears in the 100-199 range in
 * http://www.unicode.org/iso15924/iso15924-num.html, of which Arabic and
 * Hebrew are by far the most widely used. We also recognize Thaana, N'Ko, and
 * Tifinagh, which also have significant modern usage. The rest (Syriac,
 * Samaritan, Mandaic, etc.) seem to have extremely limited or no modern usage
 * and are not recognized.
 * The languages usually written in a right-to-left script are taken as those
 * with Suppress-Script: Hebr|Arab|Thaa|Nkoo|Tfng  in
 * http://www.iana.org/assignments/language-subtag-registry,
 * as well as Sindhi (sd) and Uyghur (ug).
 * The presence of other subtags of the language code, e.g. regions like EG
 * (Egypt), is ignored.
 */
public static boolean isRtlLanguage(String languageString) {
  return languageString != null &&
      RtlLocalesRe.matcher(languageString).find();
}
/**
*用于从右向左匹配语言代码的正则表达式。
*有关设计,请参见{@link#isrtlanguage}。
*/
私有静态最终模式RtlLocalesRe=Pattern.compile(
“^(ar | dv | he | iw | fa | nqo | ps | sd | ug | ur | yi |.[-|](阿拉伯|希伯尔|塔阿|恩库| Tfng))”+
“(?!.[-|](Latn | Cyrl)($|-|-|])($|-|])”;
/**
*检查BCP 47/III语言代码是否表示RTL语言,即:
*-明确指定从右向左脚本之一的语言代码,
*例如“az阿拉伯”,或
*-一种语言代码,用于指定通常用
*从右到左的脚本,例如“fa”(波斯语),明确指定
*拉丁或西里尔文字(这是常用的LTR替代文字)。
*从右到左的脚本列表显示在中的100-199范围内
* http://www.unicode.org/iso15924/iso15924-num.html,其中阿拉伯文及
*希伯来语是迄今为止使用最广泛的语言。我们也承认塔纳、恩科和
*TIFINAH,也有重要的现代用途。其余的(叙利亚语,
*撒玛利亚人、曼代克语等)的现代用法似乎非常有限或没有
*而且不被承认。
*通常使用从右向左的脚本编写的语言被视为
*带有抑制脚本:希伯尔|阿拉伯|塔阿|恩库| Tfng in
* http://www.iana.org/assignments/language-subtag-registry,
*以及信德语(sd)和维吾尔语(ug)。
*语言代码的其他子标签的存在,例如区域,如EG
*(埃及)被忽视。
*/
公共静态布尔ISRTL语言(字符串语言字符串){
返回语言字符串!=null&&
RtlLocalesRe.matcher(languageString.find();
}
请参见

TextUtils.getLayoutDirectionFromLocale(locale)

添加一个布尔资源
是\u rtl
(比方说),定义为

  • res/values/values.xml中
    
    <bool name="is_rtl">false</bool>
    
    <bool name="is_rtl">true</bool>
    

然后,在java中,您可以获得此bool资源,并立即知道用户是否处于RTL区域设置中。

AWT类有什么问题?这并不是说他们已经被抛弃了,或者其他类似的事情。例如,目前还不清楚哪些AWT类将继续在无头环境中工作。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@Glains你在说什么?从
java.awt.*