Chromium Code Reviews| 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..99284ba07425c50d1fd76c466c685a8188bd8d2b 100644 |
| --- a/chrome/browser/android/preferences/pref_service_bridge.cc |
| +++ b/chrome/browser/android/preferences/pref_service_bridge.cc |
| @@ -1147,10 +1147,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,14 +1160,21 @@ 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)); |
| + std::string lang_code; |
| + std::string country_code; |
| + if (locale_str.size() == 2u || locale_str.size() == 3u) { |
| + // Language tag consists of language only |
| + lang_code = locale_str; |
| + country_code = ""; |
| + } else if (locale_str[2] == '-') { |
| + lang_code = locale_str.substr(0, 2); |
| + country_code = locale_str.substr(3); |
| + } else if (locale_str[3] == '-') { |
| + lang_code = locale_str.substr(0, 3); |
| + country_code = locale_str.substr(4); |
| + } else { |
| + continue; |
| + } |
| // Java mostly follows ISO-639-1 and ICU, except for the following three. |
| // See documentation on java.util.Locale constructor for more. |
| @@ -1183,8 +1189,9 @@ void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( |
| if (seen_tags.find(language_tag) != seen_tags.end()) |
| continue; |
| + if (country_code != "") |
|
Seigo Nonaka
2016/10/11 11:29:28
Please use !country_code.empty() here.
Yirui Huang
2016/10/11 12:17:32
Done.
|
| + seen_tags.insert(language_tag); |
| unique_locale_list.push_back(std::make_pair(lang_code, country_code)); |
| - seen_tags.insert(language_tag); |
| } |
| // If language is not in the accept languages list, also add language |
| @@ -1201,7 +1208,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 != "") |
|
Seigo Nonaka
2016/10/11 11:29:28
ditto.
Yirui Huang
2016/10/11 12:17:32
Done.
|
| + output_list.push_back(it->first + "-" + it->second); |
| } |
| std::reverse(output_list.begin(), output_list.end()); |