Java Code Examples for android.icu.impl.locale.BaseLocale

Following code examples demonstrate how to use android.icu.impl.locale.BaseLocalefrom android. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use android.icu.impl.locale.BaseLocaleand various code implementation of this class.

        while (true) {
            rulesId = (String) idMap.get(localeId);
            if (rulesId != null) {
                break;
            }
            int ix = localeId.lastIndexOf(BaseLocale.SEP);
            if (ix == -1) {
                break;
            }
            localeId = localeId.substring(0, ix);
        } 


    public PluralRanges getPluralRanges(ULocale locale) {
        String localeId = ULocale.canonicalize(locale.getBaseName());
        while (true) {
            PluralRanges result = (PluralRanges) localeIdToPluralRanges.get(localeId);
            if (result != null) {
                return result;
            }
            int ix = localeId.lastIndexOf(BaseLocale.SEP);
            if (ix == -1) {
                return UNKNOWN_RANGE;
            }
            localeId = localeId.substring(0, ix);
        }
    } 

    private ULocale addLikelySubtags(ULocale languageCode) {
        if (languageCode.equals(UNKNOWN_LOCALE)) {
            return UNKNOWN_LOCALE;
        }
        ULocale result = ULocale.addLikelySubtags(languageCode);
        if (result != null && !result.equals(languageCode)) {
            return result;
        }
        String language = languageCode.getLanguage();
        String script = languageCode.getScript();
        String region = languageCode.getCountry();
        StringBuilder stringBuilder = new StringBuilder();
        if (language.length() == 0) {
            language = "und";
        }
        stringBuilder = stringBuilder.append(language).append(BaseLocale.SEP);
        if (script.length() == 0) {
            script = "Zzzz";
        }
        stringBuilder = stringBuilder.append(script).append(BaseLocale.SEP);
        if (region.length() == 0) {
            region = "ZZ";
        }
        return new ULocale(stringBuilder.append(region).toString());
    } 

    public static final Locale localeFromString(String s) {
        String language = s;
        String region = XmlPullParser.NO_NAMESPACE;
        String variant = XmlPullParser.NO_NAMESPACE;
        int x = s.indexOf(BaseLocale.SEP);
        if (x != -1) {
            region = s.substring(x + 1);
            language = s.substring(0, x);
        }
        x = region.indexOf(BaseLocale.SEP);
        if (x != -1) {
            variant = region.substring(x + 1);
            region = region.substring(0, x);
        }
        return new Locale(language, region, variant);
    } 

        synchronized (this) {
            if (this.lastLocale == null || !this.lastLocale.equals(localeName)) {
                PeriodFormatterData ld = (PeriodFormatterData) this.cache.get(localeName);
                if (ld == null) {
                    String ln = localeName;
                    while (!this.availableLocales.contains(ln)) {
                        int ix = ln.lastIndexOf(BaseLocale.SEP);
                        if (ix <= -1) {
                            if ("test".equals(ln)) {
                                ln = null;
                                break;
                            }
                            ln = "test";
                        } else {
                            ln = ln.substring(0, ix);
                        }
                    }
                    if (ln != null) {
                        String name = "data/pfd_" + ln + ".xml";
                        try {
                            InputStreamReader reader = new InputStreamReader(ICUData.getRequiredStream(getClass(), name), "UTF-8");
                            DataRecord dr = DataRecord.read(ln, new XMLRecordReader(reader));
                            reader.close();
                            if (dr != null) {
                                ld = new PeriodFormatterData(localeName, dr);
                            }
                            this.cache.put(localeName, ld);
                        } catch (UnsupportedEncodingException e) {
                            throw new MissingResourceException("Unhandled encoding for resource " + name, name, "");
                        } catch (IOException e2) {
                            throw new ICUUncheckedIOException("Failed to close() resource " + name, e2);
                        }
                    }
                    throw new MissingResourceException("Duration data not found for  " + localeName, PATH, localeName);
                }
                this.lastData = ld;
                this.lastLocale = localeName;
                return ld;
            }
            PeriodFormatterData periodFormatterData = this.lastData;
            return periodFormatterData;
        } 

    private static BreakIterator createBreakInstance(ULocale locale, int kind) {
        String typeKey;
        BreakIterator iter = null;
        ICUResourceBundle rb = (ICUResourceBundle) ICUResourceBundle.getBundleInstance("android/icu/impl/data/icudt56b/brkitr", locale, OpenType.LOCALE_ROOT);
        String typeKeyExt = null;
        if (kind == 2) {
            String lbKeyValue = locale.getKeywordValue("lb");
            if (lbKeyValue != null && (lbKeyValue.equals("strict") || lbKeyValue.equals("normal") || lbKeyValue.equals("loose"))) {
                typeKeyExt = BaseLocale.SEP + lbKeyValue;
            }
        }
        if (typeKeyExt == null) {
            try {
                typeKey = KIND_NAMES[kind];
            } catch (Exception e) {
                throw new MissingResourceException(e.toString(), "", "");
            }
        }
        typeKey = KIND_NAMES[kind] + typeKeyExt;
        try {
            iter = RuleBasedBreakIterator.getInstanceFromCompiledRules(ICUBinary.getData("brkitr/" + rb.getStringWithFallback("boundaries/" + typeKey)));
        } catch (Exception e2) {
            Assert.fail(e2);
        }
        ULocale uloc = ULocale.forLocale(rb.getLocale());
        iter.setLocale(uloc, uloc);
        iter.setBreakType(kind);
        return iter;
    } 

        if (disableFallback) {
            b = instantiateBundle(baseName, localeID, null, root, disableFallback);
        } else {
            b = instantiateBundle(baseName, localeID, ULocale.getDefault().getBaseName(), root, disableFallback);
        } 

Advertisement
Javadoc
@hide Only a subset of ICU is exposed in Android
Advertisement