Search in sources :

Example 16 with ULocale

use of com.ibm.icu.util.ULocale in project es6draft by anba.

the class IntlDataTools method collationCase.

static void collationCase(Path cldr) throws IOException {
    Files.walk(cldr.resolve("collation")).filter(Files::isRegularFile).forEach(p -> {
        try (Reader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) {
            Document document = document(reader);
            elementsByTagName(document, "collation").filter(e -> "standard".equals(e.getAttribute("type"))).forEach(e -> {
                elementByTagName(e, "cr").ifPresent(cr -> {
                    String text = cr.getTextContent();
                    if (text.contains("caseFirst")) {
                        System.out.println(p.getFileName());
                    }
                });
            });
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    });
    for (ULocale locale : Collator.getAvailableULocales()) {
        Collator collator = Collator.getInstance(locale);
        if (collator instanceof RuleBasedCollator) {
            RuleBasedCollator ruleBasedCollator = (RuleBasedCollator) collator;
            if (ruleBasedCollator.isUpperCaseFirst()) {
                System.out.printf("upper-first = %s%n", locale);
            } else if (ruleBasedCollator.isLowerCaseFirst()) {
                System.out.printf("lower-first = %s%n", locale);
            }
        }
    }
}
Also used : TimeZone(com.ibm.icu.util.TimeZone) java.util(java.util) Function(java.util.function.Function) DirectoryStream(java.nio.file.DirectoryStream) Matcher(java.util.regex.Matcher) ULocale(com.ibm.icu.util.ULocale) Document(org.w3c.dom.Document) StreamSupport(java.util.stream.StreamSupport) Collator(com.ibm.icu.text.Collator) Path(java.nio.file.Path) InputSource(org.xml.sax.InputSource) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) NodeList(org.w3c.dom.NodeList) Files(java.nio.file.Files) SystemTimeZoneType(com.ibm.icu.util.TimeZone.SystemTimeZoneType) IOException(java.io.IOException) Reader(java.io.Reader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Pattern(java.util.regex.Pattern) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) ULocale(com.ibm.icu.util.ULocale) Reader(java.io.Reader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Document(org.w3c.dom.Document) Collator(com.ibm.icu.text.Collator) RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator)

Example 17 with ULocale

use of com.ibm.icu.util.ULocale in project es6draft by anba.

the class LanguageData method addDerivedLanguages.

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;
}
Also used : ULocale(com.ibm.icu.util.ULocale) HashSet(java.util.HashSet)

Example 18 with ULocale

use of com.ibm.icu.util.ULocale in project es6draft by anba.

the class ListFormatObject method createListFormatter.

@SuppressWarnings("deprecation")
private ListFormatter createListFormatter() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    ICUResourceBundle r = (ICUResourceBundle) UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
    String resourceStyle = resourceStyle();
    return new ListFormatter(r.getWithFallback("listPattern/" + resourceStyle + "/2").getString(), r.getWithFallback("listPattern/" + resourceStyle + "/start").getString(), r.getWithFallback("listPattern/" + resourceStyle + "/middle").getString(), r.getWithFallback("listPattern/" + resourceStyle + "/end").getString());
}
Also used : ULocale(com.ibm.icu.util.ULocale) ICUResourceBundle(com.ibm.icu.impl.ICUResourceBundle) ListFormatter(com.ibm.icu.text.ListFormatter)

Example 19 with ULocale

use of com.ibm.icu.util.ULocale in project es6draft by anba.

the class PluralRulesObject method createNumberFormat.

private NumberFormat createNumberFormat() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    locale = locale.setKeywordValue("numbers", "latn");
    DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, NumberFormat.NUMBERSTYLE);
    if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) {
        numberFormat.setSignificantDigitsUsed(true);
        numberFormat.setMinimumSignificantDigits(minimumSignificantDigits);
        numberFormat.setMaximumSignificantDigits(maximumSignificantDigits);
    } else {
        numberFormat.setSignificantDigitsUsed(false);
        numberFormat.setMinimumIntegerDigits(minimumIntegerDigits);
        numberFormat.setMinimumFractionDigits(minimumFractionDigits);
        numberFormat.setMaximumFractionDigits(maximumFractionDigits);
    }
    // as required by ToRawPrecision/ToRawFixed
    numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    return numberFormat;
}
Also used : ULocale(com.ibm.icu.util.ULocale) DecimalFormat(com.ibm.icu.text.DecimalFormat)

Example 20 with ULocale

use of com.ibm.icu.util.ULocale in project es6draft by anba.

the class CollatorObject method createCollator.

private Collator createCollator() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    if ("search".equals(usage)) {
        // "search" usage cannot be set through unicode extensions (u-co-search), handle here:
        locale = locale.setKeywordValue("collation", "search");
    }
    RuleBasedCollator collator = (RuleBasedCollator) Collator.getInstance(locale);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
    collator.setNumericCollation(numeric);
    switch(caseFirst) {
        case "upper":
            collator.setUpperCaseFirst(true);
            break;
        case "lower":
            collator.setLowerCaseFirst(true);
            break;
        case "false":
            if (collator.isLowerCaseFirst()) {
                collator.setLowerCaseFirst(false);
            }
            if (collator.isUpperCaseFirst()) {
                collator.setUpperCaseFirst(false);
            }
            break;
        default:
            throw new AssertionError();
    }
    switch(sensitivity) {
        case "base":
            collator.setStrength(Collator.PRIMARY);
            break;
        case "accent":
            collator.setStrength(Collator.SECONDARY);
            break;
        case "case":
            collator.setStrength(Collator.PRIMARY);
            collator.setCaseLevel(true);
            break;
        case "variant":
            collator.setStrength(Collator.TERTIARY);
            break;
        default:
            throw new AssertionError();
    }
    collator.setAlternateHandlingShifted(ignorePunctuation);
    return collator;
}
Also used : RuleBasedCollator(com.ibm.icu.text.RuleBasedCollator) ULocale(com.ibm.icu.util.ULocale)

Aggregations

ULocale (com.ibm.icu.util.ULocale)25 RuleBasedCollator (com.ibm.icu.text.RuleBasedCollator)5 Collator (com.ibm.icu.text.Collator)2 DateTimePatternGenerator (com.ibm.icu.text.DateTimePatternGenerator)2 DecimalFormat (com.ibm.icu.text.DecimalFormat)2 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 Map (java.util.Map)2 Detector (com.cybozu.labs.langdetect.Detector)1 LangDetectException (com.cybozu.labs.langdetect.LangDetectException)1 Language (com.cybozu.labs.langdetect.Language)1 Skeleton (com.github.anba.es6draft.runtime.objects.intl.DateFieldSymbolTable.Skeleton)1 CacheLoader (com.google.common.cache.CacheLoader)1 LocaleString (com.google.template.soy.shared.restricted.ApiCallScopeBindingAnnotations.LocaleString)1 ICUResourceBundle (com.ibm.icu.impl.ICUResourceBundle)1 BreakIterator (com.ibm.icu.text.BreakIterator)1 ListFormatter (com.ibm.icu.text.ListFormatter)1