From 2572a84f50f9e738646f1364ff0bd46ef94ad1cc Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Thu, 28 Aug 2014 18:15:00 +0000 Subject: [PATCH] ICU-10736 ULocale.isRightToLeft(), UScript.getCodeFromName(name) X-SVN-Rev: 36267 --- .../core/src/com/ibm/icu/lang/UScript.java | 14 ++--- .../core/src/com/ibm/icu/util/ULocale.java | 51 +++++++++++++++++++ .../ibm/icu/dev/test/util/ULocaleTest.java | 12 +++++ 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/lang/UScript.java b/icu4j/main/classes/core/src/com/ibm/icu/lang/UScript.java index 971b85fe84..fa3d142056 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/lang/UScript.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/lang/UScript.java @@ -1043,19 +1043,19 @@ public final class UScript { } /** - * Gets a script codes associated with the given ISO 15924 abbreviation or name. + * Returns the script code associated with the given Unicode script property alias + * (name or abbreviation). + * Short aliases are ISO 15924 script codes. * Returns MALAYAM given "Malayam" OR "Mlym". * * @param nameOrAbbr name of the script or ISO 15924 code - * @return The script code value or INVALID_CODE if the code cannot be found. - * @internal - * @deprecated This API is ICU internal only. + * @return The script code value, or INVALID_CODE if the code cannot be found. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. */ - @Deprecated public static final int getCodeFromName(String nameOrAbbr) { try { - return UCharacter.getPropertyValueEnum(UProperty.SCRIPT, - nameOrAbbr); + return UCharacter.getPropertyValueEnum(UProperty.SCRIPT, nameOrAbbr); } catch (IllegalArgumentException e) { return INVALID_CODE; } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java b/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java index bc57266c4f..e28726755e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java @@ -41,6 +41,7 @@ import com.ibm.icu.impl.locale.LocaleSyntaxException; import com.ibm.icu.impl.locale.ParseStatus; import com.ibm.icu.impl.locale.UnicodeLocaleExtension; import com.ibm.icu.impl.locale.KeyTypeData; +import com.ibm.icu.lang.UScript; import com.ibm.icu.text.LocaleDisplayNames; import com.ibm.icu.text.LocaleDisplayNames.DialectHandling; @@ -1331,6 +1332,56 @@ public final class ULocale implements Serializable, Comparable { return LocaleIDs.getISO3Country(getCountry(localeID)); } + /** + * Pairs of (language subtag, + or -) for finding out fast if common languages + * are LTR (minus) or RTL (plus). + */ + private static final String LANG_DIR_STRING = + "root-en-es-pt-zh-ja-ko-de-fr-it-ar+he+fa+ru-nl-pl-th-tr-"; + + /** + * Returns whether this locale's script is written right-to-left. + * If there is no script subtag, then the likely script is used, + * see {@link #addLikelySubtags(ULocale)}. + * If no likely script is known, then false is returned. + * + *

A script is right-to-left according to the CLDR script metadata + * which corresponds to whether the script's letters have Bidi_Class=R or AL. + * + *

Returns true for "ar" and "en-Hebr", false for "zh" and "fa-Cyrl". + * + * @return true if the locale's script is written right-to-left + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public boolean isRightToLeft() { + String script = getScript(); + if (script.length() == 0) { + // Fastpath: We know the likely scripts and their writing direction + // for some common languages. + String lang = getLanguage(); + if (lang.length() == 0) { + return false; + } + int langIndex = LANG_DIR_STRING.indexOf(lang); + if (langIndex >= 0) { + switch (LANG_DIR_STRING.charAt(langIndex + lang.length())) { + case '-': return false; + case '+': return true; + default: break; // partial match of a longer code + } + } + // Otherwise, find the likely script. + ULocale likely = addLikelySubtags(this); + script = likely.getScript(); + if (script.length() == 0) { + return false; + } + } + int scriptCode = UScript.getCodeFromName(script); + return UScript.isRightToLeft(scriptCode); + } + // display names /** diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java index 93c3b3a099..0df7c7f77c 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java @@ -4553,4 +4553,16 @@ public class ULocaleTest extends TestFmwk { assertEquals("keyword=" + keyword + ", value=" + value, expected, legacyType); } } + + public void TestIsRightToLeft() { + assertFalse("root LTR", ULocale.ROOT.isRightToLeft()); + assertFalse("zh LTR", ULocale.CHINESE.isRightToLeft()); + assertTrue("ar RTL", new ULocale("ar").isRightToLeft()); + assertTrue("und-EG RTL", new ULocale("und-EG").isRightToLeft()); + assertFalse("fa-Cyrl LTR", new ULocale("fa-Cyrl").isRightToLeft()); + assertTrue("en-Hebr RTL", new ULocale("en-Hebr").isRightToLeft()); + assertTrue("ckb RTL", new ULocale("ckb").isRightToLeft()); // Sorani Kurdish + assertFalse("fil LTR", new ULocale("fil").isRightToLeft()); + assertFalse("he-Zyxw LTR", new ULocale("he-Zyxw").isRightToLeft()); + } }