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)); |