OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 cr.define('options.language', function() { | 5 cr.define('options.language', function() { |
6 const ArrayDataModel = cr.ui.ArrayDataModel; | 6 const ArrayDataModel = cr.ui.ArrayDataModel; |
7 const LanguageOptions = options.LanguageOptions; | 7 const LanguageOptions = options.LanguageOptions; |
8 const List = cr.ui.List; | 8 const List = cr.ui.List; |
9 const ListItem = cr.ui.ListItem; | 9 const ListItem = cr.ui.ListItem; |
10 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | 10 const ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 } | 281 } |
282 }, | 282 }, |
283 | 283 |
284 /** | 284 /** |
285 * Saves the preference. | 285 * Saves the preference. |
286 */ | 286 */ |
287 savePreference_: function() { | 287 savePreference_: function() { |
288 // Encode the language codes into a CSV string. | 288 // Encode the language codes into a CSV string. |
289 Preferences.setStringPref(this.preferredLanguagesPref, | 289 Preferences.setStringPref(this.preferredLanguagesPref, |
290 this.dataModel.slice().join(',')); | 290 this.dataModel.slice().join(',')); |
291 // Save the same language list as accept languages preference. In | 291 // Save the same language list as accept languages preference as |
292 // theory, we don't need two separate preferences but we keep these | 292 // well, but we need to expand the language list, to make it more |
293 // separate, as these are conceptually different. In other words, | 293 // acceptable. For instance, some web sites don't understand 'en-US' |
294 // using "intl.accept_languages" for preferred languages in Chrome | 294 // but 'en'. See crosbug.com/9884. |
295 // OS is a bit awkward. | 295 var acceptLanguages = this.expandLanguageCodes(this.dataModel.slice()); |
296 Preferences.setStringPref(this.acceptLanguagesPref, | 296 Preferences.setStringPref(this.acceptLanguagesPref, |
297 this.dataModel.slice().join(',')); | 297 acceptLanguages.join(',')); |
298 cr.dispatchSimpleEvent(this, 'save'); | 298 cr.dispatchSimpleEvent(this, 'save'); |
299 }, | 299 }, |
300 | 300 |
301 /** | 301 /** |
| 302 * Expands language codes to make these more suitable for Accept-Language. |
| 303 * Example: ['en-US', 'ja', 'en-CA'] => ['en-US', 'en', 'ja', 'en-CA']. |
| 304 * 'en' won't appear twice as this function eliminates duplicates. |
| 305 * @param {Array} languageCodes List of language codes. |
| 306 * @private |
| 307 */ |
| 308 expandLanguageCodes: function(languageCodes) { |
| 309 var expandedLanguageCodes = []; |
| 310 var seen = {}; // Used to eliminiate duplicates. |
| 311 for (var i = 0; i < languageCodes.length; i++) { |
| 312 var languageCode = languageCodes[i]; |
| 313 if (!(languageCode in seen)) { |
| 314 expandedLanguageCodes.push(languageCode); |
| 315 seen[languageCode] = true; |
| 316 } |
| 317 var parts = languageCode.split('-'); |
| 318 if (!(parts[0] in seen)) { |
| 319 expandedLanguageCodes.push(parts[0]); |
| 320 seen[parts[0]] = true; |
| 321 } |
| 322 } |
| 323 return expandedLanguageCodes; |
| 324 }, |
| 325 |
| 326 /** |
302 * Filters bad language codes in case bad language codes are | 327 * Filters bad language codes in case bad language codes are |
303 * stored in the preference. Removes duplicates as well. | 328 * stored in the preference. Removes duplicates as well. |
304 * @param {Array} languageCodes List of language codes. | 329 * @param {Array} languageCodes List of language codes. |
305 * @private | 330 * @private |
306 */ | 331 */ |
307 filterBadLanguageCodes_: function(languageCodes) { | 332 filterBadLanguageCodes_: function(languageCodes) { |
308 var filteredLanguageCodes = []; | 333 var filteredLanguageCodes = []; |
309 var seen = {}; | 334 var seen = {}; |
310 for (var i = 0; i < languageCodes.length; i++) { | 335 for (var i = 0; i < languageCodes.length; i++) { |
311 // Check if the the language code is valid, and not | 336 // Check if the the language code is valid, and not |
312 // duplicate. Otherwise, skip it. | 337 // duplicate. Otherwise, skip it. |
313 if (LanguageList.isValidLanguageCode(languageCodes[i]) && | 338 if (LanguageList.isValidLanguageCode(languageCodes[i]) && |
314 !(languageCodes[i] in seen)) { | 339 !(languageCodes[i] in seen)) { |
315 filteredLanguageCodes.push(languageCodes[i]); | 340 filteredLanguageCodes.push(languageCodes[i]); |
316 seen[languageCodes[i]] = true; | 341 seen[languageCodes[i]] = true; |
317 } | 342 } |
318 } | 343 } |
319 return filteredLanguageCodes; | 344 return filteredLanguageCodes; |
320 }, | 345 }, |
321 }; | 346 }; |
322 | 347 |
323 return { | 348 return { |
324 LanguageList: LanguageList | 349 LanguageList: LanguageList |
325 }; | 350 }; |
326 }); | 351 }); |
OLD | NEW |