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

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

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: format return in description 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..32fe7ce3337bc009a6dae31b0ebf73b0faa9098d 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,35 +21,84 @@ 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 = getLanguage(locale);
+ String language = languageAdjust(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.
- */
- 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.
+ * 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) {
+ // 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;
+ }
+ }
+
+ /**
+ * This function does the same as Locale.forLanguageTag() when API level is lower than 21.
+ * @return the best representative locale with the language tag that has language and/or
+ * country code.
+ */
+ public static Locale forLanguageTagCompat(String languageTag) {
+ 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);
+ }
+ 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();
}
- return language;
+ return getLocale(locale);
}
/**

Powered by Google App Engine
This is Rietveld 408576698