Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2099)

Unified Diff: base/android/java/src/org/chromium/base/LocaleUtils.java

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: cleanup LocaleUtils, update codes that are not reverted from "rebase master" Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..76089c5b9619e6586aa74483fe2ea505d60abbf1 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,81 @@ 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.
+ * 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) {
Bernhard Bauer 2016/10/20 12:44:27 Is there a reason you're not using Locale.forLangu
Yirui Huang 2016/10/21 08:24:02 reverted code, so Locale.forLanguageTag is used fo
+ if (languageTag.length() == 0) {
+ return new Locale("");
+ }
+ String[] tag = languageTag.split("-");
+ String updatedLanguage = LANGUAGE_MAP.get(tag[0]);
+ String language = updatedLanguage == null ? tag[0] : updatedLanguage;
+ 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;
+ /**
+ * 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.
+ * So apply a mapping here.
+ * 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) {
+ String originalLanguageCode = locale.getLanguage();
+ String updatedLanguageCode = LANGUAGE_MAP.get(originalLanguageCode);
Bernhard Bauer 2016/10/20 12:44:27 You could extract this (return the override from t
Yirui Huang 2016/10/21 08:24:02 This function is extracted as updateLanguageForChr
+ return updatedLanguageCode == null ? originalLanguageCode : updatedLanguageCode;
}
/**
- * @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.
+ *
+ * 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.
Bernhard Bauer 2016/10/20 12:44:27 Could you have a runtime check that does use Local
Yirui Huang 2016/10/21 08:24:02 Done.
+ * @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 = getLocaleLanguage(locale);
+ 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);
}
/**

Powered by Google App Engine
This is Rietveld 408576698