| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // http://crbug.com/9911 . Any functions bracketed by this define are going | |
| 29 // away; this define exists to allow compiler assistance in removing their use. | |
| 30 #if defined(TOOLKIT_VIEWS) | |
| 31 #define CRBUG_9911_OBSOLETE_GOING_AWAY 1 | |
| 32 #endif | |
| 33 | |
| 34 namespace l10n_util { | 28 namespace l10n_util { |
| 35 | 29 |
| 36 // This method is responsible for determining the locale as defined below. In | 30 // This method is responsible for determining the locale as defined below. In |
| 37 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 31 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
| 38 // defined on browser_process. | 32 // defined on browser_process. |
| 39 // | 33 // |
| 40 // Returns the locale used by the Application. First we use the value from the | 34 // Returns the locale used by the Application. First we use the value from the |
| 41 // command line (--lang), second we try the value in the prefs file (passed in | 35 // command line (--lang), second we try the value in the prefs file (passed in |
| 42 // as |pref_locale|), finally, we fall back on the system locale. We only return | 36 // as |pref_locale|), finally, we fall back on the system locale. We only return |
| 43 // a value if there's a corresponding resource DLL for the locale. Otherwise, | 37 // 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... |
| 79 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. | 73 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. |
| 80 // Case-insensitive. Based on BCP 47, see: | 74 // Case-insensitive. Based on BCP 47, see: |
| 81 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers | 75 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers |
| 82 bool IsValidLocaleSyntax(const std::string& locale); | 76 bool IsValidLocaleSyntax(const std::string& locale); |
| 83 | 77 |
| 84 // | 78 // |
| 85 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. | 79 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. |
| 86 // | 80 // |
| 87 | 81 |
| 88 // Pulls resource string from the string bundle and returns it. | 82 // Pulls resource string from the string bundle and returns it. |
| 89 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 90 std::wstring GetString(int message_id); | |
| 91 #endif | |
| 92 std::string GetStringUTF8(int message_id); | 83 std::string GetStringUTF8(int message_id); |
| 93 string16 GetStringUTF16(int message_id); | 84 string16 GetStringUTF16(int message_id); |
| 94 | 85 |
| 95 // Get a resource string and replace $1-$2-$3 with |a| and |b| | 86 // Get a resource string and replace $1-$2-$3 with |a| and |b| |
| 96 // respectively. Additionally, $$ is replaced by $. | 87 // respectively. Additionally, $$ is replaced by $. |
| 97 string16 GetStringFUTF16(int message_id, | 88 string16 GetStringFUTF16(int message_id, |
| 98 const string16& a); | 89 const string16& a); |
| 99 string16 GetStringFUTF16(int message_id, | 90 string16 GetStringFUTF16(int message_id, |
| 100 const string16& a, | 91 const string16& a, |
| 101 const string16& b); | 92 const string16& b); |
| 102 string16 GetStringFUTF16(int message_id, | 93 string16 GetStringFUTF16(int message_id, |
| 103 const string16& a, | 94 const string16& a, |
| 104 const string16& b, | 95 const string16& b, |
| 105 const string16& c); | 96 const string16& c); |
| 106 string16 GetStringFUTF16(int message_id, | 97 string16 GetStringFUTF16(int message_id, |
| 107 const string16& a, | 98 const string16& a, |
| 108 const string16& b, | 99 const string16& b, |
| 109 const string16& c, | 100 const string16& c, |
| 110 const string16& d); | 101 const string16& d); |
| 111 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 112 #if defined(WCHAR_T_IS_UTF16) | |
| 113 inline std::wstring GetStringF(int message_id, | |
| 114 const std::wstring& a) { | |
| 115 return GetStringFUTF16(message_id, a); | |
| 116 } | |
| 117 inline std::wstring GetStringF(int message_id, | |
| 118 const std::wstring& a, | |
| 119 const std::wstring& b) { | |
| 120 return GetStringFUTF16(message_id, a, b); | |
| 121 } | |
| 122 inline std::wstring GetStringF(int message_id, | |
| 123 const std::wstring& a, | |
| 124 const std::wstring& b, | |
| 125 const std::wstring& c) { | |
| 126 return GetStringFUTF16(message_id, a, b, c); | |
| 127 } | |
| 128 inline std::wstring GetStringF(int message_id, | |
| 129 const std::wstring& a, | |
| 130 const std::wstring& b, | |
| 131 const std::wstring& c, | |
| 132 const std::wstring& d) { | |
| 133 return GetStringFUTF16(message_id, a, b, c, d); | |
| 134 } | |
| 135 #else | |
| 136 std::wstring GetStringF(int message_id, | |
| 137 const std::wstring& a); | |
| 138 std::wstring GetStringF(int message_id, | |
| 139 const std::wstring& a, | |
| 140 const std::wstring& b); | |
| 141 std::wstring GetStringF(int message_id, | |
| 142 const std::wstring& a, | |
| 143 const std::wstring& b, | |
| 144 const std::wstring& c); | |
| 145 std::wstring GetStringF(int message_id, | |
| 146 const std::wstring& a, | |
| 147 const std::wstring& b, | |
| 148 const std::wstring& c, | |
| 149 const std::wstring& d); | |
| 150 #endif | |
| 151 #endif | |
| 152 std::string GetStringFUTF8(int message_id, | 102 std::string GetStringFUTF8(int message_id, |
| 153 const string16& a); | 103 const string16& a); |
| 154 std::string GetStringFUTF8(int message_id, | 104 std::string GetStringFUTF8(int message_id, |
| 155 const string16& a, | 105 const string16& a, |
| 156 const string16& b); | 106 const string16& b); |
| 157 std::string GetStringFUTF8(int message_id, | 107 std::string GetStringFUTF8(int message_id, |
| 158 const string16& a, | 108 const string16& a, |
| 159 const string16& b, | 109 const string16& b, |
| 160 const string16& c); | 110 const string16& c); |
| 161 std::string GetStringFUTF8(int message_id, | 111 std::string GetStringFUTF8(int message_id, |
| 162 const string16& a, | 112 const string16& a, |
| 163 const string16& b, | 113 const string16& b, |
| 164 const string16& c, | 114 const string16& c, |
| 165 const string16& d); | 115 const string16& d); |
| 166 | 116 |
| 167 // Variants that return the offset(s) of the replaced parameters. The | 117 // Variants that return the offset(s) of the replaced parameters. The |
| 168 // vector based version returns offsets ordered by parameter. For example if | 118 // vector based version returns offsets ordered by parameter. For example if |
| 169 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the | 119 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the |
| 170 // offset of b regardless of where the parameters end up in the string. | 120 // offset of b regardless of where the parameters end up in the string. |
| 171 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 172 std::wstring GetStringF(int message_id, | |
| 173 const std::wstring& a, | |
| 174 size_t* offset); | |
| 175 std::wstring GetStringF(int message_id, | |
| 176 const std::wstring& a, | |
| 177 const std::wstring& b, | |
| 178 std::vector<size_t>* offsets); | |
| 179 #endif | |
| 180 string16 GetStringFUTF16(int message_id, | 121 string16 GetStringFUTF16(int message_id, |
| 181 const string16& a, | 122 const string16& a, |
| 182 size_t* offset); | 123 size_t* offset); |
| 183 string16 GetStringFUTF16(int message_id, | 124 string16 GetStringFUTF16(int message_id, |
| 184 const string16& a, | 125 const string16& a, |
| 185 const string16& b, | 126 const string16& b, |
| 186 std::vector<size_t>* offsets); | 127 std::vector<size_t>* offsets); |
| 187 | 128 |
| 188 // Convenience functions to get a string with a single number as a parameter. | 129 // Convenience functions to get a string with a single number as a parameter. |
| 189 #if CRBUG_9911_OBSOLETE_GOING_AWAY | |
| 190 std::wstring GetStringF(int message_id, int a); | |
| 191 std::wstring GetStringF(int message_id, int64 a); | |
| 192 #endif | |
| 193 string16 GetStringFUTF16Int(int message_id, int a); | 130 string16 GetStringFUTF16Int(int message_id, int a); |
| 194 string16 GetStringFUTF16Int(int message_id, int64 a); | 131 string16 GetStringFUTF16Int(int message_id, int64 a); |
| 195 | 132 |
| 196 // Truncates the string to length characters. This breaks the string at | 133 // Truncates the string to length characters. This breaks the string at |
| 197 // the first word break before length, adding the horizontal ellipsis | 134 // the first word break before length, adding the horizontal ellipsis |
| 198 // character (unicode character 0x2026) to render ... | 135 // character (unicode character 0x2026) to render ... |
| 199 // The supplied string is returned if the string has length characters or | 136 // The supplied string is returned if the string has length characters or |
| 200 // less. | 137 // less. |
| 201 string16 TruncateString(const string16& string, size_t length); | 138 string16 TruncateString(const string16& string, size_t length); |
| 202 | 139 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 215 const std::vector<std::string>& GetAvailableLocales(); | 152 const std::vector<std::string>& GetAvailableLocales(); |
| 216 | 153 |
| 217 // Returns a vector of locale codes usable for accept-languages. | 154 // Returns a vector of locale codes usable for accept-languages. |
| 218 void GetAcceptLanguagesForLocale(const std::string& display_locale, | 155 void GetAcceptLanguagesForLocale(const std::string& display_locale, |
| 219 std::vector<std::string>* locale_codes); | 156 std::vector<std::string>* locale_codes); |
| 220 | 157 |
| 221 | 158 |
| 222 } // namespace l10n_util | 159 } // namespace l10n_util |
| 223 | 160 |
| 224 #endif // APP_L10N_UTIL_H_ | 161 #endif // APP_L10N_UTIL_H_ |
| OLD | NEW |