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

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

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: apply ICU instead of self-implementing 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..1e08d920cd4556af31a5c83c37870b9d3c29fb84 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,73 @@ 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.
+ **/
+ public static String languageAdjust(String language) {
+ // 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)) {
- 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;
+ }
+ }
+
+ /**
+ * @return the locale that best represents the language tag that has language and country code.
+ */
+ public static Locale forLanguageTag(String languageTag) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ Locale locale = Locale.forLanguageTag(languageTag);
+ return locale;
+ } else {
+ String language;
+ String country;
+ if (languageTag.length() == 2 || languageTag.length() == 3) {
+ language = languageAdjust(languageTag);
+ return new Locale(language);
+ } else if (languageTag.charAt(2) == '-') {
+ language = languageAdjust(languageTag.substring(0, 2));
+ country = languageTag.substring(3);
+ } else { // length of the language code is 3.
+ language = languageAdjust(languageTag.substring(0, 3));
+ country = languageTag.substring(4);
+ }
+ return new Locale(language, country);
+ }
+ }
+
+ /**
+ * @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();
+ } else {
+ return getLocale(locale);
}
- return country.isEmpty() ? language : language + "-" + country;
}
/**

Powered by Google App Engine
This is Rietveld 408576698