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

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

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: 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..9b78f044863f410c70a019c73764fe13ef98e86d 100644
--- a/chrome/browser/android/preferences/pref_service_bridge.cc
+++ b/chrome/browser/android/preferences/pref_service_bridge.cc
@@ -63,6 +63,7 @@
#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"
using base::android::AttachCurrentThread;
@@ -1147,10 +1148,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 +1161,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);
+ 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);
ksk1 2016/10/17 05:06:04 Using both lang_code and language_code is confusin
Yirui Huang 2016/10/17 10:40:52 Done.
+ std::string coun_code(country_code);
+
ksk1 2016/10/17 05:06:04 if (error != U_ZERO_ERROR || lang_code.empty()) {
Yirui Huang 2016/10/17 10:40:52 U_FAILURE is used for checking.
+ // 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 +1180,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 != "")
ksk1 2016/10/17 05:06:04 !coun_code.empty()
Yirui Huang 2016/10/17 10:40:52 same as in Java side, not necessary, so removed ch
+ 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 +1206,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