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

Side by Side Diff: chrome/browser/android/preferences/pref_service_bridge.cc

Issue 2406203002: Use BCP47 compliant format for locale representation (Closed)
Patch Set: create to/forLanguageTag in LocaleUtils 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/preferences/pref_service_bridge.h" 5 #include "chrome/browser/android/preferences/pref_service_bridge.h"
6 6
7 #include <jni.h> 7 #include <jni.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after
1140 1140
1141 // static 1141 // static
1142 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) { 1142 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
1143 return RegisterNativesImpl(env); 1143 return RegisterNativesImpl(env);
1144 } 1144 }
1145 1145
1146 // static 1146 // static
1147 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in 1147 // This logic should be kept in sync with prependToAcceptLanguagesIfNecessary in
1148 // chrome/android/java/src/org/chromium/chrome/browser/ 1148 // chrome/android/java/src/org/chromium/chrome/browser/
1149 // physicalweb/PwsClientImpl.java 1149 // physicalweb/PwsClientImpl.java
1150 // Input |locales| is a comma separated locale representaion. Each locale 1150 // Input |locales| is a comma separated locale representation that consists of
1151 // representation should be xx_XX style, where xx is a 2-letter 1151 // language tags(BCP47 compliant format). Each language tag contains
1152 // ISO 639-1 compliant language code and XX is a 2-letter 1152 // a language code and a country code or a language code only.
1153 // ISO 3166-1 compliant country code.
1154 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary( 1153 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
1155 const std::string& locales, 1154 const std::string& locales,
1156 std::string* accept_languages) { 1155 std::string* accept_languages) {
1157 std::vector<std::string> locale_list = 1156 std::vector<std::string> locale_list =
1158 base::SplitString(locales + "," + *accept_languages, ",", 1157 base::SplitString(locales + "," + *accept_languages, ",",
1159 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 1158 base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
1160 1159
1161 std::set<std::string> seen_tags; 1160 std::set<std::string> seen_tags;
1162 std::vector<std::pair<std::string, std::string>> unique_locale_list; 1161 std::vector<std::pair<std::string, std::string>> unique_locale_list;
1163 for (const std::string& locale_str : locale_list) { 1162 for (const std::string& locale_str : locale_list) {
1164 // TODO(yirui): Support BCP47 compliant format including 3-letter 1163 std::string lang_code;
1165 // country code, '-' separator and missing country case. 1164 std::string country_code;
1166 if (locale_str.size() != 5u || 1165 if (locale_str.size() == 2u || locale_str.size() == 3u) {
1167 (locale_str[2] != '_' && locale_str[2] != '-')) 1166 // Language tag consists of language only
1168 continue; // Skip not well formed locale. 1167 lang_code = locale_str;
1169 1168 country_code = "";
1170 std::string lang_code(locale_str.substr(0, 2)); 1169 } else if (locale_str[2] == '-') {
ksk1 2016/10/12 05:52:50 Please have length check before accessing elements
Yirui Huang 2016/10/12 09:02:04 Please see patch 6 for another implementing method
1171 std::string country_code(locale_str.substr(3, 2)); 1170 lang_code = locale_str.substr(0, 2);
1171 country_code = locale_str.substr(3);
1172 } else if (locale_str[3] == '-') {
1173 lang_code = locale_str.substr(0, 3);
1174 country_code = locale_str.substr(4);
1175 } else {
1176 continue;
1177 }
1172 1178
1173 // Java mostly follows ISO-639-1 and ICU, except for the following three. 1179 // Java mostly follows ISO-639-1 and ICU, except for the following three.
1174 // See documentation on java.util.Locale constructor for more. 1180 // See documentation on java.util.Locale constructor for more.
1175 if (lang_code == "iw") 1181 if (lang_code == "iw")
1176 lang_code = "he"; 1182 lang_code = "he";
1177 else if (lang_code == "ji") 1183 else if (lang_code == "ji")
1178 lang_code = "yi"; 1184 lang_code = "yi";
1179 else if (lang_code == "in") 1185 else if (lang_code == "in")
1180 lang_code = "id"; 1186 lang_code = "id";
ksk1 2016/10/12 05:52:50 Is it OK not to have tl -> fil conversion?
Yirui Huang 2016/10/12 09:02:04 Done in patch 6 :)
1181 1187
1182 std::string language_tag(lang_code + "-" + country_code); 1188 std::string language_tag(lang_code + "-" + country_code);
1183 1189
1184 if (seen_tags.find(language_tag) != seen_tags.end()) 1190 if (seen_tags.find(language_tag) != seen_tags.end())
1185 continue; 1191 continue;
1192 if (!country_code.empty())
1193 seen_tags.insert(language_tag);
1186 unique_locale_list.push_back(std::make_pair(lang_code, country_code)); 1194 unique_locale_list.push_back(std::make_pair(lang_code, country_code));
1187 seen_tags.insert(language_tag);
1188 } 1195 }
1189 1196
1190 // If language is not in the accept languages list, also add language 1197 // If language is not in the accept languages list, also add language
1191 // code. A language code should only be inserted after the last 1198 // code. A language code should only be inserted after the last
1192 // languageTag that contains that language. 1199 // languageTag that contains that language.
1193 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled 1200 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled
1194 // with Chrome but may fail on arbitrary lists of language tags due to 1201 // with Chrome but may fail on arbitrary lists of language tags due to
1195 // differences in case and whitespace. 1202 // differences in case and whitespace.
1196 std::set<std::string> seen_languages; 1203 std::set<std::string> seen_languages;
1197 std::vector<std::string> output_list; 1204 std::vector<std::string> output_list;
1198 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend(); 1205 for (auto it = unique_locale_list.rbegin(); it != unique_locale_list.rend();
1199 ++it) { 1206 ++it) {
1200 if (seen_languages.find(it->first) == seen_languages.end()) { 1207 if (seen_languages.find(it->first) == seen_languages.end()) {
1201 output_list.push_back(it->first); 1208 output_list.push_back(it->first);
1202 seen_languages.insert(it->first); 1209 seen_languages.insert(it->first);
1203 } 1210 }
1204 output_list.push_back(it->first + "-" + it->second); 1211 if (!it->second.empty())
1212 output_list.push_back(it->first + "-" + it->second);
1205 } 1213 }
1206 1214
1207 std::reverse(output_list.begin(), output_list.end()); 1215 std::reverse(output_list.begin(), output_list.end());
1208 *accept_languages = base::JoinString(output_list, ","); 1216 *accept_languages = base::JoinString(output_list, ",");
1209 } 1217 }
1210 1218
1211 // static 1219 // static
1212 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting( 1220 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
1213 ContentSettingsType content_type) { 1221 ContentSettingsType content_type) {
1214 JNIEnv* env = AttachCurrentThread(); 1222 JNIEnv* env = AttachCurrentThread();
1215 base::android::ScopedJavaLocalRef<jstring> android_permission = 1223 base::android::ScopedJavaLocalRef<jstring> android_permission =
1216 Java_PrefServiceBridge_getAndroidPermissionForContentSetting( 1224 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
1217 env, content_type); 1225 env, content_type);
1218 if (android_permission.is_null()) 1226 if (android_permission.is_null())
1219 return std::string(); 1227 return std::string();
1220 1228
1221 return ConvertJavaStringToUTF8(android_permission); 1229 return ConvertJavaStringToUTF8(android_permission);
1222 } 1230 }
1223 1231
1224 static void SetSupervisedUserId(JNIEnv* env, 1232 static void SetSupervisedUserId(JNIEnv* env,
1225 const JavaParamRef<jobject>& obj, 1233 const JavaParamRef<jobject>& obj,
1226 const JavaParamRef<jstring>& pref) { 1234 const JavaParamRef<jstring>& pref) {
1227 GetPrefService()->SetString(prefs::kSupervisedUserId, 1235 GetPrefService()->SetString(prefs::kSupervisedUserId,
1228 ConvertJavaStringToUTF8(env, pref)); 1236 ConvertJavaStringToUTF8(env, pref));
1229 } 1237 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698