| 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 APP_L10N_UTIL_H_ | 8 #ifndef APP_L10N_UTIL_H_ |
| 9 #define APP_L10N_UTIL_H_ | 9 #define APP_L10N_UTIL_H_ |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 // This method is responsible for determining the locale as defined below. In | 40 // This method is responsible for determining the locale as defined below. In |
| 41 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 41 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
| 42 // defined on browser_process. | 42 // defined on browser_process. |
| 43 // | 43 // |
| 44 // Returns the locale used by the Application. First we use the value from the | 44 // Returns the locale used by the Application. First we use the value from the |
| 45 // command line (--lang), second we try the value in the prefs file (passed in | 45 // command line (--lang), second we try the value in the prefs file (passed in |
| 46 // as |pref_locale|), finally, we fall back on the system locale. We only return | 46 // as |pref_locale|), finally, we fall back on the system locale. We only return |
| 47 // a value if there's a corresponding resource DLL for the locale. Otherwise, | 47 // a value if there's a corresponding resource DLL for the locale. Otherwise, |
| 48 // we fall back to en-us. | 48 // we fall back to en-us. |
| 49 std::wstring GetApplicationLocale(const std::wstring& pref_locale); | 49 std::string GetApplicationLocale(const std::wstring& pref_locale); |
| 50 | 50 |
| 51 // Given a locale code, return true if the OS is capable of supporting it. | 51 // Given a locale code, return true if the OS is capable of supporting it. |
| 52 // For instance, Oriya is not well supported on Windows XP and we return | 52 // For instance, Oriya is not well supported on Windows XP and we return |
| 53 // false for "or". | 53 // false for "or". |
| 54 bool IsLocaleSupportedByOS(const std::wstring& locale); | 54 bool IsLocaleSupportedByOS(const std::string& locale); |
| 55 | 55 |
| 56 // This method returns the Local Name of the Locale Code. For example, for | 56 // This method returns the display name of the locale code in |display_locale|. |
| 57 // |local_code_wstr| = "en-US", it returns "English (United States)". | 57 |
| 58 // |app_locale_wstr| can be obtained in the UI thread - for example: | 58 // For example, for |locale_code| = "en-US" and |display_locale| = "en", |
| 59 // const std::wstring app_locale_wstr = g_browser_process-> | 59 // it returns "English (United States)". To get the display name of |
| 60 // GetApplicationLocale(); | 60 // |locale_code| in the UI language of Chrome, |display_locale| can be |
| 61 // set to the return value of g_browser_process->GetApplicationLocale() |
| 62 // in the UI thread. |
| 61 // If |is_for_ui| is true, U+200F is appended so that it can be | 63 // If |is_for_ui| is true, U+200F is appended so that it can be |
| 62 // rendered properly in a RTL Chrome. | 64 // rendered properly in a RTL Chrome. |
| 63 std::wstring GetLocalName(const std::string& locale_code_str, | 65 string16 GetDisplayNameForLocale(const std::string& locale_code, |
| 64 const std::wstring& app_locale_wstr, | 66 const std::string& display_locale, |
| 65 bool is_for_ui); | 67 bool is_for_ui); |
| 66 | 68 |
| 67 // Pulls resource string from the string bundle and returns it. | 69 // Pulls resource string from the string bundle and returns it. |
| 68 std::wstring GetString(int message_id); | 70 std::wstring GetString(int message_id); |
| 69 std::string GetStringUTF8(int message_id); | 71 std::string GetStringUTF8(int message_id); |
| 70 string16 GetStringUTF16(int message_id); | 72 string16 GetStringUTF16(int message_id); |
| 71 | 73 |
| 72 // Get a resource string and replace $1-$2-$3 with |a| and |b| | 74 // Get a resource string and replace $1-$2-$3 with |a| and |b| |
| 73 // respectively. Additionally, $$ is replaced by $. | 75 // respectively. Additionally, $$ is replaced by $. |
| 76 string16 GetStringFUTF16(int message_id, |
| 77 const string16& a); |
| 78 string16 GetStringFUTF16(int message_id, |
| 79 const string16& a, |
| 80 const string16& b); |
| 81 string16 GetStringFUTF16(int message_id, |
| 82 const string16& a, |
| 83 const string16& b, |
| 84 const string16& c); |
| 85 string16 GetStringFUTF16(int message_id, |
| 86 const string16& a, |
| 87 const string16& b, |
| 88 const string16& c, |
| 89 const string16& d); |
| 90 #if defined(WCHAR_T_IS_UTF16) |
| 91 inline std::wstring GetStringF(int message_id, |
| 92 const std::wstring& a) { |
| 93 return GetStringFUTF16(message_id, a); |
| 94 } |
| 95 inline std::wstring GetStringF(int message_id, |
| 96 const std::wstring& a, |
| 97 const std::wstring& b) { |
| 98 return GetStringFUTF16(message_id, a, b); |
| 99 } |
| 100 inline std::wstring GetStringF(int message_id, |
| 101 const std::wstring& a, |
| 102 const std::wstring& b, |
| 103 const std::wstring& c) { |
| 104 return GetStringFUTF16(message_id, a, b, c); |
| 105 } |
| 106 inline std::wstring GetStringF(int message_id, |
| 107 const std::wstring& a, |
| 108 const std::wstring& b, |
| 109 const std::wstring& c, |
| 110 const std::wstring& d) { |
| 111 return GetStringFUTF16(message_id, a, b, c, d); |
| 112 } |
| 113 #else |
| 74 std::wstring GetStringF(int message_id, | 114 std::wstring GetStringF(int message_id, |
| 75 const std::wstring& a); | 115 const std::wstring& a); |
| 76 std::wstring GetStringF(int message_id, | 116 std::wstring GetStringF(int message_id, |
| 77 const std::wstring& a, | 117 const std::wstring& a, |
| 78 const std::wstring& b); | 118 const std::wstring& b); |
| 79 std::wstring GetStringF(int message_id, | 119 std::wstring GetStringF(int message_id, |
| 80 const std::wstring& a, | 120 const std::wstring& a, |
| 81 const std::wstring& b, | 121 const std::wstring& b, |
| 82 const std::wstring& c); | 122 const std::wstring& c); |
| 83 std::wstring GetStringF(int message_id, | 123 std::wstring GetStringF(int message_id, |
| 84 const std::wstring& a, | 124 const std::wstring& a, |
| 85 const std::wstring& b, | 125 const std::wstring& b, |
| 86 const std::wstring& c, | 126 const std::wstring& c, |
| 87 const std::wstring& d); | 127 const std::wstring& d); |
| 128 #endif |
| 88 std::string GetStringFUTF8(int message_id, | 129 std::string GetStringFUTF8(int message_id, |
| 89 const string16& a); | 130 const string16& a); |
| 90 std::string GetStringFUTF8(int message_id, | 131 std::string GetStringFUTF8(int message_id, |
| 91 const string16& a, | 132 const string16& a, |
| 92 const string16& b); | 133 const string16& b); |
| 93 std::string GetStringFUTF8(int message_id, | 134 std::string GetStringFUTF8(int message_id, |
| 94 const string16& a, | 135 const string16& a, |
| 95 const string16& b, | 136 const string16& b, |
| 96 const string16& c); | 137 const string16& c); |
| 97 std::string GetStringFUTF8(int message_id, | 138 std::string GetStringFUTF8(int message_id, |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 template <> | 348 template <> |
| 308 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, | 349 bool StringComparator<std::wstring>::operator()(const std::wstring& lhs, |
| 309 const std::wstring& rhs); | 350 const std::wstring& rhs); |
| 310 | 351 |
| 311 // In place sorting of |elements| of a vector according to the string key of | 352 // In place sorting of |elements| of a vector according to the string key of |
| 312 // each element in the vector by using collation rules for |locale|. | 353 // each element in the vector by using collation rules for |locale|. |
| 313 // |begin_index| points to the start position of elements in the vector which | 354 // |begin_index| points to the start position of elements in the vector which |
| 314 // want to be sorted. |end_index| points to the end position of elements in the | 355 // want to be sorted. |end_index| points to the end position of elements in the |
| 315 // vector which want to be sorted | 356 // vector which want to be sorted |
| 316 template <class Element> | 357 template <class Element> |
| 317 void SortVectorWithStringKey(const std::wstring& locale, | 358 void SortVectorWithStringKey(const std::string& locale, |
| 318 std::vector<Element>* elements, | 359 std::vector<Element>* elements, |
| 319 unsigned int begin_index, | 360 unsigned int begin_index, |
| 320 unsigned int end_index, | 361 unsigned int end_index, |
| 321 bool needs_stable_sort) { | 362 bool needs_stable_sort) { |
| 322 DCHECK(begin_index >= 0 && begin_index < end_index && | 363 DCHECK(begin_index >= 0 && begin_index < end_index && |
| 323 end_index <= static_cast<unsigned int>(elements->size())); | 364 end_index <= static_cast<unsigned int>(elements->size())); |
| 324 UErrorCode error = U_ZERO_ERROR; | 365 UErrorCode error = U_ZERO_ERROR; |
| 325 Locale loc(WideToASCII(locale).c_str()); | 366 Locale loc(locale.c_str()); |
| 326 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); | 367 scoped_ptr<Collator> collator(Collator::createInstance(loc, error)); |
| 327 if (U_FAILURE(error)) | 368 if (U_FAILURE(error)) |
| 328 collator.reset(); | 369 collator.reset(); |
| 329 StringComparator<Element> c(collator.get()); | 370 StringComparator<Element> c(collator.get()); |
| 330 if (needs_stable_sort) { | 371 if (needs_stable_sort) { |
| 331 stable_sort(elements->begin() + begin_index, | 372 stable_sort(elements->begin() + begin_index, |
| 332 elements->begin() + end_index, | 373 elements->begin() + end_index, |
| 333 c); | 374 c); |
| 334 } else { | 375 } else { |
| 335 sort(elements->begin() + begin_index, elements->begin() + end_index, c); | 376 sort(elements->begin() + begin_index, elements->begin() + end_index, c); |
| 336 } | 377 } |
| 337 } | 378 } |
| 338 | 379 |
| 339 template <class Element> | 380 template <class Element> |
| 340 void SortVectorWithStringKey(const std::wstring& locale, | 381 void SortVectorWithStringKey(const std::string& locale, |
| 341 std::vector<Element>* elements, | 382 std::vector<Element>* elements, |
| 342 bool needs_stable_sort) { | 383 bool needs_stable_sort) { |
| 343 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), | 384 SortVectorWithStringKey<Element>(locale, elements, 0, elements->size(), |
| 344 needs_stable_sort); | 385 needs_stable_sort); |
| 345 } | 386 } |
| 346 | 387 |
| 347 // In place sorting of strings using collation rules for |locale|. | 388 // In place sorting of strings using collation rules for |locale|. |
| 348 // TODO(port): this should take string16. | 389 // TODO(port): this should take string16. |
| 349 void SortStrings(const std::wstring& locale, | 390 void SortStrings(const std::string& locale, |
| 350 std::vector<std::wstring>* strings); | 391 std::vector<std::wstring>* strings); |
| 351 | 392 |
| 352 // Returns a vector of available locale codes. E.g., a vector containing | 393 // Returns a vector of available locale codes. E.g., a vector containing |
| 353 // en-US, es, fr, fi, pt-PT, pt-BR, etc. | 394 // en-US, es, fr, fi, pt-PT, pt-BR, etc. |
| 354 const std::vector<std::string>& GetAvailableLocales(); | 395 const std::vector<std::string>& GetAvailableLocales(); |
| 355 | 396 |
| 356 // A simple wrapper class for the bidirectional iterator of ICU. | 397 // A simple wrapper class for the bidirectional iterator of ICU. |
| 357 // This class uses the bidirectional iterator of ICU to split a line of | 398 // This class uses the bidirectional iterator of ICU to split a line of |
| 358 // bidirectional texts into visual runs in its display order. | 399 // bidirectional texts into visual runs in its display order. |
| 359 class BiDiLineIterator { | 400 class BiDiLineIterator { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 376 | 417 |
| 377 private: | 418 private: |
| 378 UBiDi* bidi_; | 419 UBiDi* bidi_; |
| 379 | 420 |
| 380 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); | 421 DISALLOW_COPY_AND_ASSIGN(BiDiLineIterator); |
| 381 }; | 422 }; |
| 382 | 423 |
| 383 } | 424 } |
| 384 | 425 |
| 385 #endif // APP_L10N_UTIL_H_ | 426 #endif // APP_L10N_UTIL_H_ |
| OLD | NEW |