OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/chromeos/input_method/input_method_util.h" | 5 #include "chrome/browser/chromeos/input_method/input_method_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include <utility> | 10 #include <utility> |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 map_entry.english_string_from_ibus, map_entry.resource_id)).second; | 309 map_entry.english_string_from_ibus, map_entry.resource_id)).second; |
310 DCHECK(result) << "Duplicated string is found: " | 310 DCHECK(result) << "Duplicated string is found: " |
311 << map_entry.english_string_from_ibus; | 311 << map_entry.english_string_from_ibus; |
312 } | 312 } |
313 } | 313 } |
314 | 314 |
315 InputMethodUtil::~InputMethodUtil() { | 315 InputMethodUtil::~InputMethodUtil() { |
316 } | 316 } |
317 | 317 |
318 bool InputMethodUtil::TranslateStringInternal( | 318 bool InputMethodUtil::TranslateStringInternal( |
319 const std::string& english_string, string16 *out_string) const { | 319 const std::string& english_string, base::string16 *out_string) const { |
320 DCHECK(out_string); | 320 DCHECK(out_string); |
321 HashType::const_iterator iter = english_to_resource_id_.find(english_string); | 321 HashType::const_iterator iter = english_to_resource_id_.find(english_string); |
322 if (iter == english_to_resource_id_.end()) { | 322 if (iter == english_to_resource_id_.end()) { |
323 // TODO(yusukes): Write Autotest which checks if all display names and all | 323 // TODO(yusukes): Write Autotest which checks if all display names and all |
324 // property names for supported input methods are listed in the resource | 324 // property names for supported input methods are listed in the resource |
325 // ID array (crosbug.com/4572). | 325 // ID array (crosbug.com/4572). |
326 LOG(ERROR) << "Resource ID is not found for: " << english_string; | 326 LOG(ERROR) << "Resource ID is not found for: " << english_string; |
327 return false; | 327 return false; |
328 } | 328 } |
329 | 329 |
330 *out_string = delegate_->GetLocalizedString(iter->second); | 330 *out_string = delegate_->GetLocalizedString(iter->second); |
331 return true; | 331 return true; |
332 } | 332 } |
333 | 333 |
334 string16 InputMethodUtil::TranslateString( | 334 base::string16 InputMethodUtil::TranslateString( |
335 const std::string& english_string) const { | 335 const std::string& english_string) const { |
336 string16 localized_string; | 336 base::string16 localized_string; |
337 if (TranslateStringInternal(english_string, &localized_string)) { | 337 if (TranslateStringInternal(english_string, &localized_string)) { |
338 return localized_string; | 338 return localized_string; |
339 } | 339 } |
340 return UTF8ToUTF16(english_string); | 340 return UTF8ToUTF16(english_string); |
341 } | 341 } |
342 | 342 |
343 bool InputMethodUtil::IsValidInputMethodId( | 343 bool InputMethodUtil::IsValidInputMethodId( |
344 const std::string& input_method_id) const { | 344 const std::string& input_method_id) const { |
345 // We can't check the component extension is whilelisted or not here because | 345 // We can't check the component extension is whilelisted or not here because |
346 // it might not be initialized. | 346 // it might not be initialized. |
(...skipping 23 matching lines...) Expand all Loading... |
370 std::string InputMethodUtil::GetKeyboardLayoutName( | 370 std::string InputMethodUtil::GetKeyboardLayoutName( |
371 const std::string& input_method_id) const { | 371 const std::string& input_method_id) const { |
372 InputMethodIdToDescriptorMap::const_iterator iter | 372 InputMethodIdToDescriptorMap::const_iterator iter |
373 = id_to_descriptor_.find(input_method_id); | 373 = id_to_descriptor_.find(input_method_id); |
374 return (iter == id_to_descriptor_.end()) ? | 374 return (iter == id_to_descriptor_.end()) ? |
375 "" : iter->second.GetPreferredKeyboardLayout(); | 375 "" : iter->second.GetPreferredKeyboardLayout(); |
376 } | 376 } |
377 | 377 |
378 std::string InputMethodUtil::GetInputMethodDisplayNameFromId( | 378 std::string InputMethodUtil::GetInputMethodDisplayNameFromId( |
379 const std::string& input_method_id) const { | 379 const std::string& input_method_id) const { |
380 string16 display_name; | 380 base::string16 display_name; |
381 if (!extension_ime_util::IsExtensionIME(input_method_id) && | 381 if (!extension_ime_util::IsExtensionIME(input_method_id) && |
382 TranslateStringInternal(input_method_id, &display_name)) { | 382 TranslateStringInternal(input_method_id, &display_name)) { |
383 return UTF16ToUTF8(display_name); | 383 return UTF16ToUTF8(display_name); |
384 } | 384 } |
385 // Return an empty string if the display name is not found. | 385 // Return an empty string if the display name is not found. |
386 return ""; | 386 return ""; |
387 } | 387 } |
388 | 388 |
389 string16 InputMethodUtil::GetInputMethodShortName( | 389 base::string16 InputMethodUtil::GetInputMethodShortName( |
390 const InputMethodDescriptor& input_method) const { | 390 const InputMethodDescriptor& input_method) const { |
391 // For the status area, we use two-letter, upper-case language code like | 391 // For the status area, we use two-letter, upper-case language code like |
392 // "US" and "JP". | 392 // "US" and "JP". |
393 string16 text; | 393 base::string16 text; |
394 | 394 |
395 // Check special cases first. | 395 // Check special cases first. |
396 for (size_t i = 0; i < kMappingFromIdToIndicatorTextLen; ++i) { | 396 for (size_t i = 0; i < kMappingFromIdToIndicatorTextLen; ++i) { |
397 if (kMappingFromIdToIndicatorText[i].input_method_id == input_method.id()) { | 397 if (kMappingFromIdToIndicatorText[i].input_method_id == input_method.id()) { |
398 text = UTF8ToUTF16(kMappingFromIdToIndicatorText[i].indicator_text); | 398 text = UTF8ToUTF16(kMappingFromIdToIndicatorText[i].indicator_text); |
399 break; | 399 break; |
400 } | 400 } |
401 } | 401 } |
402 | 402 |
403 // Display the keyboard layout name when using a keyboard layout. | 403 // Display the keyboard layout name when using a keyboard layout. |
404 if (text.empty() && | 404 if (text.empty() && |
405 IsKeyboardLayout(input_method.id())) { | 405 IsKeyboardLayout(input_method.id())) { |
406 const size_t kMaxKeyboardLayoutNameLen = 2; | 406 const size_t kMaxKeyboardLayoutNameLen = 2; |
407 const string16 keyboard_layout = | 407 const base::string16 keyboard_layout = |
408 UTF8ToUTF16(GetKeyboardLayoutName(input_method.id())); | 408 UTF8ToUTF16(GetKeyboardLayoutName(input_method.id())); |
409 text = StringToUpperASCII(keyboard_layout).substr( | 409 text = StringToUpperASCII(keyboard_layout).substr( |
410 0, kMaxKeyboardLayoutNameLen); | 410 0, kMaxKeyboardLayoutNameLen); |
411 } | 411 } |
412 | 412 |
413 // TODO(yusukes): Some languages have two or more input methods. For example, | 413 // TODO(yusukes): Some languages have two or more input methods. For example, |
414 // Thai has 3, Vietnamese has 4. If these input methods could be activated at | 414 // Thai has 3, Vietnamese has 4. If these input methods could be activated at |
415 // the same time, we should do either of the following: | 415 // the same time, we should do either of the following: |
416 // (1) Add mappings to |kMappingFromIdToIndicatorText| | 416 // (1) Add mappings to |kMappingFromIdToIndicatorText| |
417 // (2) Add suffix (1, 2, ...) to |text| when ambiguous. | 417 // (2) Add suffix (1, 2, ...) to |text| when ambiguous. |
418 | 418 |
419 if (text.empty()) { | 419 if (text.empty()) { |
420 const size_t kMaxLanguageNameLen = 2; | 420 const size_t kMaxLanguageNameLen = 2; |
421 DCHECK(!input_method.language_codes().empty()); | 421 DCHECK(!input_method.language_codes().empty()); |
422 const std::string language_code = input_method.language_codes().at(0); | 422 const std::string language_code = input_method.language_codes().at(0); |
423 text = StringToUpperASCII(UTF8ToUTF16(language_code)).substr( | 423 text = StringToUpperASCII(UTF8ToUTF16(language_code)).substr( |
424 0, kMaxLanguageNameLen); | 424 0, kMaxLanguageNameLen); |
425 } | 425 } |
426 DCHECK(!text.empty()); | 426 DCHECK(!text.empty()); |
427 return text; | 427 return text; |
428 } | 428 } |
429 | 429 |
430 string16 InputMethodUtil::GetInputMethodMediumName( | 430 base::string16 InputMethodUtil::GetInputMethodMediumName( |
431 const InputMethodDescriptor& input_method) const { | 431 const InputMethodDescriptor& input_method) const { |
432 // For the "Your input method has changed to..." bubble. In most cases | 432 // For the "Your input method has changed to..." bubble. In most cases |
433 // it uses the same name as the short name, unless found in a table | 433 // it uses the same name as the short name, unless found in a table |
434 // for medium length names. | 434 // for medium length names. |
435 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) { | 435 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) { |
436 if (kMappingImeIdToMediumLenNameResourceId[i].input_method_id == | 436 if (kMappingImeIdToMediumLenNameResourceId[i].input_method_id == |
437 input_method.id()) { | 437 input_method.id()) { |
438 return delegate_->GetLocalizedString( | 438 return delegate_->GetLocalizedString( |
439 kMappingImeIdToMediumLenNameResourceId[i].resource_id); | 439 kMappingImeIdToMediumLenNameResourceId[i].resource_id); |
440 } | 440 } |
441 } | 441 } |
442 return GetInputMethodShortName(input_method); | 442 return GetInputMethodShortName(input_method); |
443 } | 443 } |
444 | 444 |
445 string16 InputMethodUtil::GetInputMethodLongName( | 445 base::string16 InputMethodUtil::GetInputMethodLongName( |
446 const InputMethodDescriptor& input_method) const { | 446 const InputMethodDescriptor& input_method) const { |
447 if (!input_method.name().empty()) { | 447 if (!input_method.name().empty()) { |
448 // If the descriptor has a name, use it. | 448 // If the descriptor has a name, use it. |
449 return UTF8ToUTF16(input_method.name()); | 449 return UTF8ToUTF16(input_method.name()); |
450 } | 450 } |
451 | 451 |
452 // We don't show language here. Name of keyboard layout or input method | 452 // We don't show language here. Name of keyboard layout or input method |
453 // usually imply (or explicitly include) its language. | 453 // usually imply (or explicitly include) its language. |
454 | 454 |
455 // Special case for German, French and Dutch: these languages have multiple | 455 // Special case for German, French and Dutch: these languages have multiple |
456 // keyboard layouts and share the same layout of keyboard (Belgian). We need | 456 // keyboard layouts and share the same layout of keyboard (Belgian). We need |
457 // to show explicitly the language for the layout. For Arabic, Amharic, and | 457 // to show explicitly the language for the layout. For Arabic, Amharic, and |
458 // Indic languages: they share "Standard Input Method". | 458 // Indic languages: they share "Standard Input Method". |
459 const string16 standard_input_method_text = delegate_->GetLocalizedString( | 459 const base::string16 standard_input_method_text = |
460 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD); | 460 delegate_->GetLocalizedString( |
| 461 IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD); |
461 DCHECK(!input_method.language_codes().empty()); | 462 DCHECK(!input_method.language_codes().empty()); |
462 const std::string language_code = input_method.language_codes().at(0); | 463 const std::string language_code = input_method.language_codes().at(0); |
463 | 464 |
464 string16 text = TranslateString(input_method.id()); | 465 base::string16 text = TranslateString(input_method.id()); |
465 if (text == standard_input_method_text || | 466 if (text == standard_input_method_text || |
466 language_code == "de" || | 467 language_code == "de" || |
467 language_code == "fr" || | 468 language_code == "fr" || |
468 language_code == "nl") { | 469 language_code == "nl") { |
469 const string16 language_name = delegate_->GetDisplayLanguageName( | 470 const base::string16 language_name = delegate_->GetDisplayLanguageName( |
470 language_code); | 471 language_code); |
471 | 472 |
472 text = language_name + UTF8ToUTF16(" - ") + text; | 473 text = language_name + UTF8ToUTF16(" - ") + text; |
473 } | 474 } |
474 | 475 |
475 DCHECK(!text.empty()); | 476 DCHECK(!text.empty()); |
476 return text; | 477 return text; |
477 } | 478 } |
478 | 479 |
479 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId( | 480 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId( |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 if (IsKeyboardLayout(input_method.id())) { | 672 if (IsKeyboardLayout(input_method.id())) { |
672 xkb_id_to_descriptor_.insert( | 673 xkb_id_to_descriptor_.insert( |
673 std::make_pair(input_method.GetPreferredKeyboardLayout(), | 674 std::make_pair(input_method.GetPreferredKeyboardLayout(), |
674 input_method)); | 675 input_method)); |
675 } | 676 } |
676 } | 677 } |
677 } | 678 } |
678 | 679 |
679 } // namespace input_method | 680 } // namespace input_method |
680 } // namespace chromeos | 681 } // namespace chromeos |
OLD | NEW |