OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This file contains utility functions for dealing with localized | 5 // This file contains utility functions for dealing with localized |
6 // content. | 6 // content. |
7 | 7 |
8 #ifndef CHROME_COMMON_L10N_UTIL_H_ | 8 #ifndef CHROME_COMMON_L10N_UTIL_H_ |
9 #define CHROME_COMMON_L10N_UTIL_H_ | 9 #define CHROME_COMMON_L10N_UTIL_H_ |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
15 #endif | 15 #endif |
| 16 #include <algorithm> |
16 #include <string> | 17 #include <string> |
17 #include <vector> | 18 #include <vector> |
18 | 19 |
19 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 21 #include "base/logging.h" |
| 22 #include "base/scoped_ptr.h" |
| 23 #include "base/string_util.h" |
20 #include "third_party/icu38/public/common/unicode/ubidi.h" | 24 #include "third_party/icu38/public/common/unicode/ubidi.h" |
| 25 #include "unicode/coll.h" |
| 26 #include "unicode/locid.h" |
21 | 27 |
22 class PrefService; | 28 class PrefService; |
23 | 29 |
24 namespace l10n_util { | 30 namespace l10n_util { |
25 | 31 |
26 const wchar_t kRightToLeftMark[] = L"\x200f"; | 32 const wchar_t kRightToLeftMark[] = L"\x200f"; |
27 const wchar_t kLeftToRightMark[] = L"\x200e"; | 33 const wchar_t kLeftToRightMark[] = L"\x200e"; |
28 | 34 |
29 // This method is responsible for determining the locale as defined below. In | 35 // This method is responsible for determining the locale as defined below. In |
30 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 36 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 int DefaultCanvasTextAlignment(); | 171 int DefaultCanvasTextAlignment(); |
166 | 172 |
167 #if defined(OS_WIN) | 173 #if defined(OS_WIN) |
168 // Give an HWND, this function sets the WS_EX_LAYOUTRTL extended style for the | 174 // Give an HWND, this function sets the WS_EX_LAYOUTRTL extended style for the |
169 // underlying window. When this style is set, the UI for the window is going to | 175 // underlying window. When this style is set, the UI for the window is going to |
170 // be mirrored. This is generally done for the UI of right-to-left languages | 176 // be mirrored. This is generally done for the UI of right-to-left languages |
171 // such as Hebrew. | 177 // such as Hebrew. |
172 void HWNDSetRTLLayout(HWND hwnd); | 178 void HWNDSetRTLLayout(HWND hwnd); |
173 #endif | 179 #endif |
174 | 180 |
| 181 // Compares two elements' string keys and returns true if the first element's |
| 182 // string key is less than the second element's string key. The Element must |
| 183 // have a method like the follow format to return the string key. |
| 184 // const std::wstring& GetStringKey() const; |
| 185 // This uses the locale specified in the constructor. |
| 186 template <class Element> |
| 187 class StringComparator : public std::binary_function<const Element&, |
| 188 const Element&, |
| 189 bool> { |
| 190 public: |
| 191 explicit StringComparator(Collator* collator) |
| 192 : collator_(collator) { } |
| 193 |
| 194 // Returns true if lhs precedes rhs. |
| 195 bool operator()(const Element& lhs, const Element& rhs) { |
| 196 const std::wstring& lhs_string_key = lhs.GetStringKey(); |
| 197 const std::wstring& rhs_string_key = rhs.GetStringKey(); |
| 198 |
| 199 return StringComparator<std::wstring>(collator_)(lhs_string_key, |
| 200 rhs_string_key); |
| 201 } |
| 202 |
| 203 private: |
| 204 Collator* collator_; |
| 205 }; |
| 206 |
| 207 // Specialization of operator() method for std::wstring version. |
| 208 template <> |
| 209 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, |
| 210 const std::wstring& rhs); |
| 211 |
| 212 // In place sorting of |elements| of a vector according to the string key of |
| 213 // each element in the vector by using collation rules for |locale|. |
| 214 // |begin_index| points to the start position of elements in the vector which |
| 215 // want to be sorted. |end_index| points to the end position of elements in the |
| 216 // vector which want to be sorted |
| 217 template <class Element> |
| 218 void SortVectorWithStringKey(const std::wstring& locale, |
| 219 std::vector<Element>* elements, |
| 220 unsigned int begin_index, |
| 221 unsigned int end_index, |
| 222 bool needs_stable_sort) { |
| 223 DCHECK(begin_index >= 0 && begin_index < end_index && |
| 224 end_index <= static_cast<unsigned int>(elements->size())); |
| 225 UErrorCode error = U_ZERO_ERROR; |
| 226 Locale loc(WideToASCII(locale).c_str()); |
| 227 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); |
| 228 if (U_FAILURE(error)) |
| 229 collator.reset(); |
| 230 StringComparator<Element> c(collator.get()); |
| 231 if (needs_stable_sort) { |
| 232 stable_sort(elements->begin() + begin_index, |
| 233 elements->begin() + end_index, |
| 234 c); |
| 235 } else { |
| 236 sort(elements->begin() + begin_index, elements->begin() + end_index, c); |
| 237 } |
| 238 } |
| 239 |
| 240 template <class Element> |
| 241 void SortVectorWithStringKey(const std::wstring& locale, |
| 242 std::vector<Element>* elements, |
| 243 bool needs_stable_sort) { |
| 244 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
| 245 needs_stable_sort); |
| 246 } |
| 247 |
175 // In place sorting of strings using collation rules for |locale|. | 248 // In place sorting of strings using collation rules for |locale|. |
176 void SortStrings(const std::wstring& locale, | 249 void SortStrings(const std::wstring& locale, |
177 std::vector<std::wstring>* strings); | 250 std::vector<std::wstring>* strings); |
178 | 251 |
179 // Returns a vector of available locale codes. E.g., a vector containing | 252 // Returns a vector of available locale codes. E.g., a vector containing |
180 // en-US, es, fr, fi, pt-PT, pt-BR, etc. | 253 // en-US, es, fr, fi, pt-PT, pt-BR, etc. |
181 const std::vector<std::wstring>& GetAvailableLocales(); | 254 const std::vector<std::wstring>& GetAvailableLocales(); |
182 | 255 |
183 // A simple wrapper class for the bidirectional iterator of ICU. | 256 // A simple wrapper class for the bidirectional iterator of ICU. |
184 // This class uses the bidirectional iterator of ICU to split a line of | 257 // This class uses the bidirectional iterator of ICU to split a line of |
(...skipping 18 matching lines...) Expand all Loading... |
203 | 276 |
204 private: | 277 private: |
205 UBiDi* bidi_; | 278 UBiDi* bidi_; |
206 | 279 |
207 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); | 280 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); |
208 }; | 281 }; |
209 | 282 |
210 } | 283 } |
211 | 284 |
212 #endif // CHROME_COMMON_L10N_UTIL_H_ | 285 #endif // CHROME_COMMON_L10N_UTIL_H_ |
OLD | NEW |