| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "ui/base/ui_export.h" | 16 #include "ui/base/ui_base_export.h" |
| 17 | 17 |
| 18 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
| 19 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 20 #endif // OS_MACOSX | 20 #endif // OS_MACOSX |
| 21 | 21 |
| 22 namespace l10n_util { | 22 namespace l10n_util { |
| 23 |
| 23 // This method translates a generic locale name to one of the locally defined | 24 // This method translates a generic locale name to one of the locally defined |
| 24 // ones. This method returns true if it succeeds. | 25 // ones. This method returns true if it succeeds. |
| 25 UI_EXPORT bool CheckAndResolveLocale(const std::string& locale, | 26 UI_BASE_EXPORT bool CheckAndResolveLocale(const std::string& locale, |
| 26 std::string* resolved_locale); | 27 std::string* resolved_locale); |
| 27 | 28 |
| 28 // This method is responsible for determining the locale as defined below. In | 29 // This method is responsible for determining the locale as defined below. In |
| 29 // nearly all cases you shouldn't call this, rather use GetApplicationLocale | 30 // nearly all cases you shouldn't call this, rather use GetApplicationLocale |
| 30 // defined on browser_process. | 31 // defined on browser_process. |
| 31 // | 32 // |
| 32 // Returns the locale used by the Application. First we use the value from the | 33 // Returns the locale used by the Application. First we use the value from the |
| 33 // command line (--lang), second we try the value in the prefs file (passed in | 34 // command line (--lang), second we try the value in the prefs file (passed in |
| 34 // as |pref_locale|), finally, we fall back on the system locale. We only return | 35 // as |pref_locale|), finally, we fall back on the system locale. We only return |
| 35 // a value if there's a corresponding resource DLL for the locale. Otherwise, | 36 // a value if there's a corresponding resource DLL for the locale. Otherwise, |
| 36 // we fall back to en-us. | 37 // we fall back to en-us. |
| 37 UI_EXPORT std::string GetApplicationLocale(const std::string& pref_locale); | 38 UI_BASE_EXPORT std::string GetApplicationLocale(const std::string& pref_locale); |
| 38 | 39 |
| 39 // Returns true if a display name for |locale| is available in the locale | 40 // Returns true if a display name for |locale| is available in the locale |
| 40 // |display_locale|. | 41 // |display_locale|. |
| 41 UI_EXPORT bool IsLocaleNameTranslated(const char* locale, | 42 UI_BASE_EXPORT bool IsLocaleNameTranslated(const char* locale, |
| 42 const std::string& display_locale); | 43 const std::string& display_locale); |
| 43 | 44 |
| 44 // Given a locale code, return true if the OS is capable of supporting it. | 45 // Given a locale code, return true if the OS is capable of supporting it. |
| 45 // For instance, Oriya is not well supported on Windows XP and we return | 46 // For instance, Oriya is not well supported on Windows XP and we return |
| 46 // false for "or". | 47 // false for "or". |
| 47 bool IsLocaleSupportedByOS(const std::string& locale); | 48 bool IsLocaleSupportedByOS(const std::string& locale); |
| 48 | 49 |
| 49 // This method returns the display name of the locale code in |display_locale|. | 50 // This method returns the display name of the locale code in |display_locale|. |
| 50 | 51 |
| 51 // For example, for |locale| = "fr" and |display_locale| = "en", | 52 // For example, for |locale| = "fr" and |display_locale| = "en", |
| 52 // it returns "French". To get the display name of | 53 // it returns "French". To get the display name of |
| 53 // |locale| in the UI language of Chrome, |display_locale| can be | 54 // |locale| in the UI language of Chrome, |display_locale| can be |
| 54 // set to the return value of g_browser_process->GetApplicationLocale() | 55 // set to the return value of g_browser_process->GetApplicationLocale() |
| 55 // in the UI thread. | 56 // in the UI thread. |
| 56 // If |is_for_ui| is true, U+200F is appended so that it can be | 57 // If |is_for_ui| is true, U+200F is appended so that it can be |
| 57 // rendered properly in a RTL Chrome. | 58 // rendered properly in a RTL Chrome. |
| 58 UI_EXPORT base::string16 GetDisplayNameForLocale( | 59 UI_BASE_EXPORT base::string16 GetDisplayNameForLocale( |
| 59 const std::string& locale, | 60 const std::string& locale, |
| 60 const std::string& display_locale, | 61 const std::string& display_locale, |
| 61 bool is_for_ui); | 62 bool is_for_ui); |
| 62 | 63 |
| 63 // Returns the display name of the |country_code| in |display_locale|. | 64 // Returns the display name of the |country_code| in |display_locale|. |
| 64 UI_EXPORT base::string16 GetDisplayNameForCountry( | 65 UI_BASE_EXPORT base::string16 GetDisplayNameForCountry( |
| 65 const std::string& country_code, | 66 const std::string& country_code, |
| 66 const std::string& display_locale); | 67 const std::string& display_locale); |
| 67 | 68 |
| 68 // Converts all - into _, to be consistent with ICU and file system names. | 69 // Converts all - into _, to be consistent with ICU and file system names. |
| 69 UI_EXPORT std::string NormalizeLocale(const std::string& locale); | 70 UI_BASE_EXPORT std::string NormalizeLocale(const std::string& locale); |
| 70 | 71 |
| 71 // Produce a vector of parent locales for given locale. | 72 // Produce a vector of parent locales for given locale. |
| 72 // It includes the current locale in the result. | 73 // It includes the current locale in the result. |
| 73 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. | 74 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. |
| 74 UI_EXPORT void GetParentLocales(const std::string& current_locale, | 75 UI_BASE_EXPORT void GetParentLocales(const std::string& current_locale, |
| 75 std::vector<std::string>* parent_locales); | 76 std::vector<std::string>* parent_locales); |
| 76 | 77 |
| 77 // Checks if a string is plausibly a syntactically-valid locale string, | 78 // Checks if a string is plausibly a syntactically-valid locale string, |
| 78 // for cases where we want the valid input to be a locale string such as | 79 // for cases where we want the valid input to be a locale string such as |
| 79 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or | 80 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or |
| 80 // 'de_DE@collation=phonebook', but we don't want to limit it to | 81 // 'de_DE@collation=phonebook', but we don't want to limit it to |
| 81 // locales that Chrome actually knows about, so 'xx-YY' should be | 82 // locales that Chrome actually knows about, so 'xx-YY' should be |
| 82 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. | 83 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. |
| 83 // Case-insensitive. Based on BCP 47, see: | 84 // Case-insensitive. Based on BCP 47, see: |
| 84 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers | 85 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers |
| 85 UI_EXPORT bool IsValidLocaleSyntax(const std::string& locale); | 86 UI_BASE_EXPORT bool IsValidLocaleSyntax(const std::string& locale); |
| 86 | 87 |
| 87 // | 88 // |
| 88 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. | 89 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. |
| 89 // | 90 // |
| 90 | 91 |
| 91 // Pulls resource string from the string bundle and returns it. | 92 // Pulls resource string from the string bundle and returns it. |
| 92 UI_EXPORT std::string GetStringUTF8(int message_id); | 93 UI_BASE_EXPORT std::string GetStringUTF8(int message_id); |
| 93 UI_EXPORT base::string16 GetStringUTF16(int message_id); | 94 UI_BASE_EXPORT base::string16 GetStringUTF16(int message_id); |
| 94 | 95 |
| 95 // Get a resource string and replace $i with replacements[i] for all | 96 // Get a resource string and replace $i with replacements[i] for all |
| 96 // i < replacements.size(). Additionally, $$ is replaced by $. | 97 // i < replacements.size(). Additionally, $$ is replaced by $. |
| 97 // If non-NULL |offsets| will be replaced with the start points of the replaced | 98 // If non-NULL |offsets| will be replaced with the start points of the replaced |
| 98 // strings. | 99 // strings. |
| 99 UI_EXPORT base::string16 GetStringFUTF16( | 100 UI_BASE_EXPORT base::string16 GetStringFUTF16( |
| 100 int message_id, | 101 int message_id, |
| 101 const std::vector<base::string16>& replacements, | 102 const std::vector<base::string16>& replacements, |
| 102 std::vector<size_t>* offsets); | 103 std::vector<size_t>* offsets); |
| 103 | 104 |
| 104 // Convenience wrappers for the above. | 105 // Convenience wrappers for the above. |
| 105 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 106 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 106 const base::string16& a); | 107 const base::string16& a); |
| 107 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 108 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 108 const base::string16& a, | 109 const base::string16& a, |
| 109 const base::string16& b); | 110 const base::string16& b); |
| 110 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 111 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 111 const base::string16& a, | 112 const base::string16& a, |
| 112 const base::string16& b, | 113 const base::string16& b, |
| 113 const base::string16& c); | 114 const base::string16& c); |
| 114 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 115 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 115 const base::string16& a, | 116 const base::string16& a, |
| 116 const base::string16& b, | 117 const base::string16& b, |
| 117 const base::string16& c, | 118 const base::string16& c, |
| 118 const base::string16& d); | 119 const base::string16& d); |
| 119 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 120 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 120 const base::string16& a, | 121 const base::string16& a, |
| 121 const base::string16& b, | 122 const base::string16& b, |
| 122 const base::string16& c, | 123 const base::string16& c, |
| 123 const base::string16& d, | 124 const base::string16& d, |
| 124 const base::string16& e); | 125 const base::string16& e); |
| 125 UI_EXPORT std::string GetStringFUTF8(int message_id, | 126 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id, |
| 126 const base::string16& a); | 127 const base::string16& a); |
| 127 UI_EXPORT std::string GetStringFUTF8(int message_id, | 128 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id, |
| 128 const base::string16& a, | 129 const base::string16& a, |
| 129 const base::string16& b); | 130 const base::string16& b); |
| 130 UI_EXPORT std::string GetStringFUTF8(int message_id, | 131 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id, |
| 131 const base::string16& a, | 132 const base::string16& a, |
| 132 const base::string16& b, | 133 const base::string16& b, |
| 133 const base::string16& c); | 134 const base::string16& c); |
| 134 UI_EXPORT std::string GetStringFUTF8(int message_id, | 135 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id, |
| 135 const base::string16& a, | 136 const base::string16& a, |
| 136 const base::string16& b, | 137 const base::string16& b, |
| 137 const base::string16& c, | 138 const base::string16& c, |
| 138 const base::string16& d); | 139 const base::string16& d); |
| 139 | 140 |
| 140 // Variants that return the offset(s) of the replaced parameters. The | 141 // Variants that return the offset(s) of the replaced parameters. The |
| 141 // vector based version returns offsets ordered by parameter. For example if | 142 // vector based version returns offsets ordered by parameter. For example if |
| 142 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the | 143 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the |
| 143 // offset of b regardless of where the parameters end up in the string. | 144 // offset of b regardless of where the parameters end up in the string. |
| 144 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 145 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 145 const base::string16& a, | 146 const base::string16& a, |
| 146 size_t* offset); | 147 size_t* offset); |
| 147 UI_EXPORT base::string16 GetStringFUTF16(int message_id, | 148 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id, |
| 148 const base::string16& a, | 149 const base::string16& a, |
| 149 const base::string16& b, | 150 const base::string16& b, |
| 150 std::vector<size_t>* offsets); | 151 std::vector<size_t>* offsets); |
| 151 | 152 |
| 152 // Convenience functions to get a string with a single number as a parameter. | 153 // Convenience functions to get a string with a single number as a parameter. |
| 153 UI_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a); | 154 UI_BASE_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a); |
| 154 base::string16 GetStringFUTF16Int(int message_id, int64 a); | 155 base::string16 GetStringFUTF16Int(int message_id, int64 a); |
| 155 | 156 |
| 156 // Get a resource string using |number| to decide which of |message_ids| should | 157 // Get a resource string using |number| to decide which of |message_ids| should |
| 157 // be used. |message_ids| must be size 6 and in order: default, singular, zero, | 158 // be used. |message_ids| must be size 6 and in order: default, singular, zero, |
| 158 // two, few, many. | 159 // two, few, many. |
| 159 UI_EXPORT base::string16 GetPluralStringFUTF16( | 160 UI_BASE_EXPORT base::string16 GetPluralStringFUTF16( |
| 160 const std::vector<int>& message_ids, | 161 const std::vector<int>& message_ids, |
| 161 int number); | 162 int number); |
| 162 UI_EXPORT std::string GetPluralStringFUTF8(const std::vector<int>& message_ids, | 163 UI_BASE_EXPORT std::string GetPluralStringFUTF8( |
| 163 int number); | 164 const std::vector<int>& message_ids, |
| 165 int number); |
| 164 | 166 |
| 165 // In place sorting of base::string16 strings using collation rules for |locale|
. | 167 // In place sorting of base::string16 strings using collation rules for |
| 166 UI_EXPORT void SortStrings16(const std::string& locale, | 168 // |locale|. |
| 167 std::vector<base::string16>* strings); | 169 UI_BASE_EXPORT void SortStrings16(const std::string& locale, |
| 170 std::vector<base::string16>* strings); |
| 168 | 171 |
| 169 // Returns a vector of available locale codes. E.g., a vector containing | 172 // Returns a vector of available locale codes. E.g., a vector containing |
| 170 // en-US, es, fr, fi, pt-PT, pt-BR, etc. | 173 // en-US, es, fr, fi, pt-PT, pt-BR, etc. |
| 171 UI_EXPORT const std::vector<std::string>& GetAvailableLocales(); | 174 UI_BASE_EXPORT const std::vector<std::string>& GetAvailableLocales(); |
| 172 | 175 |
| 173 // Returns a vector of locale codes usable for accept-languages. | 176 // Returns a vector of locale codes usable for accept-languages. |
| 174 UI_EXPORT void GetAcceptLanguagesForLocale( | 177 UI_BASE_EXPORT void GetAcceptLanguagesForLocale( |
| 175 const std::string& display_locale, | 178 const std::string& display_locale, |
| 176 std::vector<std::string>* locale_codes); | 179 std::vector<std::string>* locale_codes); |
| 177 | 180 |
| 178 // Returns the preferred size of the contents view of a window based on | 181 // Returns the preferred size of the contents view of a window based on |
| 179 // designer given constraints which might dependent on the language used. | 182 // designer given constraints which might dependent on the language used. |
| 180 UI_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id); | 183 UI_BASE_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id); |
| 181 | 184 |
| 182 } // namespace l10n_util | 185 } // namespace l10n_util |
| 183 | 186 |
| 184 #endif // UI_BASE_L10N_L10N_UTIL_H_ | 187 #endif // UI_BASE_L10N_L10N_UTIL_H_ |
| OLD | NEW |