]> granicus.if.org Git - icu/commitdiff
ICU-10736 ULocale.isRightToLeft(), UScript.getCodeFromName(name)
authorMarkus Scherer <markus.icu@gmail.com>
Thu, 28 Aug 2014 18:15:00 +0000 (18:15 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Thu, 28 Aug 2014 18:15:00 +0000 (18:15 +0000)
X-SVN-Rev: 36267

icu4j/main/classes/core/src/com/ibm/icu/lang/UScript.java
icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/util/ULocaleTest.java

index 971b85fe848dac13297e67f671129114f0ca0d78..fa3d142056a90a7df36f5915774277ec3cdebb36 100644 (file)
@@ -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;
         }
index bc57266c4fd68d683484544c9f2e613870eb8897..e28726755ec88833a43537e910eee2bc0badff83 100644 (file)
@@ -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<ULocale> {
         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.
+     *
+     * <p>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.
+     *
+     * <p>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
 
     /**
index 93c3b3a09916770f98d408c3195edd28fac8998a..0df7c7f77cae51716ee60d4f44eb282f2ddfbb01 100644 (file)
@@ -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());
+    }
 }