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

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

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: rebase master, unit tests added for LocaleUtils 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 5c26e7a50c28618ad009b7499bd1954b44b4541c..f240e366dd822481ddcf3d19b555e4fbcf3d5e4c 100644
--- a/base/android/java/src/org/chromium/base/LocaleUtils.java
+++ b/base/android/java/src/org/chromium/base/LocaleUtils.java
@@ -4,6 +4,8 @@
package org.chromium.base;
+import android.os.Build;
+
import org.chromium.base.annotations.CalledByNative;
import java.util.Locale;
@@ -19,26 +21,85 @@ public class LocaleUtils {
}
/**
- * @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
+ * Android deprecated language codes into the modern ones used by Chromium.
*/
public static String getLocale(Locale locale) {
- String language = locale.getLanguage();
+ String language = languageAdjust(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.
+ return country.isEmpty() ? language : language + "-" + country;
+ }
+
+ /**
+ * Handle the special cases in converting a language code/region code pair into an ISO-639-1
+ * language tag.
+ * @return a updated language code if necessary.
+ **/
+ private static String languageAdjust(String language) {
Seigo Nonaka 2016/10/17 04:12:15 "adjust" sounds like bit ambiguous. How about migr
ksk1 2016/10/17 05:06:04 I think "tl" is not deprecated.
Yirui Huang 2016/10/17 10:40:51 Done.
Yirui Huang 2016/10/17 10:40:51 Done.
+ // 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
if ("iw".equals(language)) {
Seigo Nonaka 2016/10/17 04:12:15 optional: you can prepare static map for this purp
Yirui Huang 2016/10/17 10:40:51 changed using HashMap, but since this function is
Seigo Nonaka 2016/10/17 11:30:46 I don't think we need to expose this function. Ple
- language = "he";
+ return "he";
+ } else if ("ji".equals(language)) {
+ return "yi";
} else if ("in".equals(language)) {
- language = "id";
+ return "id";
} else if ("tl".equals(language)) {
- language = "fil";
+ return "fil";
+ } else {
+ return language;
+ }
+ }
+
+ /**
+ * This function does the same as Locale.forLanguageTag() when API level is lower than 21.
Seigo Nonaka 2016/10/17 04:12:15 forLanguageTag does more complicated things than s
Yirui Huang 2016/10/17 10:40:51 Done.
+ * @return the best representative locale with the language tag that has language and/or
+ * country code.
+ */
+ public static Locale forLanguageTagCompat(String languageTag) {
Seigo Nonaka 2016/10/17 04:12:15 This can be private? if this is public only for te
Yirui Huang 2016/10/17 10:40:52 Done.
+ if (languageTag.length() == 0) {
+ return new Locale("");
+ }
+ String[] tag = languageTag.split("-");
+ String language = tag[0];
+ if (language.length() != 2 && language.length() != 3) {
+ return new Locale("");
+ }
+ if (tag.length == 1) {
+ language = languageAdjust(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);
+ }
+
+ /**
+ * @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) {
+ return Locale.forLanguageTag(languageTag);
Seigo Nonaka 2016/10/17 04:12:15 Look like you don't migrate the deprecated languag
Yirui Huang 2016/10/17 10:40:51 It seems these three deprecated language codes are
Seigo Nonaka 2016/10/17 11:30:46 Okay thanks. Then, looks like Android can still re
+ }
+ 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 toLanguageTag(Locale locale) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ return locale.toLanguageTag();
Seigo Nonaka 2016/10/17 04:12:15 Same as above. Please check the deprecated languag
Yirui Huang 2016/10/17 10:40:51 Done.
+ } else {
Seigo Nonaka 2016/10/17 04:12:15 nit: you can drop else here.
Yirui Huang 2016/10/17 10:40:51 Done.
+ return getLocale(locale);
}
- return country.isEmpty() ? language : language + "-" + country;
}
/**

Powered by Google App Engine
This is Rietveld 408576698