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 UI_BASE_L10N_L10N_UTIL_H_ | 8 #ifndef UI_BASE_L10N_L10N_UTIL_H_ |
9 #define UI_BASE_L10N_L10N_UTIL_H_ | 9 #define UI_BASE_L10N_L10N_UTIL_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "ui/ui_api.h" |
17 | 18 |
18 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
19 #include "ui/base/l10n/l10n_util_mac.h" | 20 #include "ui/base/l10n/l10n_util_mac.h" |
20 #endif // OS_MACOSX | 21 #endif // OS_MACOSX |
21 | 22 |
22 namespace l10n_util { | 23 namespace l10n_util { |
23 | 24 |
24 // This method is responsible for determining the locale as defined below. In | 25 // This method is responsible for determining the locale as defined below. In |
25 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 26 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
26 // defined on browser_process. | 27 // defined on browser_process. |
27 // | 28 // |
28 // Returns the locale used by the Application. First we use the value from the | 29 // Returns the locale used by the Application. First we use the value from the |
29 // command line (--lang), second we try the value in the prefs file (passed in | 30 // command line (--lang), second we try the value in the prefs file (passed in |
30 // as |pref_locale|), finally, we fall back on the system locale. We only return | 31 // as |pref_locale|), finally, we fall back on the system locale. We only return |
31 // a value if there's a corresponding resource DLL for the locale. Otherwise, | 32 // a value if there's a corresponding resource DLL for the locale. Otherwise, |
32 // we fall back to en-us. | 33 // we fall back to en-us. |
33 std::string GetApplicationLocale(const std::string& pref_locale); | 34 UI_API std::string GetApplicationLocale(const std::string& pref_locale); |
34 | 35 |
35 // Given a locale code, return true if the OS is capable of supporting it. | 36 // Given a locale code, return true if the OS is capable of supporting it. |
36 // For instance, Oriya is not well supported on Windows XP and we return | 37 // For instance, Oriya is not well supported on Windows XP and we return |
37 // false for "or". | 38 // false for "or". |
38 bool IsLocaleSupportedByOS(const std::string& locale); | 39 bool IsLocaleSupportedByOS(const std::string& locale); |
39 | 40 |
40 // This method returns the display name of the locale code in |display_locale|. | 41 // This method returns the display name of the locale code in |display_locale|. |
41 | 42 |
42 // For example, for |locale| = "fr" and |display_locale| = "en", | 43 // For example, for |locale| = "fr" and |display_locale| = "en", |
43 // it returns "French". To get the display name of | 44 // it returns "French". To get the display name of |
44 // |locale| in the UI language of Chrome, |display_locale| can be | 45 // |locale| in the UI language of Chrome, |display_locale| can be |
45 // set to the return value of g_browser_process->GetApplicationLocale() | 46 // set to the return value of g_browser_process->GetApplicationLocale() |
46 // in the UI thread. | 47 // in the UI thread. |
47 // If |is_for_ui| is true, U+200F is appended so that it can be | 48 // If |is_for_ui| is true, U+200F is appended so that it can be |
48 // rendered properly in a RTL Chrome. | 49 // rendered properly in a RTL Chrome. |
49 string16 GetDisplayNameForLocale(const std::string& locale, | 50 UI_API string16 GetDisplayNameForLocale(const std::string& locale, |
50 const std::string& display_locale, | 51 const std::string& display_locale, |
51 bool is_for_ui); | 52 bool is_for_ui); |
52 | 53 |
53 // Converts all - into _, to be consistent with ICU and file system names. | 54 // Converts all - into _, to be consistent with ICU and file system names. |
54 std::string NormalizeLocale(const std::string& locale); | 55 UI_API std::string NormalizeLocale(const std::string& locale); |
55 | 56 |
56 // Produce a vector of parent locales for given locale. | 57 // Produce a vector of parent locales for given locale. |
57 // It includes the current locale in the result. | 58 // It includes the current locale in the result. |
58 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. | 59 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. |
59 void GetParentLocales(const std::string& current_locale, | 60 UI_API void GetParentLocales(const std::string& current_locale, |
60 std::vector<std::string>* parent_locales); | 61 std::vector<std::string>* parent_locales); |
61 | 62 |
62 // Checks if a string is plausibly a syntactically-valid locale string, | 63 // Checks if a string is plausibly a syntactically-valid locale string, |
63 // for cases where we want the valid input to be a locale string such as | 64 // for cases where we want the valid input to be a locale string such as |
64 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or | 65 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or |
65 // 'de_DE@collation=phonebook', but we don't want to limit it to | 66 // 'de_DE@collation=phonebook', but we don't want to limit it to |
66 // locales that Chrome actually knows about, so 'xx-YY' should be | 67 // locales that Chrome actually knows about, so 'xx-YY' should be |
67 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. | 68 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. |
68 // Case-insensitive. Based on BCP 47, see: | 69 // Case-insensitive. Based on BCP 47, see: |
69 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers | 70 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers |
70 bool IsValidLocaleSyntax(const std::string& locale); | 71 UI_API bool IsValidLocaleSyntax(const std::string& locale); |
71 | 72 |
72 // | 73 // |
73 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. | 74 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. |
74 // | 75 // |
75 | 76 |
76 // Pulls resource string from the string bundle and returns it. | 77 // Pulls resource string from the string bundle and returns it. |
77 std::string GetStringUTF8(int message_id); | 78 UI_API std::string GetStringUTF8(int message_id); |
78 string16 GetStringUTF16(int message_id); | 79 UI_API string16 GetStringUTF16(int message_id); |
79 | 80 |
80 // Get a resource string and replace $1-$2-$3 with |a| and |b| | 81 // Get a resource string and replace $1-$2-$3 with |a| and |b| |
81 // respectively. Additionally, $$ is replaced by $. | 82 // respectively. Additionally, $$ is replaced by $. |
82 string16 GetStringFUTF16(int message_id, | 83 UI_API string16 GetStringFUTF16(int message_id, |
83 const string16& a); | 84 const string16& a); |
84 string16 GetStringFUTF16(int message_id, | 85 UI_API string16 GetStringFUTF16(int message_id, |
85 const string16& a, | 86 const string16& a, |
86 const string16& b); | 87 const string16& b); |
87 string16 GetStringFUTF16(int message_id, | 88 UI_API string16 GetStringFUTF16(int message_id, |
88 const string16& a, | 89 const string16& a, |
89 const string16& b, | 90 const string16& b, |
90 const string16& c); | 91 const string16& c); |
91 string16 GetStringFUTF16(int message_id, | 92 UI_API string16 GetStringFUTF16(int message_id, |
92 const string16& a, | 93 const string16& a, |
93 const string16& b, | 94 const string16& b, |
94 const string16& c, | 95 const string16& c, |
95 const string16& d); | 96 const string16& d); |
96 string16 GetStringFUTF16(int message_id, | 97 UI_API string16 GetStringFUTF16(int message_id, |
97 const string16& a, | 98 const string16& a, |
98 const string16& b, | 99 const string16& b, |
99 const string16& c, | 100 const string16& c, |
100 const string16& d, | 101 const string16& d, |
101 const string16& e); | 102 const string16& e); |
102 std::string GetStringFUTF8(int message_id, | 103 UI_API std::string GetStringFUTF8(int message_id, |
103 const string16& a); | 104 const string16& a); |
104 std::string GetStringFUTF8(int message_id, | 105 UI_API std::string GetStringFUTF8(int message_id, |
105 const string16& a, | 106 const string16& a, |
106 const string16& b); | 107 const string16& b); |
107 std::string GetStringFUTF8(int message_id, | 108 UI_API std::string GetStringFUTF8(int message_id, |
108 const string16& a, | 109 const string16& a, |
109 const string16& b, | 110 const string16& b, |
110 const string16& c); | 111 const string16& c); |
111 std::string GetStringFUTF8(int message_id, | 112 UI_API std::string GetStringFUTF8(int message_id, |
112 const string16& a, | 113 const string16& a, |
113 const string16& b, | 114 const string16& b, |
114 const string16& c, | 115 const string16& c, |
115 const string16& d); | 116 const string16& d); |
116 | 117 |
117 // Variants that return the offset(s) of the replaced parameters. The | 118 // Variants that return the offset(s) of the replaced parameters. The |
118 // vector based version returns offsets ordered by parameter. For example if | 119 // vector based version returns offsets ordered by parameter. For example if |
119 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the | 120 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the |
120 // offset of b regardless of where the parameters end up in the string. | 121 // offset of b regardless of where the parameters end up in the string. |
121 string16 GetStringFUTF16(int message_id, | 122 UI_API string16 GetStringFUTF16(int message_id, |
122 const string16& a, | 123 const string16& a, |
123 size_t* offset); | 124 size_t* offset); |
124 string16 GetStringFUTF16(int message_id, | 125 UI_API string16 GetStringFUTF16(int message_id, |
125 const string16& a, | 126 const string16& a, |
126 const string16& b, | 127 const string16& b, |
127 std::vector<size_t>* offsets); | 128 std::vector<size_t>* offsets); |
128 | 129 |
129 // Convenience functions to get a string with a single number as a parameter. | 130 // Convenience functions to get a string with a single number as a parameter. |
130 string16 GetStringFUTF16Int(int message_id, int a); | 131 UI_API string16 GetStringFUTF16Int(int message_id, int a); |
131 string16 GetStringFUTF16Int(int message_id, int64 a); | 132 string16 GetStringFUTF16Int(int message_id, int64 a); |
132 | 133 |
133 // Truncates the string to length characters. This breaks the string at | 134 // Truncates the string to length characters. This breaks the string at |
134 // the first word break before length, adding the horizontal ellipsis | 135 // the first word break before length, adding the horizontal ellipsis |
135 // character (unicode character 0x2026) to render ... | 136 // character (unicode character 0x2026) to render ... |
136 // The supplied string is returned if the string has length characters or | 137 // The supplied string is returned if the string has length characters or |
137 // less. | 138 // less. |
138 string16 TruncateString(const string16& string, size_t length); | 139 UI_API string16 TruncateString(const string16& string, size_t length); |
139 | 140 |
140 // In place sorting of string16 strings using collation rules for |locale|. | 141 // In place sorting of string16 strings using collation rules for |locale|. |
141 void SortStrings16(const std::string& locale, | 142 UI_API void SortStrings16(const std::string& locale, |
142 std::vector<string16>* strings); | 143 std::vector<string16>* strings); |
143 | 144 |
144 // Returns a vector of available locale codes. E.g., a vector containing | 145 // Returns a vector of available locale codes. E.g., a vector containing |
145 // en-US, es, fr, fi, pt-PT, pt-BR, etc. | 146 // en-US, es, fr, fi, pt-PT, pt-BR, etc. |
146 const std::vector<std::string>& GetAvailableLocales(); | 147 UI_API const std::vector<std::string>& GetAvailableLocales(); |
147 | 148 |
148 // Returns a vector of locale codes usable for accept-languages. | 149 // Returns a vector of locale codes usable for accept-languages. |
149 void GetAcceptLanguagesForLocale(const std::string& display_locale, | 150 UI_API void GetAcceptLanguagesForLocale( |
150 std::vector<std::string>* locale_codes); | 151 const std::string& display_locale, |
| 152 std::vector<std::string>* locale_codes); |
151 | 153 |
152 | 154 |
153 } // namespace l10n_util | 155 } // namespace l10n_util |
154 | 156 |
155 #endif // UI_BASE_L10N_L10N_UTIL_H_ | 157 #endif // UI_BASE_L10N_L10N_UTIL_H_ |
OLD | NEW |