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