Search in sources :

Example 1 with IllformedLocaleException

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

the class LocaleTest method test_Builder_unicodeAttributes.

public void test_Builder_unicodeAttributes() {
    // Adding and removing attributes
    Locale.Builder b = new Locale.Builder();
    b.setLanguage("en");
    // Well formed attribute.
    b.addUnicodeLocaleAttribute("foooo");
    try {
        b.addUnicodeLocaleAttribute("fo");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    try {
        b.removeUnicodeLocaleAttribute("fo");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    try {
        b.addUnicodeLocaleAttribute("greaterthaneightchars");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    try {
        b.removeUnicodeLocaleAttribute("greaterthaneightchars");
        fail();
    } catch (IllformedLocaleException ifle) {
    }
    try {
        b.addUnicodeLocaleAttribute(null);
        fail();
    } catch (NullPointerException npe) {
    }
    try {
        b.removeUnicodeLocaleAttribute(null);
        fail();
    } catch (NullPointerException npe) {
    }
    Locale l = b.build();
    assertEquals("en-u-foooo", l.toLanguageTag());
    assertTrue(l.getUnicodeLocaleAttributes().contains("foooo"));
    b.addUnicodeLocaleAttribute("dAtA");
    l = b.build();
    assertEquals("data-foooo", l.getExtension('u'));
    assertTrue(l.getUnicodeLocaleAttributes().contains("data"));
    assertTrue(l.getUnicodeLocaleAttributes().contains("foooo"));
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException)

Example 2 with IllformedLocaleException

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

the class LocaleTest method test_Builder_setLocale.

public void test_Builder_setLocale() {
    // Default case.
    Locale.Builder b = new Locale.Builder();
    b.setLocale(Locale.US);
    assertEquals("en", b.build().getLanguage());
    assertEquals("US", b.build().getCountry());
    // Should throw when locale is malformed.
    // - Bad language
    Locale bad = new Locale("e", "US");
    b = new Locale.Builder();
    try {
        b.setLocale(bad);
        fail();
    } catch (IllformedLocaleException expected) {
    }
    // - Bad country
    bad = new Locale("en", "USA");
    try {
        b.setLocale(bad);
        fail();
    } catch (IllformedLocaleException expected) {
    }
    // - Bad variant
    bad = new Locale("en", "US", "c");
    try {
        b.setLocale(bad);
        fail();
    } catch (IllformedLocaleException expected) {
    }
    // Test values are normalized as they should be
    b = new Locale.Builder();
    Locale good = new Locale("EN", "us", "variant-VARIANT");
    b.setLocale(good);
    Locale l = b.build();
    assertEquals("en", l.getLanguage());
    assertEquals("US", l.getCountry());
    assertEquals("variant_VARIANT", l.getVariant());
    // Test that none of the existing fields are messed with
    // if the locale update fails.
    b = new Locale.Builder();
    b.setLanguage("fr").setRegion("FR");
    try {
        b.setLocale(bad);
        fail();
    } catch (IllformedLocaleException expected) {
    }
    l = b.build();
    assertEquals("fr", l.getLanguage());
    assertEquals("FR", l.getCountry());
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException)

Example 3 with IllformedLocaleException

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

the class LocaleTest method test_Builder_setExtension.

public void test_Builder_setExtension() {
    Locale.Builder b = new Locale.Builder();
    b.setExtension('g', "FO_ba-BR_bg");
    Locale l = b.build();
    assertEquals("fo-ba-br-bg", l.getExtension('g'));
    b = new Locale.Builder();
    // Too short
    try {
        b.setExtension('g', "fo-ba-br-x");
        fail();
    } catch (IllformedLocaleException expected) {
    }
    // Too long
    try {
        b.setExtension('g', "fo-ba-br-extension");
        fail();
    } catch (IllformedLocaleException expected) {
    }
    // Special case, the private use extension allows single char subtags.
    b.setExtension(Locale.PRIVATE_USE_EXTENSION, "fo-ba-br-m");
    l = b.build();
    assertEquals("fo-ba-br-m", l.getExtension('x'));
    // Special case, the unicode locale extension must be parsed into
    // its individual components. The correctness of the parse is tested
    // in test_parseUnicodeExtension.
    b.setExtension(Locale.UNICODE_LOCALE_EXTENSION, "foooo_BaaaR-BA_Baz-bI_BIZ");
    l = b.build();
    // Note that attributes and keywords are sorted alphabetically.
    assertEquals("baaar-foooo-ba-baz-bi-biz", l.getExtension('u'));
    assertTrue(l.getUnicodeLocaleAttributes().contains("foooo"));
    assertTrue(l.getUnicodeLocaleAttributes().contains("baaar"));
    assertEquals("baz", l.getUnicodeLocaleType("ba"));
    assertEquals("biz", l.getUnicodeLocaleType("bi"));
}
Also used : Locale(java.util.Locale) IllformedLocaleException(java.util.IllformedLocaleException)

Example 4 with IllformedLocaleException

use of java.util.IllformedLocaleException in project platform_frameworks_base by android.

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 5 with IllformedLocaleException

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

the class NumberFormatTransformer method transformRow.

@Override
@SuppressWarnings("unchecked")
public Object transformRow(Map<String, Object> row, Context context) {
    for (Map<String, String> fld : context.getAllEntityFields()) {
        String style = context.replaceTokens(fld.get(FORMAT_STYLE));
        if (style != null) {
            String column = fld.get(DataImporter.COLUMN);
            String srcCol = fld.get(RegexTransformer.SRC_COL_NAME);
            String localeStr = context.replaceTokens(fld.get(LOCALE));
            if (srcCol == null)
                srcCol = column;
            Locale locale = Locale.ROOT;
            if (localeStr != null) {
                try {
                    locale = new Locale.Builder().setLanguageTag(localeStr).build();
                } catch (IllformedLocaleException e) {
                    throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Invalid Locale '" + localeStr + "' specified for field: " + fld, e);
                }
            }
            Object val = row.get(srcCol);
            String styleSmall = style.toLowerCase(Locale.ROOT);
            if (val instanceof List) {
                List<String> inputs = (List) val;
                List results = new ArrayList();
                for (String input : inputs) {
                    try {
                        results.add(process(input, styleSmall, locale));
                    } catch (ParseException e) {
                        throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Failed to apply NumberFormat on column: " + column, e);
                    }
                }
                row.put(column, results);
            } else {
                if (val == null || val.toString().trim().equals(""))
                    continue;
                try {
                    row.put(column, process(val.toString(), styleSmall, locale));
                } catch (ParseException e) {
                    throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "Failed to apply NumberFormat on column: " + column, e);
                }
            }
        }
    }
    return row;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) IllformedLocaleException(java.util.IllformedLocaleException) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException)

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