ICU-10736 ULocale.isRightToLeft(), UScript.getCodeFromName(name)

X-SVN-Rev: 36267
This commit is contained in:
Markus Scherer 2014-08-28 18:15:00 +00:00
parent ee1f29b584
commit 2572a84f50
3 changed files with 70 additions and 7 deletions

View 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;
}

View 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
/**

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