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 "components/translate/core/browser/translate_ui_delegate.h" | 5 #include "components/translate/core/browser/translate_ui_delegate.h" |
6 | 6 |
7 #include "base/i18n/string_compare.h" | 7 #include "base/i18n/string_compare.h" |
8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
9 #include "components/translate/core/browser/language_state.h" | 9 #include "components/translate/core/browser/language_state.h" |
10 #include "components/translate/core/browser/translate_client.h" | 10 #include "components/translate/core/browser/translate_client.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
22 const char kRevertTranslation[] = "Translate.RevertTranslation"; | 22 const char kRevertTranslation[] = "Translate.RevertTranslation"; |
23 const char kPerformTranslate[] = "Translate.Translate"; | 23 const char kPerformTranslate[] = "Translate.Translate"; |
24 const char kNeverTranslateLang[] = "Translate.NeverTranslateLang"; | 24 const char kNeverTranslateLang[] = "Translate.NeverTranslateLang"; |
25 const char kNeverTranslateSite[] = "Translate.NeverTranslateSite"; | 25 const char kNeverTranslateSite[] = "Translate.NeverTranslateSite"; |
26 const char kAlwaysTranslateLang[] = "Translate.AlwaysTranslateLang"; | 26 const char kAlwaysTranslateLang[] = "Translate.AlwaysTranslateLang"; |
27 const char kModifyOriginalLang[] = "Translate.ModifyOriginalLang"; | 27 const char kModifyOriginalLang[] = "Translate.ModifyOriginalLang"; |
28 const char kModifyTargetLang[] = "Translate.ModifyTargetLang"; | 28 const char kModifyTargetLang[] = "Translate.ModifyTargetLang"; |
29 const char kDeclineTranslateDismissUI[] = "Translate.DeclineTranslateDismissUI"; | 29 const char kDeclineTranslateDismissUI[] = "Translate.DeclineTranslateDismissUI"; |
30 const char kShowErrorUI[] = "Translate.ShowErrorUI"; | 30 const char kShowErrorUI[] = "Translate.ShowErrorUI"; |
31 | 31 |
32 // Returns a Collator object which helps to sort strings in a given locale or | |
33 // null if unable to find the right collator. | |
34 // | |
35 // TODO(hajimehoshi): Write a test for icu::Collator::createInstance. | |
36 scoped_ptr<icu::Collator> CreateCollator(const std::string& locale) { | |
37 UErrorCode error = U_ZERO_ERROR; | |
38 icu::Locale loc(locale.c_str()); | |
39 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); | |
40 if (!collator || !U_SUCCESS(error)) | |
41 return nullptr; | |
Andrew Hayden (chromium.org)
2015/11/09 14:09:38
It would have been nice to add a comment here indi
| |
42 collator->setStrength(icu::Collator::PRIMARY); | |
43 return collator.Pass(); | |
44 } | |
45 | |
32 } // namespace | 46 } // namespace |
33 | 47 |
34 namespace translate { | 48 namespace translate { |
35 | 49 |
36 TranslateUIDelegate::TranslateUIDelegate( | 50 TranslateUIDelegate::TranslateUIDelegate( |
37 const base::WeakPtr<TranslateManager>& translate_manager, | 51 const base::WeakPtr<TranslateManager>& translate_manager, |
38 const std::string& original_language, | 52 const std::string& original_language, |
39 const std::string& target_language) | 53 const std::string& target_language) |
40 : translate_driver_( | 54 : translate_driver_( |
41 translate_manager->translate_client()->GetTranslateDriver()), | 55 translate_manager->translate_client()->GetTranslateDriver()), |
42 translate_manager_(translate_manager), | 56 translate_manager_(translate_manager), |
43 original_language_index_(kNoIndex), | 57 original_language_index_(kNoIndex), |
44 initial_original_language_index_(kNoIndex), | 58 initial_original_language_index_(kNoIndex), |
45 target_language_index_(kNoIndex) { | 59 target_language_index_(kNoIndex) { |
46 DCHECK(translate_driver_); | 60 DCHECK(translate_driver_); |
47 DCHECK(translate_manager_); | 61 DCHECK(translate_manager_); |
48 | 62 |
49 std::vector<std::string> language_codes; | 63 std::vector<std::string> language_codes; |
50 TranslateDownloadManager::GetSupportedLanguages(&language_codes); | 64 TranslateDownloadManager::GetSupportedLanguages(&language_codes); |
51 | 65 |
52 // Preparing for the alphabetical order in the locale. | 66 // Preparing for the alphabetical order in the locale. |
53 UErrorCode error = U_ZERO_ERROR; | |
54 std::string locale = | 67 std::string locale = |
55 TranslateDownloadManager::GetInstance()->application_locale(); | 68 TranslateDownloadManager::GetInstance()->application_locale(); |
56 icu::Locale loc(locale.c_str()); | 69 scoped_ptr<icu::Collator> collator = CreateCollator(locale); |
57 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); | |
58 collator->setStrength(icu::Collator::PRIMARY); | |
59 | 70 |
60 languages_.reserve(language_codes.size()); | 71 languages_.reserve(language_codes.size()); |
61 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); | 72 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); |
62 iter != language_codes.end(); | 73 iter != language_codes.end(); |
63 ++iter) { | 74 ++iter) { |
64 std::string language_code = *iter; | 75 std::string language_code = *iter; |
65 | 76 |
66 base::string16 language_name = | 77 base::string16 language_name = |
67 l10n_util::GetDisplayNameForLocale(language_code, locale, true); | 78 l10n_util::GetDisplayNameForLocale(language_code, locale, true); |
68 // Insert the language in languages_ in alphabetical order. | 79 // Insert the language in languages_ in alphabetical order. |
69 std::vector<LanguageNamePair>::iterator iter2; | 80 std::vector<LanguageNamePair>::iterator iter2; |
70 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { | 81 if (collator) { |
71 if (base::i18n::CompareString16WithCollator(*collator, language_name, | 82 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { |
72 iter2->second) == UCOL_LESS) { | 83 int result = base::i18n::CompareString16WithCollator(*collator, |
73 break; | 84 language_name, |
85 iter2->second); | |
86 if (result == UCOL_LESS) | |
87 break; | |
88 } | |
89 } else { | |
90 // |locale| may not be supported by ICU collator (crbug/54833). In this | |
91 // case, let's order the languages in UTF-8. | |
92 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { | |
93 if (language_name.compare(iter2->second) < 0) | |
94 break; | |
74 } | 95 } |
75 } | 96 } |
76 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); | 97 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); |
77 } | 98 } |
78 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); | 99 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); |
79 iter != languages_.end(); | 100 iter != languages_.end(); |
80 ++iter) { | 101 ++iter) { |
81 std::string language_code = iter->first; | 102 std::string language_code = iter->first; |
82 if (language_code == original_language) { | 103 if (language_code == original_language) { |
83 original_language_index_ = iter - languages_.begin(); | 104 original_language_index_ = iter - languages_.begin(); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true); | 276 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true); |
256 } | 277 } |
257 | 278 |
258 std::string TranslateUIDelegate::GetPageHost() { | 279 std::string TranslateUIDelegate::GetPageHost() { |
259 if (!translate_driver_->HasCurrentPage()) | 280 if (!translate_driver_->HasCurrentPage()) |
260 return std::string(); | 281 return std::string(); |
261 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); | 282 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); |
262 } | 283 } |
263 | 284 |
264 } // namespace translate | 285 } // namespace translate |
OLD | NEW |