Chromium Code Reviews| 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..2be05fa0476a425d0bcb2ed8bdfd751bc84ab3db 100644 |
| --- a/base/android/java/src/org/chromium/base/LocaleUtils.java |
| +++ b/base/android/java/src/org/chromium/base/LocaleUtils.java |
| @@ -4,9 +4,14 @@ |
| package org.chromium.base; |
| +import android.os.Build; |
| + |
| 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,36 +23,104 @@ public class LocaleUtils { |
| private LocaleUtils() { |
| } |
| + // 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 |
| + 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. |
| + * @return the string for the given locale with language and/or country code, translating |
|
ksk1
2016/10/17 11:34:57
"or" sounds strange
Yirui Huang
2016/10/17 13:42:27
Done.
|
| + * Android deprecated language codes into the modern ones used by Chromium. |
| */ |
| public static String getLocale(Locale locale) { |
| - String language = getLanguage(locale); |
| + String language = updateLanguageForChromium(locale.getLanguage()); |
| String country = locale.getCountry(); |
| return country.isEmpty() ? language : language + "-" + country; |
| } |
| /** |
| - * @return The language for the given locale, translating Android deprecated languages codes |
| - * into 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. |
| + **/ |
| + public static String updateLanguageForChromium(String language) { |
| + String updatedLanguageCode = LANGUAGE_MAP.get(language); |
| + return updatedLanguageCode == null ? language : updatedLanguageCode; |
| + } |
| + |
| + /** |
| + * This function can be used for alternative of Locale.forLanguageTag before API Level 21. |
| + * It creates Locale object from xx-XX style string where xx is language code and XX is |
| + * a country code. |
| + * @return the best representative locale with the language tag that has language and/or |
| + * country code. |
| + */ |
| + @VisibleForTesting |
| + static Locale forLanguageTagCompat(String languageTag) { |
|
Seigo Nonaka
2016/10/17 11:30:46
Please put public if this is accessed from test co
Yirui Huang
2016/10/17 13:42:27
Done.
|
| + if (languageTag.length() == 0) { |
| + return new Locale(""); |
| + } |
| + String[] tag = languageTag.split("-"); |
| + String language = tag[0]; |
|
ksk1
2016/10/17 11:21:16
String language = updateLanguageForChromium(tag[0]
Yirui Huang
2016/10/17 13:42:27
Done.
|
| + if (language.length() != 2 && language.length() != 3) { |
| + return new Locale(""); |
| + } |
| + if (tag.length == 1) { |
| + language = updateLanguageForChromium(languageTag); |
| + return new Locale(language); |
| + } |
| + String country = tag[1]; |
| + if (country.length() != 2 && country.length() != 3) { |
| + return new Locale(language); |
| + } |
| + return new Locale(language, country); |
| + } |
| + |
| + /** |
| + * It creates Locale object from xx-XX style string where xx is language code and XX is |
|
Seigo Nonaka
2016/10/17 11:30:46
"This function creates Locale object" or simply "C
Yirui Huang
2016/10/17 13:42:27
Done.
|
| + * a country code. |
| + * @return the locale that best represents the language tag that has language and/or |
| + * country code. |
| + */ |
| + public static Locale forLanguageTag(String languageTag) { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| + String[] tag = languageTag.split("-"); |
| + String updatedLanguage = updateLanguageForChromium(tag[0]); |
| + if (tag.length == 1) { |
| + return Locale.forLanguageTag(updatedLanguage); |
| + } |
| + return Locale.forLanguageTag(updatedLanguage + "-" + tag[1]); |
| + } |
| + return LocaleUtils.forLanguageTagCompat(languageTag); |
| + } |
| + |
| + /** |
| + * @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(); |
| - |
| - // 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"; |
| + public static String toLanguageTag(Locale locale) { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| + String languageTag = locale.toLanguageTag(); |
| + String[] tag = languageTag.split("-"); |
|
Seigo Nonaka
2016/10/17 11:30:46
Please do not split with hyphen here since there c
Yirui Huang
2016/10/17 13:42:27
Done.
|
| + String updatedLanguageTag = updateLanguageForChromium(tag[0]); |
| + if (tag.length == 1) { |
| + return updatedLanguageTag; |
| + } |
| + return updatedLanguageTag + "-" + tag[1]; |
| } |
| - return language; |
| + return getLocale(locale); |
| } |
| /** |