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

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

Issue 2481293004: Use LocaleList in Android/Webview locale representation (Closed)
Patch Set: description changes Created 4 years, 1 month 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
« no previous file with comments | « base/BUILD.gn ('k') | base/android/javatests/src/org/chromium/base/LocaleUtilsTest.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 31e5c4294beafcc99f3d48eed1675e93cd7dd720..04cbd8f9aae8fbadadee0e6767e29fb7128d371e 100644
--- a/base/android/java/src/org/chromium/base/LocaleUtils.java
+++ b/base/android/java/src/org/chromium/base/LocaleUtils.java
@@ -6,9 +6,12 @@ package org.chromium.base;
import android.annotation.TargetApi;
import android.os.Build;
+import android.os.LocaleList;
+import android.text.TextUtils;
import org.chromium.base.annotations.CalledByNative;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
@@ -60,7 +63,8 @@ public class LocaleUtils {
* codes used by Chromium.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private static Locale getUpdatedLocaleForChromium(Locale locale) {
+ @VisibleForTesting
+ public static Locale getUpdatedLocaleForChromium(Locale locale) {
String languageForChrome = LANGUAGE_MAP_FOR_CHROMIUM.get(locale.getLanguage());
if (languageForChrome == null) {
return locale;
@@ -84,7 +88,8 @@ public class LocaleUtils {
* codes used by Chromium.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
- private static Locale getUpdatedLocaleForAndroid(Locale locale) {
+ @VisibleForTesting
+ public static Locale getUpdatedLocaleForAndroid(Locale locale) {
String languageForAndroid = LANGUAGE_MAP_FOR_ANDROID.get(locale.getLanguage());
if (languageForAndroid == null) {
return locale;
@@ -93,7 +98,7 @@ public class LocaleUtils {
}
/**
- * This function creates Locale object from xx-XX style string where xx is language code
+ * This function creates a Locale object from xx-XX style string where xx is language code
* and XX is a country code. This works for API level lower than 21.
* @return the locale that best represents the language tag.
*/
@@ -117,7 +122,7 @@ public class LocaleUtils {
}
/**
- * This function creates Locale object from xx-XX style string where xx is language code
+ * This function creates a 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.
*/
@@ -132,10 +137,16 @@ public class LocaleUtils {
/**
* Converts Locale object to the BCP 47 compliant string format.
* This works for API level lower than 24.
+ *
+ * Note that for Android M or before, 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 only usable for Android N or after.
* @return a well-formed IETF BCP 47 language tag with language and country code that
* represents this locale.
*/
- public static String toLanguageTagCompat(Locale locale) {
+ public static String toLanguageTag(Locale locale) {
String language = getUpdatedLanguageForChromium(locale.getLanguage());
String country = locale.getCountry();
if (language.equals("no") && country.equals("NO") && locale.getVariant().equals("NY")) {
@@ -145,33 +156,32 @@ public class LocaleUtils {
}
/**
- * Converts Locale object to the BCP 47 compliant string format.
+ * Converts LocaleList object to the comma separated BCP 47 compliant string format.
*
- * Note that for Android M or before, 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 only usable for Android N or after.
* @return a well-formed IETF BCP 47 language tag with language and country code that
- * represents this locale.
+ * represents this locale list.
*/
- public static String toLanguageTag(Locale locale) {
- // TODO(yirui): use '>= N' once SDK is updated to include the version code
- if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
- locale = getUpdatedLocaleForChromium(locale);
- return locale.toLanguageTag();
+ @TargetApi(Build.VERSION_CODES.N)
+ public static String toLanguageTags(LocaleList localeList) {
+ ArrayList<String> newLocaleList = new ArrayList<>();
+ for (int i = 0; i < localeList.size(); i++) {
+ Locale locale = getUpdatedLocaleForChromium(localeList.get(i));
+ newLocaleList.add(toLanguageTag(locale));
}
- return toLanguageTagCompat(locale);
+ return TextUtils.join(",", newLocaleList);
}
/**
- * @return a well-formed IETF BCP 47 language tag with language and country code that
- * represents a default locale.
+ * @return a comma separated language tags string that represents a default locale or locales.
+ * Each language tag is well-formed IETF BCP 47 language tag with language and country
+ * code.
*/
@CalledByNative
public static String getDefaultLocaleString() {
- Locale locale = Locale.getDefault();
- return toLanguageTag(locale);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+ return toLanguageTags(LocaleList.getAdjustedDefault());
+ }
+ return toLanguageTag(Locale.getDefault());
}
/**
« no previous file with comments | « base/BUILD.gn ('k') | base/android/javatests/src/org/chromium/base/LocaleUtilsTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698