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

Unified Diff: chrome/browser/android/preferences/pref_service_bridge.cc

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: chrome/browser/android/preferences/pref_service_bridge.cc
diff --git a/chrome/browser/android/preferences/pref_service_bridge.cc b/chrome/browser/android/preferences/pref_service_bridge.cc
index 4a3de8f5e1d8bb5088ed41c613fbdc95f601ea8a..87ca23f0ca20f7be43da98c0e6d679826dbfae83 100644
--- a/chrome/browser/android/preferences/pref_service_bridge.cc
+++ b/chrome/browser/android/preferences/pref_service_bridge.cc
@@ -63,8 +63,10 @@
#include "components/web_resource/web_resource_pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "jni/PrefServiceBridge_jni.h"
+#include "third_party/icu/source/common/unicode/uloc.h"
#include "ui/base/l10n/l10n_util.h"
+
ksk1 2016/10/12 06:10:24 remove
Yirui Huang 2016/10/12 09:02:04 Done.
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::ConvertJavaStringToUTF8;
@@ -1147,10 +1149,9 @@ bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
// This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in
// chrome/android/java/src/org/chromium/chrome/browser/
// physicalweb/PwsClientImpl.java
-// Input |locales| is a comma separated locale representaion. Each locale
-// representation should be xx_XX style, where xx is a 2-letter
-// ISO 639-1 compliant language code and XX is a 2-letter
-// ISO 3166-1 compliant country code.
+// Input |locales| is a comma separated locale representation that consists of
+// language tags(BCP47 compliant format). Each language tag contains
+// a language code and a country code or a language code only.
void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
const std::string& locales,
std::string* accept_languages) {
@@ -1161,16 +1162,18 @@ void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
std::set<std::string> seen_tags;
std::vector<std::pair<std::string, std::string>> unique_locale_list;
for (const std::string& locale_str : locale_list) {
- // TODO(yirui): Support BCP47 compliant format including 3-letter
- // country code, '-' separator and missing country case.
- if (locale_str.size() != 5u ||
- (locale_str[2] != '_' && locale_str[2] != '-'))
- continue; // Skip not well formed locale.
-
- std::string lang_code(locale_str.substr(0, 2));
- std::string country_code(locale_str.substr(3, 2));
-
- // Java mostly follows ISO-639-1 and ICU, except for the following three.
+ char locale_ID[ULOC_FULLNAME_CAPACITY];
+ char language_code[ULOC_LANG_CAPACITY];
+ char country_code[ULOC_COUNTRY_CAPACITY];
+ UErrorCode error = U_ZERO_ERROR;
+ uloc_forLanguageTag(locale_str.c_str(), locale_ID, ULOC_FULLNAME_CAPACITY,
+ nullptr, &error);
ksk1 2016/10/12 06:10:24 wrong indent
Yirui Huang 2016/10/12 09:02:04 Done.
+ uloc_getLanguage(locale_ID, language_code, ULOC_LANG_CAPACITY, &error);
+ uloc_getCountry(locale_ID, country_code, ULOC_COUNTRY_CAPACITY, &error);
+ std::string lang_code(language_code);
+ std::string coun_code(country_code);
+
+ // Java mostly follows ISO-639-1 and ICU, except for the following four.
// See documentation on java.util.Locale constructor for more.
if (lang_code == "iw")
lang_code = "he";
@@ -1178,13 +1181,16 @@ void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
lang_code = "yi";
else if (lang_code == "in")
lang_code = "id";
+ else if (lang_code == "tl")
+ lang_code = "fil";
- std::string language_tag(lang_code + "-" + country_code);
+ std::string language_tag(lang_code + "-" + coun_code);
if (seen_tags.find(language_tag) != seen_tags.end())
continue;
- unique_locale_list.push_back(std::make_pair(lang_code, country_code));
- seen_tags.insert(language_tag);
+ if (coun_code != "")
+ seen_tags.insert(language_tag);
+ unique_locale_list.push_back(std::make_pair(lang_code, coun_code));
}
// If language is not in the accept languages list, also add language
@@ -1201,7 +1207,8 @@ void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
output_list.push_back(it->first);
seen_languages.insert(it->first);
}
- output_list.push_back(it->first + "-" + it->second);
+ if (!it->second.empty())
+ output_list.push_back(it->first + "-" + it->second);
}
std::reverse(output_list.begin(), output_list.end());

Powered by Google App Engine
This is Rietveld 408576698