| Index: base/android/java/src/org/chromium/base/LocaleUtils.java
|
| diff --git a/base/android/java/src/org/chromium/base/LocaleUtils.java b/base/android/java/src/org/chromium/base/LocaleUtils.java
|
| index 35d04a9b73b8e337f8469420e4141343810b2ef3..d9014f1addd574ce642881daee637b7ef526a232 100644
|
| --- a/base/android/java/src/org/chromium/base/LocaleUtils.java
|
| +++ b/base/android/java/src/org/chromium/base/LocaleUtils.java
|
| @@ -6,7 +6,10 @@ package org.chromium.base;
|
|
|
| import org.chromium.base.annotations.CalledByNative;
|
|
|
| +import java.util.Collections;
|
| +import java.util.HashMap;
|
| import java.util.Locale;
|
| +import java.util.Map;
|
|
|
| /**
|
| * This class provides the locale related methods.
|
| @@ -18,45 +21,91 @@ public class LocaleUtils {
|
| private LocaleUtils() {
|
| }
|
|
|
| + private static final Map<String, String> LANGUAGE_MAP;
|
| +
|
| + static {
|
| + HashMap<String, String> map = new HashMap<>();
|
| + map.put("iw", "he"); // Hebrew
|
| + map.put("ji", "yi"); // Yiddish
|
| + map.put("in", "id"); // Indonesian
|
| + map.put("tl", "fil"); // Filipino
|
| + LANGUAGE_MAP = Collections.unmodifiableMap(map);
|
| + }
|
| +
|
| /**
|
| - * @return The string for the given locale, translating Android deprecated language codes
|
| - * into the modern ones used by Chromium.
|
| + * Handle the special cases in converting a language code/region code pair into an ISO-639-1
|
| + * language tag. When a language code used for Chromium is different from the language code
|
| + * in Android, update that language code for Chromium.
|
| + * @return a updated language code for Chromium if necessary.
|
| + **/
|
| + private static String updateLanguageForChromium(String language) {
|
| + String updatedLanguageCode = LANGUAGE_MAP.get(language);
|
| + return updatedLanguageCode == null ? language : updatedLanguageCode;
|
| + }
|
| +
|
| + /**
|
| + * This function creates Locale object from xx-XX style string where xx is language code
|
| + * and XX is a country code.
|
| + * @return the locale that best represents the language tag.
|
| */
|
| - public static String getLocale(Locale locale) {
|
| - String language = getLanguage(locale);
|
| - String country = locale.getCountry();
|
| + public static Locale forLanguageTag(String languageTag) {
|
| + if (languageTag.length() == 0) {
|
| + return new Locale("");
|
| + }
|
| + String[] tag = languageTag.split("-");
|
| + String language = updateLanguageForChromium(tag[0]);
|
| + if (language.length() != 2 && language.length() != 3) {
|
| + return new Locale("");
|
| + }
|
| + if (tag.length == 1) {
|
| + return new Locale(language);
|
| + }
|
| + String country = tag[1];
|
| + if (country.length() != 2 && country.length() != 3) {
|
| + return new Locale(language);
|
| + }
|
| + return new Locale(language, country);
|
| + }
|
|
|
| - return country.isEmpty() ? language : language + "-" + country;
|
| + /**
|
| + * Android uses deprecated lanuages codes for Hebrew, Yiddish and Indonesian but
|
| + * Chromium uses the updated codes. Also, Android uses "tl" while Chromium uses "fil"
|
| + * for Tagalog/Filipino. So apply a mapping.
|
| + * See http://developer.android.com/reference/java/util/Locale.html
|
| + * @return a updated language code for Chromium with given locale.
|
| + */
|
| + public static String getLocaleLanguage(Locale locale) {
|
| + return updateLanguageForChromium(locale.getLanguage());
|
| }
|
|
|
| /**
|
| - * @return The language for the given locale, translating Android deprecated languages codes
|
| - * into modern ones used by Chromium.
|
| + * Converts Locale object to the BC47 compliant string format.
|
| + *
|
| + * Java keeps deprecated language codes for Hebrew, Yiddish and Indonesian but Chromium uses
|
| + * updated ones. Similarly, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino.
|
| + * Note that we cannot use Locale.getLanguage() and Locale.toLanguageTag() for this purpose.
|
| + * Since Locale.getLanguage() returns deprecated language code even if the Locale object is
|
| + * constructed with updated language code. As for Locale.toLanguageTag(), it does a special
|
| + * conversion from deprecated language code to updated one, but it is not usable because this
|
| + * special conversion is not working Android M or before.
|
| + * @return a well-formed IETF BCP 47 language tag with language and country code that
|
| + * represents this locale.
|
| */
|
| - public static String getLanguage(Locale locale) {
|
| - String language = locale.getLanguage();
|
| + public static String getLocaleString(Locale locale) {
|
| + String language = updateLanguageForChromium(locale.getLanguage());
|
| + String country = locale.getCountry();
|
|
|
| - // Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the
|
| - // updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino.
|
| - // So apply a mapping.
|
| - // See http://developer.android.com/reference/java/util/Locale.html
|
| - if ("iw".equals(language)) {
|
| - language = "he";
|
| - } else if ("in".equals(language)) {
|
| - language = "id";
|
| - } else if ("tl".equals(language)) {
|
| - language = "fil";
|
| - }
|
| - return language;
|
| + return country.isEmpty() ? language : language + "-" + country;
|
| }
|
|
|
| /**
|
| - * @return The default locale, translating Android deprecated language codes into the modern
|
| - * ones used by Chromium.
|
| + * @return a well-formed IETF BCP 47 language tag with language and country code that
|
| + * represents a default locale.
|
| */
|
| @CalledByNative
|
| - public static String getDefaultLocale() {
|
| - return getLocale(Locale.getDefault());
|
| + public static String getDefaultLocaleString() {
|
| + Locale locale = Locale.getDefault();
|
| + return getLocaleString(locale);
|
| }
|
|
|
| /**
|
|
|