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