diff options
author | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-02 15:53:44 +0000 |
---|---|---|
committer | kuan@chromium.org <kuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-02 15:53:44 +0000 |
commit | f7eb4703666f6e8776ef084e6b60e19616edf7a4 (patch) | |
tree | 4f89249b66997767ac2aed0d6595a153b32f6ec7 /app | |
parent | 931fae8fbb1766629f2d82d772c7f612b440c4b2 (diff) | |
download | chromium_src-f7eb4703666f6e8776ef084e6b60e19616edf7a4.zip chromium_src-f7eb4703666f6e8776ef084e6b60e19616edf7a4.tar.gz chromium_src-f7eb4703666f6e8776ef084e6b60e19616edf7a4.tar.bz2 |
translate infobar: sort languages in menus by application locale
- provide string16 sorting methods (there were only std::wstring sorting methods before)
- problem is fixed on win, mac, linux and chromeos
BUG=39850
TEST=verify per bug report.
Review URL: http://codereview.chromium.org/1542009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43484 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-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 |
3 files changed, 50 insertions, 16 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 |