OLD | NEW |
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/ui/webui/chromeos/login/l10n_util.h" | 5 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "chrome/browser/chromeos/input_method/input_method_util.h" | 28 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
29 #include "chrome/grit/generated_resources.h" | 29 #include "chrome/grit/generated_resources.h" |
30 #include "chromeos/ime/component_extension_ime_manager.h" | 30 #include "chromeos/ime/component_extension_ime_manager.h" |
31 #include "chromeos/ime/input_method_descriptor.h" | 31 #include "chromeos/ime/input_method_descriptor.h" |
32 #include "chromeos/ime/input_method_manager.h" | 32 #include "chromeos/ime/input_method_manager.h" |
33 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
34 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
35 | 35 |
36 namespace chromeos { | 36 namespace chromeos { |
37 | 37 |
| 38 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER"; |
| 39 |
38 namespace { | 40 namespace { |
39 | 41 |
40 const char kSequenceToken[] = "chromeos_login_l10n_util"; | 42 const char kSequenceToken[] = "chromeos_login_l10n_util"; |
41 | 43 |
42 scoped_ptr<base::DictionaryValue> CreateInputMethodsEntry( | 44 scoped_ptr<base::DictionaryValue> CreateInputMethodsEntry( |
43 const input_method::InputMethodDescriptor& method, | 45 const input_method::InputMethodDescriptor& method, |
44 const std::string selected) { | 46 const std::string selected) { |
45 input_method::InputMethodUtil* util = | 47 input_method::InputMethodUtil* util = |
46 input_method::InputMethodManager::Get()->GetInputMethodUtil(); | 48 input_method::InputMethodManager::Get()->GetInputMethodUtil(); |
47 const std::string& ime_id = method.id(); | 49 const std::string& ime_id = method.id(); |
(...skipping 15 matching lines...) Expand all Loading... |
63 // TODO(dpolukhin): crbug.com/407579 | 65 // TODO(dpolukhin): crbug.com/407579 |
64 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { | 66 void AddOptgroupOtherLayouts(base::ListValue* input_methods_list) { |
65 scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue); | 67 scoped_ptr<base::DictionaryValue> optgroup(new base::DictionaryValue); |
66 optgroup->SetString( | 68 optgroup->SetString( |
67 "optionGroupName", | 69 "optionGroupName", |
68 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS)); | 70 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_KEYBOARD_LAYOUTS)); |
69 input_methods_list->Append(optgroup.release()); | 71 input_methods_list->Append(optgroup.release()); |
70 } | 72 } |
71 #endif | 73 #endif |
72 | 74 |
| 75 base::DictionaryValue* CreateLanguageEntry( |
| 76 const std::string& language_code, |
| 77 const base::string16& language_display_name, |
| 78 const base::string16& language_native_display_name) { |
| 79 base::string16 display_name = language_display_name; |
| 80 const bool markup_removal = |
| 81 base::i18n::UnadjustStringForLocaleDirection(&display_name); |
| 82 DCHECK(markup_removal); |
| 83 |
| 84 const bool has_rtl_chars = |
| 85 base::i18n::StringContainsStrongRTLChars(display_name); |
| 86 const std::string directionality = has_rtl_chars ? "rtl" : "ltr"; |
| 87 |
| 88 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); |
| 89 dictionary->SetString("code", language_code); |
| 90 dictionary->SetString("displayName", language_display_name); |
| 91 dictionary->SetString("textDirection", directionality); |
| 92 dictionary->SetString("nativeDisplayName", language_native_display_name); |
| 93 return dictionary.release(); |
| 94 } |
| 95 |
73 // Gets the list of languages with |descriptors| based on |base_language_codes|. | 96 // Gets the list of languages with |descriptors| based on |base_language_codes|. |
74 // The |most_relevant_language_codes| will be first in the list. If | 97 // The |most_relevant_language_codes| will be first in the list. If |
75 // |insert_divider| is true, an entry with its "code" attribute set to | 98 // |insert_divider| is true, an entry with its "code" attribute set to |
76 // kMostRelevantLanguagesDivider is placed between the most relevant languages | 99 // kMostRelevantLanguagesDivider is placed between the most relevant languages |
77 // and all others. | 100 // and all others. |
78 scoped_ptr<base::ListValue> GetLanguageList( | 101 scoped_ptr<base::ListValue> GetLanguageList( |
79 const input_method::InputMethodDescriptors& descriptors, | 102 const input_method::InputMethodDescriptors& descriptors, |
80 const std::vector<std::string>& base_language_codes, | 103 const std::vector<std::string>& base_language_codes, |
81 const std::vector<std::string>& most_relevant_language_codes, | 104 const std::vector<std::string>& most_relevant_language_codes, |
82 bool insert_divider) { | 105 bool insert_divider) { |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 for (size_t i = 0; i < out_display_names.size(); ++i) { | 281 for (size_t i = 0; i < out_display_names.size(); ++i) { |
259 // Sets the directionality of the display language name. | 282 // Sets the directionality of the display language name. |
260 base::string16 display_name(out_display_names[i]); | 283 base::string16 display_name(out_display_names[i]); |
261 if (insert_divider && display_name == divider16) { | 284 if (insert_divider && display_name == divider16) { |
262 // Insert divider. | 285 // Insert divider. |
263 base::DictionaryValue* dictionary = new base::DictionaryValue(); | 286 base::DictionaryValue* dictionary = new base::DictionaryValue(); |
264 dictionary->SetString("code", kMostRelevantLanguagesDivider); | 287 dictionary->SetString("code", kMostRelevantLanguagesDivider); |
265 language_list->Append(dictionary); | 288 language_list->Append(dictionary); |
266 continue; | 289 continue; |
267 } | 290 } |
268 const bool markup_removal = | |
269 base::i18n::UnadjustStringForLocaleDirection(&display_name); | |
270 DCHECK(markup_removal); | |
271 const bool has_rtl_chars = | |
272 base::i18n::StringContainsStrongRTLChars(display_name); | |
273 const std::string directionality = has_rtl_chars ? "rtl" : "ltr"; | |
274 | 291 |
275 const LanguagePair& pair = language_map[out_display_names[i]]; | 292 const LanguagePair& pair = language_map[out_display_names[i]]; |
276 base::DictionaryValue* dictionary = new base::DictionaryValue(); | 293 language_list->Append( |
277 dictionary->SetString("code", pair.first); | 294 CreateLanguageEntry(pair.first, out_display_names[i], pair.second)); |
278 dictionary->SetString("displayName", out_display_names[i]); | |
279 dictionary->SetString("textDirection", directionality); | |
280 dictionary->SetString("nativeDisplayName", pair.second); | |
281 language_list->Append(dictionary); | |
282 } | 295 } |
283 | 296 |
284 return language_list.Pass(); | 297 return language_list.Pass(); |
285 } | 298 } |
286 | 299 |
287 // Invokes |callback| with a list of keyboard layouts that can be used for | 300 // Invokes |callback| with a list of keyboard layouts that can be used for |
288 // |resolved_locale|. | 301 // |resolved_locale|. |
289 void GetKeyboardLayoutsForResolvedLocale( | 302 void GetKeyboardLayoutsForResolvedLocale( |
290 const GetKeyboardLayoutsForLocaleCallback& callback, | 303 const GetKeyboardLayoutsForLocaleCallback& callback, |
291 const std::string& resolved_locale) { | 304 const std::string& resolved_locale) { |
(...skipping 22 matching lines...) Expand all Loading... |
314 util->GetInputMethodDescriptorFromId(*it); | 327 util->GetInputMethodDescriptorFromId(*it); |
315 if (!InsertString(ime->id(), &input_methods_added)) | 328 if (!InsertString(ime->id(), &input_methods_added)) |
316 continue; | 329 continue; |
317 input_methods_list->Append( | 330 input_methods_list->Append( |
318 CreateInputMethodsEntry(*ime, selected).release()); | 331 CreateInputMethodsEntry(*ime, selected).release()); |
319 } | 332 } |
320 | 333 |
321 callback.Run(input_methods_list.Pass()); | 334 callback.Run(input_methods_list.Pass()); |
322 } | 335 } |
323 | 336 |
324 } // namespace | 337 // For "UI Language" drop-down menu at OOBE screen we need to decide which |
| 338 // entry to mark "selected". If user has just selected "requested_locale", |
| 339 // but "loaded_locale" was actually loaded, we mark original user choice |
| 340 // "selected" only if loaded_locale is a backup for "requested_locale". |
| 341 std::string CalculateSelectedLanguage(const std::string& requested_locale, |
| 342 const std::string& loaded_locale) { |
| 343 std::string resolved_locale; |
| 344 if (!l10n_util::CheckAndResolveLocale(requested_locale, &resolved_locale)) |
| 345 return loaded_locale; |
325 | 346 |
326 const char kMostRelevantLanguagesDivider[] = "MOST_RELEVANT_LANGUAGES_DIVIDER"; | 347 if (resolved_locale == loaded_locale) |
| 348 return requested_locale; |
327 | 349 |
328 scoped_ptr<base::ListValue> GetUILanguageList( | 350 return loaded_locale; |
329 const std::vector<std::string>* most_relevant_language_codes, | 351 } |
330 const std::string& selected) { | |
331 ComponentExtensionIMEManager* manager = | |
332 input_method::InputMethodManager::Get()-> | |
333 GetComponentExtensionIMEManager(); | |
334 input_method::InputMethodDescriptors descriptors = | |
335 manager->GetXkbIMEAsInputMethodDescriptor(); | |
336 scoped_ptr<base::ListValue> languages_list(GetLanguageList( | |
337 descriptors, | |
338 l10n_util::GetAvailableLocales(), | |
339 most_relevant_language_codes | |
340 ? *most_relevant_language_codes | |
341 : StartupCustomizationDocument::GetInstance()->configured_locales(), | |
342 true)); | |
343 | 352 |
| 353 void ResolveLanguageListOnBlockingPool( |
| 354 const chromeos::locale_util::LanguageSwitchResult* language_switch_result, |
| 355 scoped_ptr<base::ListValue>* list, |
| 356 std::string* list_locale, |
| 357 std::string* selected_language) { |
| 358 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 359 |
| 360 if (!language_switch_result) { |
| 361 selected_language->clear(); |
| 362 } else { |
| 363 if (language_switch_result->success) { |
| 364 if (language_switch_result->requested_locale == |
| 365 language_switch_result->loaded_locale) { |
| 366 *selected_language = language_switch_result->requested_locale; |
| 367 } else { |
| 368 *selected_language = |
| 369 CalculateSelectedLanguage(language_switch_result->requested_locale, |
| 370 language_switch_result->loaded_locale); |
| 371 } |
| 372 } else { |
| 373 *selected_language = language_switch_result->loaded_locale; |
| 374 } |
| 375 } |
| 376 const std::string selected_code = |
| 377 selected_language->empty() ? g_browser_process->GetApplicationLocale() |
| 378 : *selected_language; |
| 379 |
| 380 *list_locale = language_switch_result |
| 381 ? language_switch_result->loaded_locale |
| 382 : g_browser_process->GetApplicationLocale(); |
| 383 list->reset(chromeos::GetUILanguageList(NULL, selected_code).release()); |
| 384 } |
| 385 |
| 386 void OnLanguageListResolved( |
| 387 UILanguageListResolvedCallback callback, |
| 388 scoped_ptr<scoped_ptr<base::ListValue>> new_language_list, |
| 389 scoped_ptr<std::string> new_language_list_locale, |
| 390 scoped_ptr<std::string> new_selected_language) { |
| 391 callback.Run(new_language_list->Pass(), |
| 392 *new_language_list_locale, |
| 393 *new_selected_language); |
| 394 } |
| 395 |
| 396 void AdjustUILanguageList(const std::string& selected, |
| 397 base::ListValue* languages_list) { |
344 for (size_t i = 0; i < languages_list->GetSize(); ++i) { | 398 for (size_t i = 0; i < languages_list->GetSize(); ++i) { |
345 base::DictionaryValue* language_info = NULL; | 399 base::DictionaryValue* language_info = NULL; |
346 if (!languages_list->GetDictionary(i, &language_info)) | 400 if (!languages_list->GetDictionary(i, &language_info)) |
347 NOTREACHED(); | 401 NOTREACHED(); |
348 | 402 |
349 std::string value; | 403 std::string value; |
350 language_info->GetString("code", &value); | 404 language_info->GetString("code", &value); |
351 std::string display_name; | 405 std::string display_name; |
352 language_info->GetString("displayName", &display_name); | 406 language_info->GetString("displayName", &display_name); |
353 std::string native_name; | 407 std::string native_name; |
354 language_info->GetString("nativeDisplayName", &native_name); | 408 language_info->GetString("nativeDisplayName", &native_name); |
355 | 409 |
356 // If it's an option group divider, add field name. | 410 // If it's an option group divider, add field name. |
357 if (value == kMostRelevantLanguagesDivider) { | 411 if (value == kMostRelevantLanguagesDivider) { |
358 language_info->SetString( | 412 language_info->SetString( |
359 "optionGroupName", | 413 "optionGroupName", |
360 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES)); | 414 l10n_util::GetStringUTF16(IDS_OOBE_OTHER_LANGUAGES)); |
361 } | 415 } |
362 if (display_name != native_name) { | 416 if (display_name != native_name) { |
363 display_name = base::StringPrintf("%s - %s", | 417 display_name = base::StringPrintf("%s - %s", |
364 display_name.c_str(), | 418 display_name.c_str(), |
365 native_name.c_str()); | 419 native_name.c_str()); |
366 } | 420 } |
367 | 421 |
368 language_info->SetString("value", value); | 422 language_info->SetString("value", value); |
369 language_info->SetString("title", display_name); | 423 language_info->SetString("title", display_name); |
370 if (value == selected) | 424 if (value == selected) |
371 language_info->SetBoolean("selected", true); | 425 language_info->SetBoolean("selected", true); |
372 } | 426 } |
| 427 } |
| 428 |
| 429 } // namespace |
| 430 |
| 431 void ResolveUILanguageList( |
| 432 scoped_ptr<chromeos::locale_util::LanguageSwitchResult> |
| 433 language_switch_result, |
| 434 UILanguageListResolvedCallback callback) { |
| 435 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 436 |
| 437 scoped_ptr<scoped_ptr<base::ListValue>> new_language_list( |
| 438 new scoped_ptr<base::ListValue>()); |
| 439 scoped_ptr<std::string> new_language_list_locale(new std::string); |
| 440 scoped_ptr<std::string> new_selected_language(new std::string); |
| 441 |
| 442 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 443 FROM_HERE, |
| 444 base::Bind(&ResolveLanguageListOnBlockingPool, |
| 445 base::Owned(language_switch_result.release()), |
| 446 base::Unretained(new_language_list.get()), |
| 447 base::Unretained(new_language_list_locale.get()), |
| 448 base::Unretained(new_selected_language.get())), |
| 449 base::Bind(&OnLanguageListResolved, |
| 450 callback, |
| 451 base::Passed(new_language_list.Pass()), |
| 452 base::Passed(new_language_list_locale.Pass()), |
| 453 base::Passed(new_selected_language.Pass()))); |
| 454 } |
| 455 |
| 456 scoped_ptr<base::ListValue> GetMinimalUILanguageList() { |
| 457 const std::string application_locale = |
| 458 g_browser_process->GetApplicationLocale(); |
| 459 base::string16 language_native_display_name = |
| 460 l10n_util::GetDisplayNameForLocale( |
| 461 application_locale, application_locale, true); |
| 462 |
| 463 scoped_ptr<base::ListValue> language_list(new base::ListValue()); |
| 464 language_list->Append(CreateLanguageEntry(application_locale, |
| 465 language_native_display_name, |
| 466 language_native_display_name)); |
| 467 AdjustUILanguageList(std::string(), language_list.get()); |
| 468 return language_list.Pass(); |
| 469 } |
| 470 |
| 471 scoped_ptr<base::ListValue> GetUILanguageList( |
| 472 const std::vector<std::string>* most_relevant_language_codes, |
| 473 const std::string& selected) { |
| 474 ComponentExtensionIMEManager* manager = |
| 475 input_method::InputMethodManager::Get() |
| 476 ->GetComponentExtensionIMEManager(); |
| 477 input_method::InputMethodDescriptors descriptors = |
| 478 manager->GetXkbIMEAsInputMethodDescriptor(); |
| 479 scoped_ptr<base::ListValue> languages_list(GetLanguageList( |
| 480 descriptors, |
| 481 l10n_util::GetAvailableLocales(), |
| 482 most_relevant_language_codes |
| 483 ? *most_relevant_language_codes |
| 484 : StartupCustomizationDocument::GetInstance()->configured_locales(), |
| 485 true)); |
| 486 AdjustUILanguageList(selected, languages_list.get()); |
373 return languages_list.Pass(); | 487 return languages_list.Pass(); |
374 } | 488 } |
375 | 489 |
376 std::string FindMostRelevantLocale( | 490 std::string FindMostRelevantLocale( |
377 const std::vector<std::string>& most_relevant_language_codes, | 491 const std::vector<std::string>& most_relevant_language_codes, |
378 const base::ListValue& available_locales, | 492 const base::ListValue& available_locales, |
379 const std::string& fallback_locale) { | 493 const std::string& fallback_locale) { |
380 for (std::vector<std::string>::const_iterator most_relevant_it = | 494 for (std::vector<std::string>::const_iterator most_relevant_it = |
381 most_relevant_language_codes.begin(); | 495 most_relevant_language_codes.begin(); |
382 most_relevant_it != most_relevant_language_codes.end(); | 496 most_relevant_it != most_relevant_language_codes.end(); |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 scoped_ptr<base::DictionaryValue> GetCurrentKeyboardLayout() { | 617 scoped_ptr<base::DictionaryValue> GetCurrentKeyboardLayout() { |
504 const input_method::InputMethodDescriptor current_input_method = | 618 const input_method::InputMethodDescriptor current_input_method = |
505 input_method::InputMethodManager::Get() | 619 input_method::InputMethodManager::Get() |
506 ->GetActiveIMEState() | 620 ->GetActiveIMEState() |
507 ->GetCurrentInputMethod(); | 621 ->GetCurrentInputMethod(); |
508 return CreateInputMethodsEntry(current_input_method, | 622 return CreateInputMethodsEntry(current_input_method, |
509 current_input_method.id()); | 623 current_input_method.id()); |
510 } | 624 } |
511 | 625 |
512 } // namespace chromeos | 626 } // namespace chromeos |
OLD | NEW |