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
3 // found in the LICENSE file.
4 
5 // This file contains utility functions for dealing with localized
6 // content.
7 
8 #ifndef UI_BASE_L10N_L10N_UTIL_H_
9 #define UI_BASE_L10N_L10N_UTIL_H_
10 
11 #include <stddef.h>
12 #include <stdint.h>
13 
14 #include <string>
15 #include <vector>
16 
17 #include "base/strings/string16.h"
18 #include "build/build_config.h"
19 #include "ui/base/ui_base_export.h"
20 
21 #if defined(OS_MACOSX)
22 #include "ui/base/l10n/l10n_util_mac.h"
23 #endif  // OS_MACOSX
24 
25 namespace l10n_util {
26 
27 // Takes normalized locale as |locale|. Returns language part (before '-').
28 UI_BASE_EXPORT std::string GetLanguage(const std::string& locale);
29 
30 // This method translates a generic locale name to one of the locally defined
31 // ones. This method returns true if it succeeds.
32 UI_BASE_EXPORT bool CheckAndResolveLocale(const std::string& locale,
33                                           std::string* resolved_locale);
34 
35 // This method is responsible for determining the locale as defined below. In
36 // nearly all cases you shouldn't call this, rather use GetApplicationLocale
37 // defined on browser_process.
38 //
39 // Returns the locale used by the Application.  First we use the value from the
40 // command line (--lang), second we try the value in the prefs file (passed in
41 // as |pref_locale|), finally, we fall back on the system locale. We only return
42 // a value if there's a corresponding resource DLL for the locale.  Otherwise,
43 // we fall back to en-us. |set_icu_locale| determines whether the resulting
44 // locale is set as the default ICU locale before returning it.
45 UI_BASE_EXPORT std::string GetApplicationLocale(const std::string& pref_locale,
46                                                 bool set_icu_locale);
47 
48 // Convenience version of GetApplicationLocale() that sets the resulting locale
49 // as the default ICU locale before returning it.
50 UI_BASE_EXPORT std::string GetApplicationLocale(const std::string& pref_locale);
51 
52 // Returns true if a display name for |locale| is available in the locale
53 // |display_locale|.
54 UI_BASE_EXPORT bool IsLocaleNameTranslated(const char* locale,
55                                            const std::string& display_locale);
56 
57 // Given a locale code, return true if the OS is capable of supporting it.
58 // For instance, Oriya is not well supported on Windows XP and we return
59 // false for "or".
60 bool IsLocaleSupportedByOS(const std::string& locale);
61 
62 // This method returns the display name of the locale code in |display_locale|.
63 
64 // For example, for |locale| = "fr" and |display_locale| = "en",
65 // it returns "French". To get the display name of
66 // |locale| in the UI language of Chrome, |display_locale| can be
67 // set to the return value of g_browser_process->GetApplicationLocale()
68 // in the UI thread.
69 // If |is_for_ui| is true, U+200F is appended so that it can be
70 // rendered properly in a RTL Chrome.
71 UI_BASE_EXPORT base::string16 GetDisplayNameForLocale(
72     const std::string& locale,
73     const std::string& display_locale,
74     bool is_for_ui,
75     bool disallow_default = false);
76 
77 // Returns the display name of the |country_code| in |display_locale|.
78 UI_BASE_EXPORT base::string16 GetDisplayNameForCountry(
79     const std::string& country_code,
80     const std::string& display_locale);
81 
82 // Converts all - into _, to be consistent with ICU and file system names.
83 UI_BASE_EXPORT std::string NormalizeLocale(const std::string& locale);
84 
85 // Produce a vector of parent locales for given locale.
86 // It includes the current locale in the result.
87 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr.
88 UI_BASE_EXPORT void GetParentLocales(const std::string& current_locale,
89                                      std::vector<std::string>* parent_locales);
90 
91 // Checks if a string is plausibly a syntactically-valid locale string,
92 // for cases where we want the valid input to be a locale string such as
93 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or
94 // 'de_DE@collation=phonebook', but we don't want to limit it to
95 // locales that Chrome actually knows about, so 'xx-YY' should be
96 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not.
97 // Case-insensitive. Based on BCP 47, see:
98 //   http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
99 UI_BASE_EXPORT bool IsValidLocaleSyntax(const std::string& locale);
100 
101 //
102 // Mac Note: See l10n_util_mac.h for some NSString versions and other support.
103 //
104 
105 // Pulls resource string from the string bundle and returns it.
106 UI_BASE_EXPORT std::string GetStringUTF8(int message_id);
107 UI_BASE_EXPORT base::string16 GetStringUTF16(int message_id);
108 
109 // Get a resource string and replace $i with replacements[i] for all
110 // i < replacements.size(). Additionally, $$ is replaced by $.
111 // If non-NULL |offsets| will be replaced with the start points of the replaced
112 // strings.
113 UI_BASE_EXPORT base::string16 GetStringFUTF16(
114     int message_id,
115     const std::vector<base::string16>& replacements,
116     std::vector<size_t>* offsets);
117 
118 // Convenience wrappers for the above.
119 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
120                                               const base::string16& a);
121 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
122                                               const base::string16& a,
123                                               const base::string16& b);
124 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
125                                               const base::string16& a,
126                                               const base::string16& b,
127                                               const base::string16& c);
128 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
129                                               const base::string16& a,
130                                               const base::string16& b,
131                                               const base::string16& c,
132                                               const base::string16& d);
133 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
134                                               const base::string16& a,
135                                               const base::string16& b,
136                                               const base::string16& c,
137                                               const base::string16& d,
138                                               const base::string16& e);
139 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
140                                           const base::string16& a);
141 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
142                                           const base::string16& a,
143                                           const base::string16& b);
144 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
145                                           const base::string16& a,
146                                           const base::string16& b,
147                                           const base::string16& c);
148 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
149                                           const base::string16& a,
150                                           const base::string16& b,
151                                           const base::string16& c,
152                                           const base::string16& d);
153 
154 // Variants that return the offset(s) of the replaced parameters. The
155 // vector based version returns offsets ordered by parameter. For example if
156 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the
157 // offset of b regardless of where the parameters end up in the string.
158 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
159                                               const base::string16& a,
160                                               size_t* offset);
161 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
162                                               const base::string16& a,
163                                               const base::string16& b,
164                                               std::vector<size_t>* offsets);
165 
166 // Convenience functions to get a string with a single integer as a parameter.
167 // The result will use non-ASCII(native) digits if required by a locale
168 // convention (e.g. Persian, Bengali).
169 // If a message requires plural formatting (e.g. "3 tabs open"), use
170 // GetPluralStringF*, instead. To format a double, integer or percentage alone
171 // without any surrounding text (e.g. "3.57", "123", "45%"), use
172 // base::Format{Double,Number,Percent}. With more than two numbers or
173 // number + surrounding text, use base::i18n::MessageFormatter.
174 // // Note that native digits have to be used in UI in general.
175 // base::{Int*,Double}ToString convert a number to a string with
176 // ASCII digits in non-UI strings.
177 UI_BASE_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a);
178 base::string16 GetStringFUTF16Int(int message_id, int64_t a);
179 
180 // Convenience functions to format a string with a single number that requires
181 // plural formatting. Note that a simple 2-way rule (singular vs plural)
182 // breaks down for a number of languages. Instead of two separate messages
183 // for singular and plural, use this method with one message in ICU syntax.
184 // See http://userguide.icu-project.org/formatparse/messages and
185 // go/plurals (Google internal) for more details and examples.
186 //
187 // For complex messages with input parameters of multiple types (int,
188 // double, time, string; e.g. "At 3:45 on Feb 3, 2016, 5 files are downloaded
189 // at 3 MB/s."), use base::i18n::MessageFormatter.
190 // message_format_unittests.cc also has more examples of plural formatting.
191 UI_BASE_EXPORT base::string16 GetPluralStringFUTF16(int message_id, int number);
192 UI_BASE_EXPORT std::string GetPluralStringFUTF8(int message_id, int number);
193 
194 // Get a string when you only care about 'single vs multiple' distinction.
195 // The message pointed to by |message_id| should be in ICU syntax
196 // (see the references above for Plural) with 'single', 'multiple', and
197 // 'other' (fallback) instead of 'male', 'female', and 'other' (fallback).
198 UI_BASE_EXPORT base::string16 GetSingleOrMultipleStringUTF16(int message_id,
199                                                              bool is_multiple);
200 
201 // In place sorting of base::string16 strings using collation rules for
202 // |locale|.
203 UI_BASE_EXPORT void SortStrings16(const std::string& locale,
204                                   std::vector<base::string16>* strings);
205 
206 // Returns a vector of available locale codes. E.g., a vector containing
207 // en-US, es, fr, fi, pt-PT, pt-BR, etc.
208 UI_BASE_EXPORT const std::vector<std::string>& GetAvailableLocales();
209 
210 // Returns a vector of locale codes usable for accept-languages.
211 UI_BASE_EXPORT void GetAcceptLanguagesForLocale(
212     const std::string& display_locale,
213     std::vector<std::string>* locale_codes);
214 
215 // Returns true if |locale| is in a predefined AcceptLanguageList and
216 // a display name for the |locale| is available in the locale |display_locale|.
217 UI_BASE_EXPORT bool IsLanguageAccepted(const std::string& display_locale,
218                                        const std::string& locale);
219 
220 // Returns the preferred size of the contents view of a window based on
221 // designer given constraints which might dependent on the language used.
222 UI_BASE_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id);
223 
224 UI_BASE_EXPORT const char* const* GetAcceptLanguageListForTesting();
225 
226 UI_BASE_EXPORT size_t GetAcceptLanguageListSizeForTesting();
227 
228 }  // namespace l10n_util
229 
230 #endif  // UI_BASE_L10N_L10N_UTIL_H_
231