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

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

Issue 2393673003: Support multiple locales string for accept language list (Closed)
Patch Set: Fixing Prepend Accepting List for Locale List 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 4ddaa35f57066297b40d4ccac5e6c67181670552..c86925d2a649b3df3652b16c87903ccd2fa0b35e 100644
--- a/chrome/browser/android/preferences/pref_service_bridge.cc
+++ b/chrome/browser/android/preferences/pref_service_bridge.cc
@@ -1063,7 +1063,6 @@ static void ResetAcceptLanguages(JNIEnv* env,
const JavaParamRef<jstring>& default_locale) {
std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES));
std::string locale_string(ConvertJavaStringToUTF8(env, default_locale));
-
PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string,
&accept_languages);
GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages);
@@ -1147,39 +1146,62 @@ bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
const std::string& locale,
std::string* accept_languages) {
- if (locale.size() != 5u || locale[2] != '_') // not well-formed
- return;
-
- std::string language(locale.substr(0, 2));
- std::string region(locale.substr(3, 2));
-
- // Java mostly follows ISO-639-1 and ICU, except for the following three.
- // See documentation on java.util.Locale constructor for more.
- if (language == "iw") {
- language = "he";
- } else if (language == "ji") {
- language = "yi";
- } else if (language == "in") {
- language = "id";
+ std::vector<std::string> lans;
Seigo Nonaka 2016/10/05 07:15:41 probably, languages or langs are preferred.
Yirui Huang 2016/10/06 01:17:59 Done.
+ std::string languages_to_check = locale;
+ // Check see if it is a Locale List or single Locale
+ if (languages_to_check.length() > 5) {
Seigo Nonaka 2016/10/05 07:15:41 Looks like this is not a good condition for checki
Yirui Huang 2016/10/06 01:17:59 Done.
+ while (languages_to_check.find(",") != std::string::npos) {
+ std::size_t bfound = 0;
+ std::size_t efound = languages_to_check.find(",");
+ lans.push_back(languages_to_check.substr(bfound, efound));
+ bfound = efound + 1;
+ languages_to_check = languages_to_check.substr(bfound);
Seigo Nonaka 2016/10/05 07:15:41 Since languages_to_check is a std::string, you cre
Yirui Huang 2016/10/06 01:17:59 Done.
+ // Check the last element
+ if (languages_to_check.length() <= 5) {
Seigo Nonaka 2016/10/05 07:15:41 Similarly, languages_to_check can be still list ev
Yirui Huang 2016/10/06 01:17:59 Done.
+ lans.push_back(languages_to_check);
+ break;
+ }
+ }
+ } else {
+ lans.push_back(languages_to_check);
}
- std::string language_region(language + "-" + region);
-
- if (accept_languages->find(language_region) == std::string::npos) {
- std::vector<std::string> parts;
- parts.push_back(language_region);
- // If language is not in the accept languages list, also add language code.
- // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
- // with Chrome but may fail on arbitrary lists of language tags due to
- // differences in case and whitespace.
- if (accept_languages->find(language + ",") == std::string::npos &&
- !std::equal(language.rbegin(), language.rend(),
- accept_languages->rbegin())) {
- parts.push_back(language);
+ std::vector<std::string> parts;
+ for (std::size_t i = 0; i < lans.size(); i++) {
+ if (lans[i].size() != 5u || lans[i][2] != '_') // not well-formed
+ continue;
+ std::string language(lans[i].substr(0, 2));
+ std::string region(lans[i].substr(3, 2));
+
+ // Java mostly follows ISO-639-1 and ICU, except for the following three.
+ // See documentation on java.util.Locale constructor for more.
+ if (language == "iw") {
+ language = "he";
+ } else if (language == "ji") {
+ language = "yi";
+ } else if (language == "in") {
+ language = "id";
+ }
+
+ std::string language_region(language + "-" + region);
+
+ if (accept_languages->find(language_region) == std::string::npos) {
+ parts.push_back(language_region);
+ // If language is not in the accept languages list, also add language
+ // code.
+ // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
+ // with Chrome but may fail on arbitrary lists of language tags due to
+ // differences in case and whitespace.
+ if ((accept_languages->find(language) == std::string::npos ||
+ accept_languages->find(language + ",") == std::string::npos) &&
+ !std::equal(language.rbegin(), language.rend(),
+ accept_languages->rbegin())) {
+ parts.push_back(language);
+ }
}
- parts.push_back(*accept_languages);
- *accept_languages = base::JoinString(parts, ",");
}
+ parts.push_back(*accept_languages);
+ *accept_languages = base::JoinString(parts, ",");
}
// static

Powered by Google App Engine
This is Rietveld 408576698