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

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: 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 DCHECK(translate_manager_); 47 DCHECK(translate_manager_);
48 48
49 std::vector<std::string> language_codes; 49 std::vector<std::string> language_codes;
50 TranslateDownloadManager::GetSupportedLanguages(&language_codes); 50 TranslateDownloadManager::GetSupportedLanguages(&language_codes);
51 51
52 // Preparing for the alphabetical order in the locale. 52 // Preparing for the alphabetical order in the locale.
53 UErrorCode error = U_ZERO_ERROR; 53 UErrorCode error = U_ZERO_ERROR;
54 std::string locale = 54 std::string locale =
55 TranslateDownloadManager::GetInstance()->application_locale(); 55 TranslateDownloadManager::GetInstance()->application_locale();
56 icu::Locale loc(locale.c_str()); 56 icu::Locale loc(locale.c_str());
57 scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error)); 57 icu::Collator* collator_instance = icu::Collator::createInstance(loc, error);
Miguel Garcia 2015/11/02 12:09:26 this code is starting to be far from trivial and i
hajimehoshi 2015/11/04 05:50:30 createInstance's behavior depends on platforms, so
58 collator->setStrength(icu::Collator::PRIMARY); 58 scoped_ptr<icu::Collator> collator;
59 if (U_SUCCESS(error)) {
60 DCHECK(collator_instance);
61 collator = scoped_ptr<icu::Collator>(collator_instance);
62 collator->setStrength(icu::Collator::PRIMARY);
63 }
Miguel Garcia 2015/11/02 12:09:26 It seems you can get a collator object and still h
hajimehoshi 2015/11/04 05:50:30 Thanks. Changed to check if |collator_instance| is
59 64
60 languages_.reserve(language_codes.size()); 65 languages_.reserve(language_codes.size());
61 for (std::vector<std::string>::const_iterator iter = language_codes.begin(); 66 for (std::vector<std::string>::const_iterator iter = language_codes.begin();
62 iter != language_codes.end(); 67 iter != language_codes.end();
63 ++iter) { 68 ++iter) {
64 std::string language_code = *iter; 69 std::string language_code = *iter;
65 70
66 base::string16 language_name = 71 base::string16 language_name =
67 l10n_util::GetDisplayNameForLocale(language_code, locale, true); 72 l10n_util::GetDisplayNameForLocale(language_code, locale, true);
68 // Insert the language in languages_ in alphabetical order. 73 // Insert the language in languages_ in alphabetical order.
69 std::vector<LanguageNamePair>::iterator iter2; 74 std::vector<LanguageNamePair>::iterator iter2;
70 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) { 75 for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
71 if (base::i18n::CompareString16WithCollator(*collator, language_name, 76 if (collator) {
Miguel Garcia 2015/11/02 12:09:26 can you perhaps check if we should do ascii or icu
hajimehoshi 2015/11/04 05:50:30 Done.
72 iter2->second) == UCOL_LESS) { 77 int result = base::i18n::CompareString16WithCollator(*collator,
78 language_name,
79 iter2->second);
80 if (result == UCOL_LESS)
81 break;
82 continue;
83 }
84 // |locale| may not be supported by ICU collator (crbug/54833). In this
85 // case, let's order the languages in ASCII.
86 if (language_name.compare(iter2->second) < 0)
73 break; 87 break;
74 }
75 } 88 }
76 languages_.insert(iter2, LanguageNamePair(language_code, language_name)); 89 languages_.insert(iter2, LanguageNamePair(language_code, language_name));
77 } 90 }
78 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin(); 91 for (std::vector<LanguageNamePair>::const_iterator iter = languages_.begin();
79 iter != languages_.end(); 92 iter != languages_.end();
80 ++iter) { 93 ++iter) {
81 std::string language_code = iter->first; 94 std::string language_code = iter->first;
82 if (language_code == original_language) { 95 if (language_code == original_language) {
83 original_language_index_ = iter - languages_.begin(); 96 original_language_index_ = iter - languages_.begin();
84 initial_original_language_index_ = original_language_index_; 97 initial_original_language_index_ = original_language_index_;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true); 268 UMA_HISTOGRAM_BOOLEAN(kAlwaysTranslateLang, true);
256 } 269 }
257 270
258 std::string TranslateUIDelegate::GetPageHost() { 271 std::string TranslateUIDelegate::GetPageHost() {
259 if (!translate_driver_->HasCurrentPage()) 272 if (!translate_driver_->HasCurrentPage())
260 return std::string(); 273 return std::string();
261 return translate_driver_->GetLastCommittedURL().HostNoBrackets(); 274 return translate_driver_->GetLastCommittedURL().HostNoBrackets();
262 } 275 }
263 276
264 } // namespace translate 277 } // 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