Java Code Examples for com.ibm.icu.util.ULocale#getScript()

The following examples show how to use com.ibm.icu.util.ULocale#getScript() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UScript.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
private static int[] getCodesFromLocale(ULocale locale) {
    // Multi-script languages, equivalent to the LocaleScript data
    // that we used to load from locale resource bundles.
    String lang = locale.getLanguage();
    if(lang.equals("ja")) {
        return new int[] { UScript.KATAKANA, UScript.HIRAGANA, UScript.HAN };
    }
    if(lang.equals("ko")) {
        return new int[] { UScript.HANGUL, UScript.HAN };
    }
    String script = locale.getScript();
    if(lang.equals("zh") && script.equals("Hant")) {
        return new int[] { UScript.HAN, UScript.BOPOMOFO };
    }
    // Explicit script code.
    if(script.length() != 0) {
        int scriptCode = UScript.getCodeFromName(script);
        if(scriptCode != UScript.INVALID_CODE) {
            if(scriptCode == UScript.SIMPLIFIED_HAN || scriptCode == UScript.TRADITIONAL_HAN) {
                scriptCode = UScript.HAN;
            }
            return new int[] { scriptCode };
        }
    }
    return null;
}
 
Example 2
Source File: UScript.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
private static int[] getCodesFromLocale(ULocale locale) {
    // Multi-script languages, equivalent to the LocaleScript data
    // that we used to load from locale resource bundles.
    String lang = locale.getLanguage();
    if(lang.equals("ja")) {
        return new int[] { UScript.KATAKANA, UScript.HIRAGANA, UScript.HAN };
    }
    if(lang.equals("ko")) {
        return new int[] { UScript.HANGUL, UScript.HAN };
    }
    String script = locale.getScript();
    if(lang.equals("zh") && script.equals("Hant")) {
        return new int[] { UScript.HAN, UScript.BOPOMOFO };
    }
    // Explicit script code.
    if(script.length() != 0) {
        int scriptCode = UScript.getCodeFromName(script);
        if(scriptCode != UScript.INVALID_CODE) {
            if(scriptCode == UScript.SIMPLIFIED_HAN || scriptCode == UScript.TRADITIONAL_HAN) {
                scriptCode = UScript.HAN;
            }
            return new int[] { scriptCode };
        }
    }
    return null;
}
 
Example 3
Source File: XLikelySubtags.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Implementation of LocaleMatcher.canonicalize(ULocale).
 */
public ULocale canonicalize(ULocale locale) {
    String lang = locale.getLanguage();
    String lang2 = languageAliases.get(lang);
    String region = locale.getCountry();
    String region2 = regionAliases.get(region);
    if (lang2 != null || region2 != null) {
        return new ULocale(
            lang2 == null ? lang : lang2,
            locale.getScript(),
            region2 == null ? region : region2);
    }
    return locale;
}
 
Example 4
Source File: IntlAbstractOperations.java    From es6draft with MIT License 5 votes vote down vote up
private static ULocale addLikelySubtagsWithDefaults(ULocale locale) {
    ULocale maximized = ULocale.addLikelySubtags(locale);
    if (maximized == locale) {
        // If already in maximal form or no data available for maximization, make sure
        // language, script and region are not undefined (ICU4J expects all are defined).
        String language = locale.getLanguage();
        String script = locale.getScript();
        String region = locale.getCountry();
        if (language.isEmpty() || script.isEmpty() || region.isEmpty()) {
            return new ULocale(toLocaleId(language, script, region));
        }
    }
    return maximized;
}
 
Example 5
Source File: LanguageData.java    From es6draft with MIT License 5 votes vote down vote up
private static Set<String> addDerivedLanguages(List<String> available) {
    HashSet<String> availableSet = new HashSet<>(available);
    HashSet<String> derivedSet = new HashSet<>(available);
    for (ULocale locale : ULocale.getAvailableLocales()) {
        String languageTag = locale.toLanguageTag();
        if (derivedSet.contains(languageTag)) {
            continue;
        }
        if (!(locale.getVariant().isEmpty() && locale.getUnicodeLocaleKeys().isEmpty())) {
            // Ignore locales with variants or unicode extension sequences.
            continue;
        }
        String language = locale.getLanguage();
        if (availableSet.contains(language)) {
            derivedSet.add(languageTag);
            continue;
        }
        String script = locale.getScript();
        if (!script.isEmpty()) {
            String languageScript = language + "-" + script;
            if (availableSet.contains(languageScript)) {
                derivedSet.add(languageTag);
                continue;
            }
        }
        String country = locale.getCountry();
        if (!country.isEmpty()) {
            String languageCountry = language + "-" + country;
            if (availableSet.contains(languageCountry)) {
                derivedSet.add(languageTag);
                continue;
            }
        }
    }
    return derivedSet;
}
 
Example 6
Source File: LocaleValidityChecker.java    From fitnotifications with Apache License 2.0 4 votes vote down vote up
public boolean isValid(ULocale locale, Where where) {
    where.set(null, null);
    final String language = locale.getLanguage();
    final String script = locale.getScript();
    final String region = locale.getCountry();
    final String variantString = locale.getVariant();
    final Set<Character> extensionKeys = locale.getExtensionKeys();
    //        if (language.isEmpty()) {
    //            // the only case where this is valid is if there is only an 'x' extension string
    //            if (!script.isEmpty() || !region.isEmpty() || variantString.isEmpty() 
    //                    || extensionKeys.size() != 1 || !extensionKeys.contains('x')) {
    //                return where.set(Datatype.x, "Null language only with x-...");
    //            }
    //            return true; // for x string, wellformedness = valid
    //        }
    if (!isValid(Datatype.language, language, where)) {
        // special case x
        if (language.equals("x")) {
            where.set(null, null); // for x, well-formed == valid
            return true;
        }
        return false;
    }
    if (!isValid(Datatype.script, script, where)) return false;
    if (!isValid(Datatype.region, region, where)) return false;
    if (!variantString.isEmpty()) {
        for (String variant : SEPARATOR.split(variantString)) {
            if (!isValid(Datatype.variant, variant, where)) return false;
        }
    }
    for (Character c : extensionKeys) {
        try {
            Datatype datatype = Datatype.valueOf(c+"");
            switch (datatype) {
            case x:
                return true; // if it is syntactic (checked by ULocale) it is valid
            case t:
            case u:
                if (!isValidU(locale, datatype, locale.getExtension(c), where)) return false;
                break;
            }
        } catch (Exception e) {
            return where.set(Datatype.illegal, c+"");
        }
    }
    return true;
}
 
Example 7
Source File: LocaleValidityChecker.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public boolean isValid(ULocale locale, Where where) {
    where.set(null, null);
    final String language = locale.getLanguage();
    final String script = locale.getScript();
    final String region = locale.getCountry();
    final String variantString = locale.getVariant();
    final Set<Character> extensionKeys = locale.getExtensionKeys();
    //        if (language.isEmpty()) {
    //            // the only case where this is valid is if there is only an 'x' extension string
    //            if (!script.isEmpty() || !region.isEmpty() || variantString.isEmpty()
    //                    || extensionKeys.size() != 1 || !extensionKeys.contains('x')) {
    //                return where.set(Datatype.x, "Null language only with x-...");
    //            }
    //            return true; // for x string, wellformedness = valid
    //        }
    if (!isValid(Datatype.language, language, where)) {
        // special case x
        if (language.equals("x")) {
            where.set(null, null); // for x, well-formed == valid
            return true;
        }
        return false;
    }
    if (!isValid(Datatype.script, script, where)) return false;
    if (!isValid(Datatype.region, region, where)) return false;
    if (!variantString.isEmpty()) {
        for (String variant : SEPARATOR.split(variantString)) {
            if (!isValid(Datatype.variant, variant, where)) return false;
        }
    }
    for (Character c : extensionKeys) {
        try {
            Datatype datatype = Datatype.valueOf(c+"");
            switch (datatype) {
            case x:
                return true; // if it is syntactic (checked by ULocale) it is valid
            case t:
            case u:
                if (!isValidU(locale, datatype, locale.getExtension(c), where)) return false;
                break;
            default:
                break;
            }
        } catch (Exception e) {
            return where.set(Datatype.illegal, c+"");
        }
    }
    return true;
}