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

The following examples show how to use com.ibm.icu.util.ULocale#addLikelySubtags() . 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: LocaleValidityChecker.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
/**
 * @param locale
 * @param subtag
 * @return
 */
private boolean isSubdivision(ULocale locale, String subtag) {
    // First check if the subtag is valid
    if (subtag.length() < 3) {
        return false;
    }
    String region = subtag.substring(0, subtag.charAt(0) <= '9' ? 3 : 2);
    String subdivision = subtag.substring(region.length());
    if (ValidIdentifiers.isValid(Datatype.subdivision, datasubtypes, region, subdivision) == null) {
        return false;
    }
    // Then check for consistency with the locale's region
    String localeRegion = locale.getCountry();
    if (localeRegion.isEmpty()) {
        ULocale max = ULocale.addLikelySubtags(locale);
        localeRegion = max.getCountry();
    }
    if (!region.equalsIgnoreCase(localeRegion)) {
        return false;
    }
    return true;
}
 
Example 2
Source File: LocaleValidityChecker.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param locale
 * @param subtag
 * @return
 */
private boolean isSubdivision(ULocale locale, String subtag) {
    // First check if the subtag is valid
    if (subtag.length() < 3) {
        return false;
    }
    String region = subtag.substring(0, subtag.charAt(0) <= '9' ? 3 : 2);
    String subdivision = subtag.substring(region.length());
    if (ValidIdentifiers.isValid(Datatype.subdivision, datasubtypes, region, subdivision) == null) {
        return false;
    }
    // Then check for consistency with the locale's region
    String localeRegion = locale.getCountry();
    if (localeRegion.isEmpty()) {
        ULocale max = ULocale.addLikelySubtags(locale);
        localeRegion = max.getCountry();
    }
    if (!region.equalsIgnoreCase(localeRegion)) {
        return false;
    }
    return true;
}
 
Example 3
Source File: DateTimePatternGenerator.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private void getAllowedHourFormats(ULocale uLocale) {
    // key can be either region or locale (lang_region)
    //        ZW{
    //            allowed{
    //                "h",
    //                "H",
    //            }
    //            preferred{"h"}
    //        }
    //        af_ZA{
    //            allowed{
    //                "h",
    //                "H",
    //                "hB",
    //                "hb",
    //            }
    //            preferred{"h"}
    //        }

    ULocale max = ULocale.addLikelySubtags(uLocale);
    String country = max.getCountry();
    if (country.isEmpty()) {
        country = "001";
    }
    String langCountry = max.getLanguage() + "_" + country;
    String[] list = LOCALE_TO_ALLOWED_HOUR.get(langCountry);
    if (list == null) {
        list = LOCALE_TO_ALLOWED_HOUR.get(country);
        if (list == null) {
            list = LAST_RESORT_ALLOWED_HOUR_FORMAT;
        }
    }
    allowedHourFormats = list;
}
 
Example 4
Source File: TimeZoneFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Private method returning the target region. The target regions is determined by
 * the locale of this instance. When a generic name is coming from
 * a meta zone, this region is used for checking if the time zone
 * is a reference zone of the meta zone.
 *
 * @return the target region
 */
private synchronized String getTargetRegion() {
    if (_region == null) {
        _region = _locale.getCountry();
        if (_region.length() == 0) {
            ULocale tmp = ULocale.addLikelySubtags(_locale);
            _region = tmp.getCountry();
            if (_region.length() == 0) {
                _region = "001";
            }
        }
    }
    return _region;
}
 
Example 5
Source File: UScript.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Helper function to find the code from locale.
 * @param locale The locale.
 */
private static int[] findCodeFromLocale(ULocale locale) {
    int[] result = getCodesFromLocale(locale);
    if(result != null) {
        return result;
    }
    ULocale likely = ULocale.addLikelySubtags(locale);
    return getCodesFromLocale(likely);
}
 
Example 6
Source File: TZDBTimeZoneNames.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private String getTargetRegion() {
    if (_region == null) {
        String region = _locale.getCountry();
        if (region.length() == 0) {
            ULocale tmp = ULocale.addLikelySubtags(_locale);
            region = tmp.getCountry();
            if (region.length() == 0) {
                region = "001";
            }
        }
        _region = region;
    }
    return _region;
}
 
Example 7
Source File: TimeZoneGenericNames.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Private method returning the target region. The target regions is determined by
 * the locale of this instance. When a generic name is coming from
 * a meta zone, this region is used for checking if the time zone
 * is a reference zone of the meta zone.
 *
 * @return the target region
 */
private synchronized String getTargetRegion() {
    if (_region == null) {
        _region = _locale.getCountry();
        if (_region.length() == 0) {
            ULocale tmp = ULocale.addLikelySubtags(_locale);
            _region = tmp.getCountry();
            if (_region.length() == 0) {
                _region = "001";
            }
        }
    }
    return _region;
}
 
Example 8
Source File: UScript.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper function to find the code from locale.
 * @param locale The locale.
 */
private static int[] findCodeFromLocale(ULocale locale) {
    int[] result = getCodesFromLocale(locale);
    if(result != null) {
        return result;
    }
    ULocale likely = ULocale.addLikelySubtags(locale);
    return getCodesFromLocale(likely);
}
 
Example 9
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;
}