Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: components/translate/core/browser/translate_ui_delegate.cc

Issue 1421473009: Translate: Order language names in ASCII for locales ICU doesn't support (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Miguel's review Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
Miguel Garcia 2015/11/04 10:30:53 nit: Add // or null if unable to find the right c
33 //
34 // TODO(hajimehoshi): Write a test for icu::Collator::createInstance.
35 scoped_ptr<icu::Collator> CreateCollator(const std::string& locale) {
36 UErrorCode error = U_ZERO_ERROR;
37 icu::Locale loc(locale.c_str());
38 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
39 if (!collator || !U_SUCCESS(error))
40 return nullptr;
41 collator->setStrength(icu::Collator::PRIMARY);
42 return collator.Pass();
43 }
44
32 } // namespace 45 } // namespace
33 46
34 namespace translate { 47 namespace translate {
35 48
36 TranslateUIDelegate::TranslateUIDelegate( 49 TranslateUIDelegate::TranslateUIDelegate(
37 const base::WeakPtr<TranslateManager>& translate_manager, 50 const base::WeakPtr<TranslateManager>& translate_manager,
38 const std::string& original_language, 51 const std::string& original_language,
39 const std::string& target_language) 52 const std::string& target_language)
40 : translate_driver_( 53 : translate_driver_(
41 translate_manager->translate_client()->GetTranslateDriver()), 54 translate_manager->translate_client()->GetTranslateDriver()),
42 translate_manager_(translate_manager), 55 translate_manager_(translate_manager),
43 original_language_index_(kNoIndex), 56 original_language_index_(kNoIndex),
44 initial_original_language_index_(kNoIndex), 57 initial_original_language_index_(kNoIndex),
45 target_language_index_(kNoIndex) { 58 target_language_index_(kNoIndex) {
46 DCHECK(translate_driver_); 59 DCHECK(translate_driver_);
47 DCHECK(translate_manager_); 60 DCHECK(translate_manager_);
48 61
49 std::vector<std::string> language_codes; 62 std::vector<std::string> language_codes;
50 TranslateDownloadManager::GetSupportedLanguages(&language_codes); 63 TranslateDownloadManager::GetSupportedLanguages(&language_codes);
51 64
52 // Preparing for the alphabetical order in the locale. 65 // Preparing for the alphabetical order in the locale.
53 UErrorCode error = U_ZERO_ERROR;
54 std::string locale = 66 std::string locale =
55 TranslateDownloadManager::GetInstance()->application_locale(); 67 TranslateDownloadManager::GetInstance()->application_locale();
56 icu::Locale loc(locale.c_str()); 68 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 69
60 languages_.reserve(language_codes.size()); 70 languages_.reserve(language_codes.size());
61 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); 71 for (std::vector<std::string>::const_iterator iter = language_codes.begin();
62 iter != language_codes.end(); 72 iter != language_codes.end();
63 ++iter) { 73 ++iter) {
64 std::string language_code = *iter; 74 std::string language_code = *iter;
65 75
66 base::string16 language_name = 76 base::string16 language_name =
67 l10n_util::GetDisplayNameForLocale(language_code, locale, true); 77 l10n_util::GetDisplayNameForLocale(language_code, locale, true);
68 // Insert the language in languages_ in alphabetical order. 78 // Insert the language in languages_ in alphabetical order.
69 std::vector<LanguageNamePair>::iterator iter2; 79 std::vector<LanguageNamePair>::iterator iter2;
70 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { 80 if (collator) {
71 if (base::i18n::CompareString16WithCollator(*collator, language_name, 81 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
72 iter2->second) == UCOL_LESS) { 82 int result = base::i18n::CompareString16WithCollator(*collator,
73 break; 83 language_name,
84 iter2->second);
85 if (result == UCOL_LESS)
86 break;
87 }
88 } else {
89 // |locale| may not be supported by ICU collator (crbug/54833). In this
90 // case, let's order the languages in ASCII.
Miguel Garcia 2015/11/04 10:30:53 Can you change ASCII to UTF-8 here? The actual sor
91 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
92 if (language_name.compare(iter2->second) < 0)
93 break;
74 } 94 }
75 } 95 }
76 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); 96 languages_.insert(iter2, LanguageNamePair(language_code, language_name));
77 } 97 }
78 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); 98 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin();
79 iter != languages_.end(); 99 iter != languages_.end();
80 ++iter) { 100 ++iter) {
81 std::string language_code = iter->first; 101 std::string language_code = iter->first;
82 if (language_code == original_language) { 102 if (language_code == original_language) {
83 original_language_index_ = iter - languages_.begin(); 103 original_language_index_ = iter - languages_.begin();
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true); 275 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true);
256 } 276 }
257 277
258 std::string TranslateUIDelegate::GetPageHost() { 278 std::string TranslateUIDelegate::GetPageHost() {
259 if (!translate_driver_->HasCurrentPage()) 279 if (!translate_driver_->HasCurrentPage())
260 return std::string(); 280 return std::string();
261 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); 281 return translate_driver_->GetLastCommittedURL().HostNoBrackets();
262 } 282 }
263 283
264 } // namespace translate 284 } // namespace translate
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698