com.ibm.icu.impl.LocaleUtility Java Examples

The following examples show how to use com.ibm.icu.impl.LocaleUtility. 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: ULocale.java    From fitnotifications with Apache License 2.0 4 votes vote down vote up
/**
 * {@icu} Based on a list of acceptable locales, determine an available locale for the
 * user.  NullPointerException is thrown if acceptLanguageList or availableLocales is
 * null.  If fallback is non-null, it will contain true if a fallback locale (one not
 * in the acceptLanguageList) was returned.  The value on entry is ignored.  ULocale
 * will be one of the locales in availableLocales, or the ROOT ULocale if if a ROOT
 * locale was used as a fallback (because nothing else in availableLocales matched).
 * No ULocale array element should be null; behavior is undefined if this is the case.
 * @param acceptLanguageList list of acceptable locales
 * @param availableLocales list of available locales. One of these will be returned.
 * @param fallback if non-null, a 1-element array containing a boolean to be set with
 * the fallback status
 * @return one of the locales from the availableLocales list, or null if none match
 * @stable ICU 3.4
 */

public static ULocale acceptLanguage(ULocale[] acceptLanguageList, ULocale[]
        availableLocales, boolean[] fallback) {
    // fallbacklist
    int i,j;
    if(fallback != null) {
        fallback[0]=true;
    }
    for(i=0;i<acceptLanguageList.length;i++) {
        ULocale aLocale = acceptLanguageList[i];
        boolean[] setFallback = fallback;
        do {
            for(j=0;j<availableLocales.length;j++) {
                if(availableLocales[j].equals(aLocale)) {
                    if(setFallback != null) {
                        setFallback[0]=false; // first time with this locale - not a fallback.
                    }
                    return availableLocales[j];
                }
                // compare to scriptless alias, so locales such as
                // zh_TW, zh_CN are considered as available locales - see #7190
                if (aLocale.getScript().length() == 0
                        && availableLocales[j].getScript().length() > 0
                        && availableLocales[j].getLanguage().equals(aLocale.getLanguage())
                        && availableLocales[j].getCountry().equals(aLocale.getCountry())
                        && availableLocales[j].getVariant().equals(aLocale.getVariant())) {
                    ULocale minAvail = ULocale.minimizeSubtags(availableLocales[j]);
                    if (minAvail.getScript().length() == 0) {
                        if(setFallback != null) {
                            setFallback[0] = false; // not a fallback.
                        }
                        return aLocale;
                    }
                }
            }
            Locale loc = aLocale.toLocale();
            Locale parent = LocaleUtility.fallback(loc);
            if(parent != null) {
                aLocale = new ULocale(parent);
            } else {
                aLocale = null;
            }
            setFallback = null; // Do not set fallback in later iterations
        } while (aLocale != null);
    }
    return null;
}
 
Example #2
Source File: ULocale.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@icu} Based on a list of acceptable locales, determine an available locale for the
 * user.  NullPointerException is thrown if acceptLanguageList or availableLocales is
 * null.  If fallback is non-null, it will contain true if a fallback locale (one not
 * in the acceptLanguageList) was returned.  The value on entry is ignored.  ULocale
 * will be one of the locales in availableLocales, or the ROOT ULocale if if a ROOT
 * locale was used as a fallback (because nothing else in availableLocales matched).
 * No ULocale array element should be null; behavior is undefined if this is the case.
 * @param acceptLanguageList list of acceptable locales
 * @param availableLocales list of available locales. One of these will be returned.
 * @param fallback if non-null, a 1-element array containing a boolean to be set with
 * the fallback status
 * @return one of the locales from the availableLocales list, or null if none match
 * @stable ICU 3.4
 */

public static ULocale acceptLanguage(ULocale[] acceptLanguageList, ULocale[]
        availableLocales, boolean[] fallback) {
    // fallbacklist
    int i,j;
    if(fallback != null) {
        fallback[0]=true;
    }
    for(i=0;i<acceptLanguageList.length;i++) {
        ULocale aLocale = acceptLanguageList[i];
        boolean[] setFallback = fallback;
        do {
            for(j=0;j<availableLocales.length;j++) {
                if(availableLocales[j].equals(aLocale)) {
                    if(setFallback != null) {
                        setFallback[0]=false; // first time with this locale - not a fallback.
                    }
                    return availableLocales[j];
                }
                // compare to scriptless alias, so locales such as
                // zh_TW, zh_CN are considered as available locales - see #7190
                if (aLocale.getScript().length() == 0
                        && availableLocales[j].getScript().length() > 0
                        && availableLocales[j].getLanguage().equals(aLocale.getLanguage())
                        && availableLocales[j].getCountry().equals(aLocale.getCountry())
                        && availableLocales[j].getVariant().equals(aLocale.getVariant())) {
                    ULocale minAvail = ULocale.minimizeSubtags(availableLocales[j]);
                    if (minAvail.getScript().length() == 0) {
                        if(setFallback != null) {
                            setFallback[0] = false; // not a fallback.
                        }
                        return aLocale;
                    }
                }
            }
            Locale loc = aLocale.toLocale();
            Locale parent = LocaleUtility.fallback(loc);
            if(parent != null) {
                aLocale = new ULocale(parent);
            } else {
                aLocale = null;
            }
            setFallback = null; // Do not set fallback in later iterations
        } while (aLocale != null);
    }
    return null;
}
 
Example #3
Source File: LocaleService.java    From singleton with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * A function to convert a string of the form aa_BB_CC to a locale object
 *
 * @param locale
 *            A string representing a specific locale in [lang]_[country
 *            (region)] format. e.g., ja_JP, zh_CN
 * @return Returns a well-formed IETF BCP 47 language tag representing this
 *         locale
 */
public String getLocaleFromName(String locale) {
	return LocaleUtility.getLocaleFromName(locale).toLanguageTag();
}