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 // TODO(hajimehoshi): Write a test for icu::Collator::createInstance. | |
33 scoped_ptr<icu::Collator> CreateCollator(const std::string& locale) { | |
Miguel Garcia
2015/11/04 09:14:58
document the return value
hajimehoshi
2015/11/04 09:48:48
Done.
| |
34 UErrorCode error = U_ZERO_ERROR; | |
35 icu::Locale loc(locale.c_str()); | |
36 icu::Collator* collator_instance = icu::Collator::createInstance(loc, error); | |
37 if (!collator_instance) | |
Miguel Garcia
2015/11/04 09:14:58
I think this should be if(!collator_instance || !U
hajimehoshi
2015/11/04 09:48:48
Ah, that's much better. Done.
| |
38 return nullptr; | |
39 scoped_ptr<icu::Collator> collator(collator_instance); | |
Miguel Garcia
2015/11/04 09:14:58
can you make this atomic? i.e. just do
scoped_ptr<
hajimehoshi
2015/11/04 09:48:48
Done.
| |
40 collator->setStrength(icu::Collator::PRIMARY); | |
41 return collator.Pass(); | |
42 } | |
43 | |
32 } // namespace | 44 } // namespace |
33 | 45 |
34 namespace translate { | 46 namespace translate { |
35 | 47 |
36 TranslateUIDelegate::TranslateUIDelegate( | 48 TranslateUIDelegate::TranslateUIDelegate( |
37 const base::WeakPtr<TranslateManager>& translate_manager, | 49 const base::WeakPtr<TranslateManager>& translate_manager, |
38 const std::string& original_language, | 50 const std::string& original_language, |
39 const std::string& target_language) | 51 const std::string& target_language) |
40 : translate_driver_( | 52 : translate_driver_( |
41 translate_manager->translate_client()->GetTranslateDriver()), | 53 translate_manager->translate_client()->GetTranslateDriver()), |
42 translate_manager_(translate_manager), | 54 translate_manager_(translate_manager), |
43 original_language_index_(kNoIndex), | 55 original_language_index_(kNoIndex), |
44 initial_original_language_index_(kNoIndex), | 56 initial_original_language_index_(kNoIndex), |
45 target_language_index_(kNoIndex) { | 57 target_language_index_(kNoIndex) { |
46 DCHECK(translate_driver_); | 58 DCHECK(translate_driver_); |
47 DCHECK(translate_manager_); | 59 DCHECK(translate_manager_); |
48 | 60 |
49 std::vector<std::string> language_codes; | 61 std::vector<std::string> language_codes; |
50 TranslateDownloadManager::GetSupportedLanguages(&language_codes); | 62 TranslateDownloadManager::GetSupportedLanguages(&language_codes); |
51 | 63 |
52 // Preparing for the alphabetical order in the locale. | 64 // Preparing for the alphabetical order in the locale. |
53 UErrorCode error = U_ZERO_ERROR; | |
54 std::string locale = | 65 std::string locale = |
55 TranslateDownloadManager::GetInstance()->application_locale(); | 66 TranslateDownloadManager::GetInstance()->application_locale(); |
56 icu::Locale loc(locale.c_str()); | 67 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 | 68 |
60 languages_.reserve(language_codes.size()); | 69 languages_.reserve(language_codes.size()); |
61 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); | 70 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); |
62 iter != language_codes.end(); | 71 iter != language_codes.end(); |
63 ++iter) { | 72 ++iter) { |
64 std::string language_code = *iter; | 73 std::string language_code = *iter; |
65 | 74 |
66 base::string16 language_name = | 75 base::string16 language_name = |
67 l10n_util::GetDisplayNameForLocale(language_code, locale, true); | 76 l10n_util::GetDisplayNameForLocale(language_code, locale, true); |
68 // Insert the language in languages_ in alphabetical order. | 77 // Insert the language in languages_ in alphabetical order. |
69 std::vector<LanguageNamePair>::iterator iter2; | 78 std::vector<LanguageNamePair>::iterator iter2; |
70 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { | 79 if (collator) { |
71 if (base::i18n::CompareString16WithCollator(*collator, language_name, | 80 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { |
72 iter2->second) == UCOL_LESS) { | 81 int result = base::i18n::CompareString16WithCollator(*collator, |
73 break; | 82 language_name, |
83 iter2->second); | |
84 if (result == UCOL_LESS) | |
85 break; | |
86 } | |
87 } else { | |
88 // |locale| may not be supported by ICU collator (crbug/54833). In this | |
89 // case, let's order the languages in ASCII. | |
Miguel Garcia
2015/11/04 09:14:58
I guess this is actually ordering in UTF-8 which a
| |
90 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { | |
91 if (language_name.compare(iter2->second) < 0) | |
92 break; | |
74 } | 93 } |
75 } | 94 } |
76 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); | 95 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); |
77 } | 96 } |
78 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); | 97 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); |
79 iter != languages_.end(); | 98 iter != languages_.end(); |
80 ++iter) { | 99 ++iter) { |
81 std::string language_code = iter->first; | 100 std::string language_code = iter->first; |
82 if (language_code == original_language) { | 101 if (language_code == original_language) { |
83 original_language_index_ = iter - languages_.begin(); | 102 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); | 274 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true); |
256 } | 275 } |
257 | 276 |
258 std::string TranslateUIDelegate::GetPageHost() { | 277 std::string TranslateUIDelegate::GetPageHost() { |
259 if (!translate_driver_->HasCurrentPage()) | 278 if (!translate_driver_->HasCurrentPage()) |
260 return std::string(); | 279 return std::string(); |
261 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); | 280 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); |
262 } | 281 } |
263 | 282 |
264 } // namespace translate | 283 } // namespace translate |
OLD | NEW |