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