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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/translate/core/browser/translate_ui_delegate.cc
diff --git a/components/translate/core/browser/translate_ui_delegate.cc b/components/translate/core/browser/translate_ui_delegate.cc
index fb33f7a755190a32d49f905fcfcafbfcac70ab9e..d1912dc738d73ffd304229a6d3876e9f95f92f3a 100644
--- a/components/translate/core/browser/translate_ui_delegate.cc
+++ b/components/translate/core/browser/translate_ui_delegate.cc
@@ -29,6 +29,20 @@ const char kModifyTargetLang[] = "Translate.ModifyTargetLang";
const char kDeclineTranslateDismissUI[] = "Translate.DeclineTranslateDismissUI";
const char kShowErrorUI[] = "Translate.ShowErrorUI";
+// Returns a Collator object which helps to sort strings in a given locale or
+// null if unable to find the right collator.
+//
+// TODO(hajimehoshi): Write a test for icu::Collator::createInstance.
+scoped_ptr<icu::Collator> CreateCollator(const std::string& locale) {
+ UErrorCode error = U_ZERO_ERROR;
+ icu::Locale loc(locale.c_str());
+ scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
+ if (!collator || !U_SUCCESS(error))
+ return nullptr;
Andrew Hayden (chromium.org) 2015/11/09 14:09:38 It would have been nice to add a comment here indi
+ collator->setStrength(icu::Collator::PRIMARY);
+ return collator.Pass();
+}
+
} // namespace
namespace translate {
@@ -50,12 +64,9 @@ TranslateUIDelegate::TranslateUIDelegate(
TranslateDownloadManager::GetSupportedLanguages(&language_codes);
// Preparing for the alphabetical order in the locale.
- UErrorCode error = U_ZERO_ERROR;
std::string locale =
TranslateDownloadManager::GetInstance()->application_locale();
- icu::Locale loc(locale.c_str());
- scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(loc, error));
- collator->setStrength(icu::Collator::PRIMARY);
+ scoped_ptr<icu::Collator> collator = CreateCollator(locale);
languages_.reserve(language_codes.size());
for (std::vector<std::string>::const_iterator iter = language_codes.begin();
@@ -67,10 +78,20 @@ TranslateUIDelegate::TranslateUIDelegate(
l10n_util::GetDisplayNameForLocale(language_code, locale, true);
// Insert the language in languages_ in alphabetical order.
std::vector<LanguageNamePair>::iterator iter2;
- for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
- if (base::i18n::CompareString16WithCollator(*collator, language_name,
- iter2->second) == UCOL_LESS) {
- break;
+ if (collator) {
+ for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
+ int result = base::i18n::CompareString16WithCollator(*collator,
+ language_name,
+ iter2->second);
+ if (result == UCOL_LESS)
+ break;
+ }
+ } else {
+ // |locale| may not be supported by ICU collator (crbug/54833). In this
+ // case, let's order the languages in UTF-8.
+ for (iter2 = languages_.begin(); iter2 != languages_.end(); ++iter2) {
+ if (language_name.compare(iter2->second) < 0)
+ break;
}
}
languages_.insert(iter2, LanguageNamePair(language_code, language_name));
« 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