Search in sources :

Example 6 with IllformedLocaleException

use of java.util.IllformedLocaleException in project jdk8u_jdk by JetBrains.

the class LocaleServiceProviderPool method getLookupLocale.

/**
     * Returns an instance of Locale used for service look up.
     * The result Locale has no extensions except for ja_JP_JP
     * and th_TH_TH
     *
     * @param locale the locale
     * @return the locale used for service look up
     */
static Locale getLookupLocale(Locale locale) {
    Locale lookupLocale = locale;
    if (locale.hasExtensions() && !locale.equals(JRELocaleConstants.JA_JP_JP) && !locale.equals(JRELocaleConstants.TH_TH_TH)) {
        // remove extensions
        Builder locbld = new Builder();
        try {
            locbld.setLocale(locale);
            locbld.clearExtensions();
            lookupLocale = locbld.build();
        } catch (IllformedLocaleException e) {
            // A Locale with non-empty extensions
            // should have well-formed fields except
            // for ja_JP_JP and th_TH_TH. Therefore,
            // it should never enter in this catch clause.
            config(LocaleServiceProviderPool.class, "A locale(" + locale + ") has non-empty extensions, but has illformed fields.");
            // Fallback - script field will be lost.
            lookupLocale = new Locale(locale.getLanguage(), locale.getCountry(), locale.getVariant());
        }
    }
    return lookupLocale;
}
Also used : Locale(java.util.Locale) Builder(java.util.Locale.Builder) IllformedLocaleException(java.util.IllformedLocaleException)

Example 7 with IllformedLocaleException

use of java.util.IllformedLocaleException in project android_frameworks_base by AOSPA.

the class LocaleStore method fillCache.

public static void fillCache(Context context) {
    if (sFullyInitialized) {
        return;
    }
    Set<String> simCountries = getSimCountries(context);
    for (String localeId : LocalePicker.getSupportedLocales(context)) {
        if (localeId.isEmpty()) {
            throw new IllformedLocaleException("Bad locale entry in locale_config.xml");
        }
        LocaleInfo li = new LocaleInfo(localeId);
        if (simCountries.contains(li.getLocale().getCountry())) {
            li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
        }
        sLocaleCache.put(li.getId(), li);
        final Locale parent = li.getParent();
        if (parent != null) {
            String parentId = parent.toLanguageTag();
            if (!sLocaleCache.containsKey(parentId)) {
                sLocaleCache.put(parentId, new LocaleInfo(parent));
            }
        }
    }
    boolean isInDeveloperMode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
    for (String localeId : LocalePicker.getPseudoLocales()) {
        LocaleInfo li = getLocaleInfo(Locale.forLanguageTag(localeId));
        if (isInDeveloperMode) {
            li.setTranslated(true);
            li.mIsPseudo = true;
            li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
        } else {
            sLocaleCache.remove(li.getId());
        }
    }
    // TODO: See if we can reuse what LocaleList.matchScore does
    final HashSet<String> localizedLocales = new HashSet<>();
    for (String localeId : LocalePicker.getSystemAssetLocales()) {
        LocaleInfo li = new LocaleInfo(localeId);
        final String country = li.getLocale().getCountry();
        // All this is to figure out if we should suggest a country
        if (!country.isEmpty()) {
            LocaleInfo cachedLocale = null;
            if (sLocaleCache.containsKey(li.getId())) {
                // the simple case, e.g. fr-CH
                cachedLocale = sLocaleCache.get(li.getId());
            } else {
                // e.g. zh-TW localized, zh-Hant-TW in cache
                final String langScriptCtry = li.getLangScriptKey() + "-" + country;
                if (sLocaleCache.containsKey(langScriptCtry)) {
                    cachedLocale = sLocaleCache.get(langScriptCtry);
                }
            }
            if (cachedLocale != null) {
                cachedLocale.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CFG;
            }
        }
        localizedLocales.add(li.getLangScriptKey());
    }
    // Serbian in Latin script is only partially localized in N.
    localizedLocales.remove("sr-Latn");
    for (LocaleInfo li : sLocaleCache.values()) {
        li.setTranslated(localizedLocales.contains(li.getLangScriptKey()));
    }
    addSuggestedLocalesForRegion(Locale.getDefault());
    sFullyInitialized = true;
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException) HashSet(java.util.HashSet)

Example 8 with IllformedLocaleException

use of java.util.IllformedLocaleException in project j2objc by google.

the class LocaleTest method test_Builder_unicodeKeywords.

public void test_Builder_unicodeKeywords() {
    // Adding and removing attributes
    Locale.Builder b = new Locale.Builder();
    b.setLanguage("en");
    // Key not of length 2.
    try {
        b.setUnicodeLocaleKeyword("k", "fooo");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    // Value too short
    try {
        b.setUnicodeLocaleKeyword("k", "fo");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    // Value too long
    try {
        b.setUnicodeLocaleKeyword("k", "foooooooo");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    // Null should clear the key.
    b.setUnicodeLocaleKeyword("bo", "baaz");
    Locale l = b.build();
    assertEquals("bo-baaz", l.getExtension('u'));
    assertEquals("baaz", l.getUnicodeLocaleType("bo"));
    b = new Locale.Builder();
    b.setUnicodeLocaleKeyword("bo", "baaz");
    b.setUnicodeLocaleKeyword("bo", null);
    l = b.build();
    assertNull(l.getExtension('u'));
    assertNull(l.getUnicodeLocaleType("bo"));
    // When we set attributes, they should show up before extensions.
    b = new Locale.Builder();
    b.addUnicodeLocaleAttribute("fooo");
    b.addUnicodeLocaleAttribute("gooo");
    b.setUnicodeLocaleKeyword("fo", "baz");
    b.setUnicodeLocaleKeyword("ka", "kaz");
    l = b.build();
    assertEquals("fooo-gooo-fo-baz-ka-kaz", l.getExtension('u'));
    assertEquals("baz", l.getUnicodeLocaleType("fo"));
    assertEquals("kaz", l.getUnicodeLocaleType("ka"));
    assertTrue(l.getUnicodeLocaleAttributes().contains("fooo"));
    assertTrue(l.getUnicodeLocaleAttributes().contains("gooo"));
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException)

Example 9 with IllformedLocaleException

use of java.util.IllformedLocaleException in project android_frameworks_base by ResurrectionRemix.

the class LocaleStore method fillCache.

public static void fillCache(Context context) {
    if (sFullyInitialized) {
        return;
    }
    Set<String> simCountries = getSimCountries(context);
    for (String localeId : LocalePicker.getSupportedLocales(context)) {
        if (localeId.isEmpty()) {
            throw new IllformedLocaleException("Bad locale entry in locale_config.xml");
        }
        LocaleInfo li = new LocaleInfo(localeId);
        if (simCountries.contains(li.getLocale().getCountry())) {
            li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
        }
        sLocaleCache.put(li.getId(), li);
        final Locale parent = li.getParent();
        if (parent != null) {
            String parentId = parent.toLanguageTag();
            if (!sLocaleCache.containsKey(parentId)) {
                sLocaleCache.put(parentId, new LocaleInfo(parent));
            }
        }
    }
    boolean isInDeveloperMode = Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
    for (String localeId : LocalePicker.getPseudoLocales()) {
        LocaleInfo li = getLocaleInfo(Locale.forLanguageTag(localeId));
        if (isInDeveloperMode) {
            li.setTranslated(true);
            li.mIsPseudo = true;
            li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
        } else {
            sLocaleCache.remove(li.getId());
        }
    }
    // TODO: See if we can reuse what LocaleList.matchScore does
    final HashSet<String> localizedLocales = new HashSet<>();
    for (String localeId : LocalePicker.getSystemAssetLocales()) {
        LocaleInfo li = new LocaleInfo(localeId);
        final String country = li.getLocale().getCountry();
        // All this is to figure out if we should suggest a country
        if (!country.isEmpty()) {
            LocaleInfo cachedLocale = null;
            if (sLocaleCache.containsKey(li.getId())) {
                // the simple case, e.g. fr-CH
                cachedLocale = sLocaleCache.get(li.getId());
            } else {
                // e.g. zh-TW localized, zh-Hant-TW in cache
                final String langScriptCtry = li.getLangScriptKey() + "-" + country;
                if (sLocaleCache.containsKey(langScriptCtry)) {
                    cachedLocale = sLocaleCache.get(langScriptCtry);
                }
            }
            if (cachedLocale != null) {
                cachedLocale.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CFG;
            }
        }
        localizedLocales.add(li.getLangScriptKey());
    }
    // Serbian in Latin script is only partially localized in N.
    localizedLocales.remove("sr-Latn");
    for (LocaleInfo li : sLocaleCache.values()) {
        li.setTranslated(localizedLocales.contains(li.getLangScriptKey()));
    }
    addSuggestedLocalesForRegion(Locale.getDefault());
    sFullyInitialized = true;
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException) HashSet(java.util.HashSet)

Example 10 with IllformedLocaleException

use of java.util.IllformedLocaleException in project lucene-solr by apache.

the class DateFormatEvaluator method evaluate.

@Override
public String evaluate(String expression, Context context) {
    List<Object> l = parseParams(expression, context.getVariableResolver());
    if (l.size() < 2 || l.size() > 4) {
        throw new DataImportHandlerException(SEVERE, "'formatDate()' must have two, three or four parameters ");
    }
    Object o = l.get(0);
    Object format = l.get(1);
    if (format instanceof VariableWrapper) {
        VariableWrapper wrapper = (VariableWrapper) format;
        o = wrapper.resolve();
        format = o.toString();
    }
    // we default to ENGLISH for dates for full Java 9 compatibility
    Locale locale = Locale.ENGLISH;
    if (l.size() > 2) {
        Object localeObj = l.get(2);
        String localeStr = null;
        if (localeObj instanceof VariableWrapper) {
            localeStr = ((VariableWrapper) localeObj).resolve().toString();
        } else {
            localeStr = localeObj.toString();
        }
        locale = availableLocales.get(localeStr);
        if (locale == null)
            try {
                locale = new Locale.Builder().setLanguageTag(localeStr).build();
            } catch (IllformedLocaleException ex) {
                throw new DataImportHandlerException(SEVERE, "Malformed / non-existent locale: " + localeStr, ex);
            }
    }
    // DWS TODO: is this the right default for us?  Deserves explanation if so.
    TimeZone tz = TimeZone.getDefault();
    if (l.size() == 4) {
        Object tzObj = l.get(3);
        String tzStr = null;
        if (tzObj instanceof VariableWrapper) {
            tzStr = ((VariableWrapper) tzObj).resolve().toString();
        } else {
            tzStr = tzObj.toString();
        }
        if (availableTimezones.contains(tzStr)) {
            tz = TimeZone.getTimeZone(tzStr);
        } else {
            throw new DataImportHandlerException(SEVERE, "Unsupported Timezone: " + tzStr);
        }
    }
    String dateFmt = format.toString();
    SimpleDateFormat fmt = getDateFormat(dateFmt, tz, locale);
    Date date = null;
    if (o instanceof VariableWrapper) {
        date = evaluateWrapper((VariableWrapper) o, locale, tz);
    } else {
        date = evaluateString(o.toString(), locale, tz);
    }
    return fmt.format(date);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) IllformedLocaleException(java.util.IllformedLocaleException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

IllformedLocaleException (java.util.IllformedLocaleException)12 Locale (java.util.Locale)12 HashSet (java.util.HashSet)5 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Builder (java.util.Locale.Builder)1 TimeZone (java.util.TimeZone)1