Java Code Examples for java.util.Locale#getISO3Language()

The following examples show how to use java.util.Locale#getISO3Language() . These examples are extracted from open source projects. 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 Project: android_9.0.0_r45   File: TtsEngines.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the old-style string form of the locale. It consists of 3 letter codes:
 * <ul>
 *   <li>"ISO 639-2/T language code" if the locale has no country entry</li>
 *   <li> "ISO 639-2/T language code{@link #LOCALE_DELIMITER}ISO 3166 country code"
 *     if the locale has no variant entry</li>
 *   <li> "ISO 639-2/T language code{@link #LOCALE_DELIMITER}ISO 3166 country
 *     code{@link #LOCALE_DELIMITER}variant" if the locale has a variant entry</li>
 * </ul>
 * If we fail to generate those codes using {@link Locale#getISO3Country()} and
 * {@link Locale#getISO3Language()}, then we return new String[]{"eng","USA",""};
 */
static public String[] toOldLocaleStringFormat(Locale locale) {
    String[] ret = new String[]{"","",""};
    try {
        // Note that the default locale might have an empty variant
        // or language.
        ret[0] = locale.getISO3Language();
        ret[1] = locale.getISO3Country();
        ret[2] = locale.getVariant();

        return ret;
    } catch (MissingResourceException e) {
        // Default locale does not have a ISO 3166 and/or ISO 639-2/T codes. Return the
        // default "eng-usa" (that would be the result of Locale.getDefault() == Locale.US).
        return new String[]{"eng","USA",""};
    }
}
 
Example 2
private static boolean isValid(@NonNull Locale locale) {
  try {
    return locale.getISO3Language() != null && locale.getISO3Country() != null;
  } catch (Exception ex) {
    return false;
  }
}
 
Example 3
private String getMenuLanguage() {
    Locale locale = Locale.getDefault();
    if (locale.equals(Locale.TAIWAN) || locale.equals(HONG_KONG) || locale.equals(MACAU)) {
        // Android always returns "zho" for all Chinese variants.
        // Use "bibliographic" code defined in CEC639-2 for traditional
        // Chinese used in Taiwan/Hong Kong/Macau.
        return "chi";
    } else {
        return locale.getISO3Language();
    }
}
 
Example 4
@VisibleForTesting
static boolean isValidLangauge(String lang) {
  String[] parts = lang.split("_", 3);

  if (parts.length != 2) return false;

  try {
    final Locale loc = new Locale(parts[0], parts[1]);

    return (loc.getISO3Language() != null) && (loc.getISO3Country() != null);
  } catch (MissingResourceException e) {
    return false;
  }
}
 
Example 5
Source Project: Stringlate   File: LocaleString.java    License: MIT License 5 votes vote down vote up
private static boolean isValid(final Locale locale) {
    try {
        return locale.getISO3Language() != null && locale.getISO3Country() != null;
    } catch (MissingResourceException ignored) {
        return false;
    }
}
 
Example 6
Source Project: android_9.0.0_r45   File: TtsEngines.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Parses a locale encoded as a string, and tries its best to return a valid {@link Locale}
 * object, even if the input string is encoded using the old-style 3 character format e.g.
 * "deu-deu". At the end, we test if the resulting locale can return ISO3 language and
 * country codes ({@link Locale#getISO3Language()} and {@link Locale#getISO3Country()}),
 * if it fails to do so, we return null.
 */
public Locale parseLocaleString(String localeString) {
    String language = "", country = "", variant = "";
    if (!TextUtils.isEmpty(localeString)) {
        String[] split = localeString.split(
                "[" + LOCALE_DELIMITER_OLD + LOCALE_DELIMITER_NEW + "]");
        language = split[0].toLowerCase();
        if (split.length == 0) {
            Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object. Only" +
                        " separators");
            return null;
        }
        if (split.length > 3) {
            Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object. Too" +
                    " many separators");
            return null;
        }
        if (split.length >= 2) {
            country = split[1].toUpperCase();
        }
        if (split.length >= 3) {
            variant = split[2];
        }

    }

    String normalizedLanguage = sNormalizeLanguage.get(language);
    if (normalizedLanguage != null) {
        language = normalizedLanguage;
    }

    String normalizedCountry= sNormalizeCountry.get(country);
    if (normalizedCountry != null) {
        country = normalizedCountry;
    }

    if (DBG) Log.d(TAG, "parseLocalePref(" + language + "," + country +
            "," + variant +")");

    Locale result = new Locale(language, country, variant);
    try {
        result.getISO3Language();
        result.getISO3Country();
        return result;
    } catch(MissingResourceException e) {
        Log.w(TAG, "Failed to convert " + localeString + " to a valid Locale object.");
        return null;
    }
}
 
Example 7
Source Project: sis   File: ValueConverter.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Converts the given locale to a language code. For better compliance with ISO standards, the language code
 * should be a 3-letters ISO 639-2 code (e.g. {@code "jpn"} for {@linkplain Locale#JAPANESE Japanese}).
 * However those codes may not be available for every locales.
 *
 * <p>The default implementation performs the following steps:</p>
 * <ul>
 *   <li>Try {@link Locale#getISO3Language()}:<ul>
 *     <li>On success, return that value if non-empty, or {@code null} otherwise.</li>
 *     <li>If an exception has been thrown, then:<ul>
 *       <li>If {@link #exceptionOccured exceptionOccured(…)} return {@code true}, then
 *           returns {@code value.getLanguage()} if non-empty or {@code null} otherwise.</li>
 *       <li>If {@code exceptionOccured(…)} returned {@code false} (which is the default
 *           behavior), then let the exception propagate.</li>
 *     </ul></li>
 *   </ul></li>
 * </ul>
 *
 * @param  context  context (GML version, locale, <i>etc.</i>) of the (un)marshalling process.
 * @param  value    the locale to convert to a language code, or {@code null}.
 * @return the language code, or {@code null} if the given value was null or does not contains a language code.
 * @throws MissingResourceException if no language code can be found for the given locale.
 *
 * @see Locale#getISO3Language()
 * @see Locale#getLanguage()
 */
public String toLanguageCode(final MarshalContext context, final Locale value) throws MissingResourceException {
    if (value != null) {
        String code;
        try {
            code = value.getISO3Language();
        } catch (MissingResourceException e) {
            if (!exceptionOccured(context, value, Locale.class, String.class, e)) {
                throw e;
            }
            code = value.getLanguage();
        }
        if (!code.isEmpty()) {
            return code;
        }
    }
    return null;
}