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