2010-11-16 25 views
11

Tôi cần có khả năng phát hiện ngôn ngữ hiện tại mà người dùng của tôi đang xem là ngôn ngữ RTL (Phải sang trái) như tiếng Ả Rập hay không.Có cách nào để phát hiện ngôn ngữ RTL trong Java không?

Hiện tại tôi chỉ phát hiện điều này dựa trên mã ngôn ngữ từ user.language thuộc tính hệ thống, nhưng phải có cách tốt hơn.

Trả lời

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

tôi cảm thấy một chút bẩn dựa vào lớp AWT là khá ngày và tôi đã đối phó với BCP-47 mã ngôn ngữ, vì vậy tôi đã kết thúc việc sao chép mã này từ Google Closure Templates:

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

Xem https://closure-templates.googlecode.com/svn-history/r21/trunk/java/src/com/google/template/soy/internal/i18n/BidiUtils.java

+1

sai wi là gì các lớp học AWT? Nó không giống như họ đã bị bỏ rơi hoặc bất cứ điều gì như thế. –

+0

Nó không thực sự rõ ràng mà các lớp học AWT sẽ tiếp tục làm việc trong một môi trường không đầu, ví dụ. – Trejkaz

Các vấn đề liên quan