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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: rebased to 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: chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java
index c618d54f9f1d96765077dcc65d5b6d3fc252bca9..8b95364b502f3c74cb1267777f76db120eee2625 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/physicalweb/PwsClientImpl.java
@@ -14,6 +14,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import org.chromium.base.LocaleUtils;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
@@ -29,7 +30,6 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.HashSet;
-import java.util.Locale;
/**
* This class sends requests to the Physical Web Service.
@@ -227,7 +227,7 @@ class PwsClientImpl implements PwsClient {
*/
@VisibleForTesting
String getAcceptLanguage() {
- String defaultLocale = Locale.getDefault().toString();
+ String defaultLocale = LocaleUtils.getDefaultLocale();
if (sDefaultLocale == null || !sDefaultLocale.equals(defaultLocale)) {
String acceptLanguages = mContext.getResources().getString(R.string.accept_languages);
acceptLanguages = prependToAcceptLanguagesIfNecessary(defaultLocale, acceptLanguages);
@@ -266,7 +266,8 @@ class PwsClientImpl implements PwsClient {
* Get the language code for the default locale and prepend it to the Accept-Language string if
* it isn't already present. The logic should match PrependToAcceptLanguagesIfNecessary in
* chrome/browser/android/preferences/pref_service_bridge.cc
- * @param locales A string representing a default locale or a list of default locales.
+ * @param locales A string representing a default language tag or a list of default language
+ * tags.
* @param acceptLanguages The default language list for the language of the user's locale.
* @return An updated language list.
*/
@@ -278,22 +279,30 @@ class PwsClientImpl implements PwsClient {
HashSet<String> seenLocales = new HashSet<>();
ArrayList<String> uniqueList = new ArrayList<>();
for (String locale : localeList) {
- // TODO(yirui): Support BCP47 compliant format including 3-letter country code,
- // '-' separator and missing country case.
- if (locale.length() != 5 || (locale.charAt(2) != '_' && locale.charAt(2) != '-')) {
- // Skip checking not well formed locales.
+ String language;
Seigo Nonaka 2016/10/11 11:29:28 You can use Locale.forLanguageTag here like for (
Yirui Huang 2016/10/11 12:17:32 Done.
+ String country;
+ if (locale.length() == 2 || locale.length() == 3) {
+ language = locale;
+ country = "";
+ } else if (locale.charAt(2) == '-') {
+ language = locale.substring(0, 2);
+ country = locale.substring(3);
+ } else if (locale.charAt(3) == '-') {
+ language = locale.substring(0, 3);
+ country = locale.substring(4);
+ } else {
continue;
}
- String language = locale.substring(0, 2);
- String country = locale.substring(3);
String languageTag = makeLanguageTag(language, country);
if (seenLocales.contains(languageTag)) {
continue;
}
+ if (country != "") {
Seigo Nonaka 2016/10/11 11:29:28 Please use !country.isEmpty() here.
Yirui Huang 2016/10/11 12:17:32 Done.
+ seenLocales.add(languageTag);
+ }
uniqueList.add(languageTag);
- seenLocales.add(languageTag);
}
// If language is not in the accept languages list, also add language code.
@@ -306,12 +315,23 @@ class PwsClientImpl implements PwsClient {
ArrayList<String> outputList = new ArrayList<>();
for (int i = uniqueList.size() - 1; i >= 0; i--) {
String localeAdd = uniqueList.get(i);
- String languageAdd = localeAdd.substring(0, 2);
+ int lastChar = localeAdd.length() - 1;
+ String languageAdd;
+ if (localeAdd.charAt(lastChar) == '-') {
+ languageAdd = localeAdd.substring(0, lastChar);
+ } else if (localeAdd.charAt(2) == '-') {
+ languageAdd = localeAdd.substring(0, 2);
+ } else { // length of the language code is 3.
+ languageAdd = localeAdd.substring(0, 3);
+ }
+
if (!seenLanguages.contains(languageAdd)) {
seenLanguages.add(languageAdd);
outputList.add(languageAdd);
}
- outputList.add(localeAdd);
+ if (lastChar != languageAdd.length()) {
+ outputList.add(localeAdd);
+ }
}
Collections.reverse(outputList);
return TextUtils.join(",", outputList);

Powered by Google App Engine
This is Rietveld 408576698