Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #pragma once | 10 #pragma once |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <functional> | 13 #include <functional> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 | 18 |
| 19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/scoped_ptr.h" | 20 #include "base/scoped_ptr.h" |
| 21 #include "base/string16.h" | 21 #include "base/string16.h" |
| 22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
| 23 | 23 |
| 24 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 25 #include "app/l10n_util_mac.h" | 25 #include "app/l10n_util_mac.h" |
| 26 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
| 27 | 27 |
| 28 #define CRBUG_9911_OBSOLETE_GOING_AWAY 1 | |
|
Evan Martin
2010/12/24 01:00:42
Can you comment this? (Add a link to the bug, des
Avi (use Gerrit)
2010/12/28 15:02:55
Done.
| |
| 29 | |
| 28 namespace l10n_util { | 30 namespace l10n_util { |
| 29 | 31 |
| 30 // This method is responsible for determining the locale as defined below. In | 32 // This method is responsible for determining the locale as defined below. In |
| 31 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 33 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
| 32 // defined on browser_process. | 34 // defined on browser_process. |
| 33 // | 35 // |
| 34 // Returns the locale used by the Application. First we use the value from the | 36 // Returns the locale used by the Application. First we use the value from the |
| 35 // command line (--lang), second we try the value in the prefs file (passed in | 37 // command line (--lang), second we try the value in the prefs file (passed in |
| 36 // as |pref_locale|), finally, we fall back on the system locale. We only return | 38 // as |pref_locale|), finally, we fall back on the system locale. We only return |
| 37 // a value if there's a corresponding resource DLL for the locale. Otherwise, | 39 // a value if there's a corresponding resource DLL for the locale. Otherwise, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. | 75 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. |
| 74 // Case-insensitive. Based on BCP 47, see: | 76 // Case-insensitive. Based on BCP 47, see: |
| 75 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers | 77 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers |
| 76 bool IsValidLocaleSyntax(const std::string& locale); | 78 bool IsValidLocaleSyntax(const std::string& locale); |
| 77 | 79 |
| 78 // | 80 // |
| 79 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. | 81 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. |
| 80 // | 82 // |
| 81 | 83 |
| 82 // Pulls resource string from the string bundle and returns it. | 84 // Pulls resource string from the string bundle and returns it. |
| 85 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 83 std::wstring GetString(int message_id); | 86 std::wstring GetString(int message_id); |
| 87 #endif | |
| 84 std::string GetStringUTF8(int message_id); | 88 std::string GetStringUTF8(int message_id); |
| 85 string16 GetStringUTF16(int message_id); | 89 string16 GetStringUTF16(int message_id); |
| 86 | 90 |
| 87 // Get a resource string and replace $1-$2-$3 with |a| and |b| | 91 // Get a resource string and replace $1-$2-$3 with |a| and |b| |
| 88 // respectively. Additionally, $$ is replaced by $. | 92 // respectively. Additionally, $$ is replaced by $. |
| 89 string16 GetStringFUTF16(int message_id, | 93 string16 GetStringFUTF16(int message_id, |
| 90 const string16& a); | 94 const string16& a); |
| 91 string16 GetStringFUTF16(int message_id, | 95 string16 GetStringFUTF16(int message_id, |
| 92 const string16& a, | 96 const string16& a, |
| 93 const string16& b); | 97 const string16& b); |
| 94 string16 GetStringFUTF16(int message_id, | 98 string16 GetStringFUTF16(int message_id, |
| 95 const string16& a, | 99 const string16& a, |
| 96 const string16& b, | 100 const string16& b, |
| 97 const string16& c); | 101 const string16& c); |
| 98 string16 GetStringFUTF16(int message_id, | 102 string16 GetStringFUTF16(int message_id, |
| 99 const string16& a, | 103 const string16& a, |
| 100 const string16& b, | 104 const string16& b, |
| 101 const string16& c, | 105 const string16& c, |
| 102 const string16& d); | 106 const string16& d); |
| 107 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 103 #if defined(WCHAR_T_IS_UTF16) | 108 #if defined(WCHAR_T_IS_UTF16) |
| 104 inline std::wstring GetStringF(int message_id, | 109 inline std::wstring GetStringF(int message_id, |
| 105 const std::wstring& a) { | 110 const std::wstring& a) { |
| 106 return GetStringFUTF16(message_id, a); | 111 return GetStringFUTF16(message_id, a); |
| 107 } | 112 } |
| 108 inline std::wstring GetStringF(int message_id, | 113 inline std::wstring GetStringF(int message_id, |
| 109 const std::wstring& a, | 114 const std::wstring& a, |
| 110 const std::wstring& b) { | 115 const std::wstring& b) { |
| 111 return GetStringFUTF16(message_id, a, b); | 116 return GetStringFUTF16(message_id, a, b); |
| 112 } | 117 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 132 std::wstring GetStringF(int message_id, | 137 std::wstring GetStringF(int message_id, |
| 133 const std::wstring& a, | 138 const std::wstring& a, |
| 134 const std::wstring& b, | 139 const std::wstring& b, |
| 135 const std::wstring& c); | 140 const std::wstring& c); |
| 136 std::wstring GetStringF(int message_id, | 141 std::wstring GetStringF(int message_id, |
| 137 const std::wstring& a, | 142 const std::wstring& a, |
| 138 const std::wstring& b, | 143 const std::wstring& b, |
| 139 const std::wstring& c, | 144 const std::wstring& c, |
| 140 const std::wstring& d); | 145 const std::wstring& d); |
| 141 #endif | 146 #endif |
| 147 #endif | |
| 142 std::string GetStringFUTF8(int message_id, | 148 std::string GetStringFUTF8(int message_id, |
| 143 const string16& a); | 149 const string16& a); |
| 144 std::string GetStringFUTF8(int message_id, | 150 std::string GetStringFUTF8(int message_id, |
| 145 const string16& a, | 151 const string16& a, |
| 146 const string16& b); | 152 const string16& b); |
| 147 std::string GetStringFUTF8(int message_id, | 153 std::string GetStringFUTF8(int message_id, |
| 148 const string16& a, | 154 const string16& a, |
| 149 const string16& b, | 155 const string16& b, |
| 150 const string16& c); | 156 const string16& c); |
| 151 std::string GetStringFUTF8(int message_id, | 157 std::string GetStringFUTF8(int message_id, |
| 152 const string16& a, | 158 const string16& a, |
| 153 const string16& b, | 159 const string16& b, |
| 154 const string16& c, | 160 const string16& c, |
| 155 const string16& d); | 161 const string16& d); |
| 156 | 162 |
| 157 // Variants that return the offset(s) of the replaced parameters. The | 163 // Variants that return the offset(s) of the replaced parameters. The |
| 158 // vector based version returns offsets ordered by parameter. For example if | 164 // vector based version returns offsets ordered by parameter. For example if |
| 159 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the | 165 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the |
| 160 // offset of b regardless of where the parameters end up in the string. | 166 // offset of b regardless of where the parameters end up in the string. |
| 167 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 161 std::wstring GetStringF(int message_id, | 168 std::wstring GetStringF(int message_id, |
| 162 const std::wstring& a, | 169 const std::wstring& a, |
| 163 size_t* offset); | 170 size_t* offset); |
| 164 std::wstring GetStringF(int message_id, | 171 std::wstring GetStringF(int message_id, |
| 165 const std::wstring& a, | 172 const std::wstring& a, |
| 166 const std::wstring& b, | 173 const std::wstring& b, |
| 167 std::vector<size_t>* offsets); | 174 std::vector<size_t>* offsets); |
| 175 #endif | |
| 168 string16 GetStringFUTF16(int message_id, | 176 string16 GetStringFUTF16(int message_id, |
| 169 const string16& a, | 177 const string16& a, |
| 170 size_t* offset); | 178 size_t* offset); |
| 171 string16 GetStringFUTF16(int message_id, | 179 string16 GetStringFUTF16(int message_id, |
| 172 const string16& a, | 180 const string16& a, |
| 173 const string16& b, | 181 const string16& b, |
| 174 std::vector<size_t>* offsets); | 182 std::vector<size_t>* offsets); |
| 175 | 183 |
| 176 // Convenience formatters for a single number. | 184 // Convenience formatters for a single number. |
| 185 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 177 std::wstring GetStringF(int message_id, int a); | 186 std::wstring GetStringF(int message_id, int a); |
| 178 std::wstring GetStringF(int message_id, int64 a); | 187 std::wstring GetStringF(int message_id, int64 a); |
| 188 #endif | |
| 189 string16 GetStringFUTF16(int message_id, int a); | |
| 190 string16 GetStringFUTF16(int message_id, int64 a); | |
| 179 | 191 |
| 180 // Truncates the string to length characters. This breaks the string at | 192 // Truncates the string to length characters. This breaks the string at |
| 181 // the first word break before length, adding the horizontal ellipsis | 193 // the first word break before length, adding the horizontal ellipsis |
| 182 // character (unicode character 0x2026) to render ... | 194 // character (unicode character 0x2026) to render ... |
| 183 // The supplied string is returned if the string has length characters or | 195 // The supplied string is returned if the string has length characters or |
| 184 // less. | 196 // less. |
| 185 string16 TruncateString(const string16& string, size_t length); | 197 string16 TruncateString(const string16& string, size_t length); |
| 186 | 198 |
| 187 // Returns the lower case equivalent of string. | 199 // Returns the lower case equivalent of string. |
| 188 string16 ToLower(const string16& string); | 200 string16 ToLower(const string16& string); |
| 189 | 201 |
| 190 // Returns the upper case equivalent of string. | 202 // Returns the upper case equivalent of string. |
| 191 string16 ToUpper(const string16& string); | 203 string16 ToUpper(const string16& string); |
| 192 | 204 |
| 193 // In place sorting of std::wstring strings using collation rules for |locale|. | |
| 194 void SortStrings(const std::string& locale, | |
| 195 std::vector<std::wstring>* strings); | |
| 196 | |
| 197 // In place sorting of string16 strings using collation rules for |locale|. | 205 // In place sorting of string16 strings using collation rules for |locale|. |
| 198 void SortStrings16(const std::string& locale, | 206 void SortStrings16(const std::string& locale, |
| 199 std::vector<string16>* strings); | 207 std::vector<string16>* strings); |
| 200 | 208 |
| 201 // Returns a vector of available locale codes. E.g., a vector containing | 209 // Returns a vector of available locale codes. E.g., a vector containing |
| 202 // en-US, es, fr, fi, pt-PT, pt-BR, etc. | 210 // en-US, es, fr, fi, pt-PT, pt-BR, etc. |
| 203 const std::vector<std::string>& GetAvailableLocales(); | 211 const std::vector<std::string>& GetAvailableLocales(); |
| 204 | 212 |
| 205 // Returns a vector of locale codes usable for accept-languages. | 213 // Returns a vector of locale codes usable for accept-languages. |
| 206 void GetAcceptLanguagesForLocale(const std::string& display_locale, | 214 void GetAcceptLanguagesForLocale(const std::string& display_locale, |
| 207 std::vector<std::string>* locale_codes); | 215 std::vector<std::string>* locale_codes); |
| 208 | 216 |
| 209 | 217 |
| 210 } // namespace l10n_util | 218 } // namespace l10n_util |
| 211 | 219 |
| 212 #endif // APP_L10N_UTIL_H_ | 220 #endif // APP_L10N_UTIL_H_ |
| OLD | NEW |