diff options
-rw-r--r-- | app/l10n_util.cc | 52 | ||||
-rw-r--r-- | app/l10n_util.h | 7 | ||||
-rw-r--r-- | app/l10n_util_collator.h | 7 | ||||
-rw-r--r-- | chrome/browser/translate/languages_menu_model.cc | 26 |
4 files changed, 75 insertions, 17 deletions
diff --git a/app/l10n_util.cc b/app/l10n_util.cc index d4eeed1..2133066 100644 --- a/app/l10n_util.cc +++ b/app/l10n_util.cc @@ -751,32 +751,38 @@ string16 ToUpper(const string16& string) { return result; } -// Compares the character data stored in two different strings by specified -// Collator instance. +// Compares the character data stored in two different string16 strings by +// specified Collator instance. +UCollationResult CompareString16WithCollator(const icu::Collator* collator, + const string16& lhs, + const string16& rhs) { + DCHECK(collator); + UErrorCode error = U_ZERO_ERROR; + UCollationResult result = collator->compare( + static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), + static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), + error); + DCHECK(U_SUCCESS(error)); + return result; +} + +// Compares the character data stored in two different std:wstring strings by +// specified Collator instance. UCollationResult CompareStringWithCollator(const icu::Collator* collator, const std::wstring& lhs, const std::wstring& rhs) { DCHECK(collator); - UErrorCode error = U_ZERO_ERROR; + UCollationResult result; #if defined(WCHAR_T_IS_UTF32) // Need to convert to UTF-16 to be compatible with UnicodeString's // constructor. string16 lhs_utf16 = WideToUTF16(lhs); string16 rhs_utf16 = WideToUTF16(rhs); - UCollationResult result = collator->compare( - static_cast<const UChar*>(lhs_utf16.c_str()), - static_cast<int>(lhs_utf16.length()), - static_cast<const UChar*>(rhs_utf16.c_str()), - static_cast<int>(rhs_utf16.length()), - error); + result = CompareString16WithCollator(collator, lhs_utf16, rhs_utf16); #else - UCollationResult result = collator->compare( - static_cast<const UChar*>(lhs.c_str()), static_cast<int>(lhs.length()), - static_cast<const UChar*>(rhs.c_str()), static_cast<int>(rhs.length()), - error); + result = CompareString16WithCollator(collator, lhs, rhs); #endif - DCHECK(U_SUCCESS(error)); return result; } @@ -791,11 +797,29 @@ bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, return CompareStringWithCollator(collator_, lhs, rhs) == UCOL_LESS; }; +#if !defined(WCHAR_T_IS_UTF16) +// Specialization of operator() method for string16 version. +template <> +bool StringComparator<string16>::operator()(const string16& lhs, + const string16& rhs) { + // If we can not get collator instance for specified locale, just do simple + // string compare. + if (!collator_) + return lhs < rhs; + return CompareString16WithCollator(collator_, lhs, rhs) == UCOL_LESS; +}; +#endif // !defined(WCHAR_T_IS_UTF16) + void SortStrings(const std::string& locale, std::vector<std::wstring>* strings) { SortVectorWithStringKey(locale, strings, false); } +void SortStrings16(const std::string& locale, + std::vector<string16>* strings) { + SortVectorWithStringKey(locale, strings, false); +} + const std::vector<std::string>& GetAvailableLocales() { static std::vector<std::string> locales; if (locales.empty()) { diff --git a/app/l10n_util.h b/app/l10n_util.h index c176f73f..e0100c0 100644 --- a/app/l10n_util.h +++ b/app/l10n_util.h @@ -174,11 +174,14 @@ string16 ToLower(const string16& string); // Returns the upper case equivalent of string. string16 ToUpper(const string16& string); -// In place sorting of strings using collation rules for |locale|. -// TODO(port): this should take string16. +// In place sorting of std::wstring strings using collation rules for |locale|. void SortStrings(const std::string& locale, std::vector<std::wstring>* strings); +// In place sorting of string16 strings using collation rules for |locale|. +void SortStrings16(const std::string& locale, + std::vector<string16>* strings); + // Returns a vector of available locale codes. E.g., a vector containing // en-US, es, fr, fi, pt-PT, pt-BR, etc. const std::vector<std::string>& GetAvailableLocales(); diff --git a/app/l10n_util_collator.h b/app/l10n_util_collator.h index 8140cf0..6964f43 100644 --- a/app/l10n_util_collator.h +++ b/app/l10n_util_collator.h @@ -112,6 +112,13 @@ template <> bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, const std::wstring& rhs); +#if !defined(WCHAR_T_IS_UTF16) +// Specialization of operator() method for string16 version. +template <> +bool StringComparator<string16>::operator()(const string16& lhs, + const string16& rhs); +#endif // !defined(WCHAR_T_IS_UTF16) + // In place sorting of |elements| of a vector according to the string key of // each element in the vector by using collation rules for |locale|. // |begin_index| points to the start position of elements in the vector which diff --git a/chrome/browser/translate/languages_menu_model.cc b/chrome/browser/translate/languages_menu_model.cc index 5231ce0..2fb0925 100644 --- a/chrome/browser/translate/languages_menu_model.cc +++ b/chrome/browser/translate/languages_menu_model.cc @@ -4,8 +4,10 @@ #include "chrome/browser/translate/languages_menu_model.h" +#include "app/l10n_util.h" #include "base/string_util.h" #include "chrome/app/chrome_dll_resource.h" +#include "chrome/browser/browser_process.h" #include "chrome/browser/translate/translate_infobars_delegates.h" LanguagesMenuModel::LanguagesMenuModel( @@ -22,9 +24,31 @@ LanguagesMenuModel::LanguagesMenuModel( base_command_id = IDC_TRANSLATE_TARGET_LANGUAGE_BASE; translate_delegate->GetAvailableTargetLanguages(&languages); } + + // List of languages in chrome language. + std::vector<string16> display_languages; + // Reserve size since we know it. + display_languages.reserve(languages.size()); + + // Map of language's display name to its menu command id. + std::map<string16, int> language_cmdid_map; + + // Get display name of each locale and hash it into language cmdid map. std::vector<std::string>::const_iterator iter = languages.begin(); for (int i = base_command_id; iter != languages.end(); ++i, ++iter) { - AddItem(i, translate_delegate->GetDisplayNameForLocale(*iter)); + string16 display_name = translate_delegate->GetDisplayNameForLocale(*iter); + display_languages.push_back(display_name); + language_cmdid_map[display_name] = i; + } + + // Sort using locale-specific sorter. + l10n_util::SortStrings16(g_browser_process->GetApplicationLocale(), + &display_languages); + + // Add sorted list of display languages to menu. + for (std::vector<string16>::const_iterator iter = display_languages.begin(); + iter != display_languages.end(); ++iter) { + AddItem(language_cmdid_map[*iter], *iter); } } |