Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / l10n / l10n_util.h
blob9317556260f636cc2d86a4f599e2e94ccc38ea7f
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.
5 // This file contains utility functions for dealing with localized
6 // content.
8 #ifndef UI_BASE_L10N_L10N_UTIL_H_
9 #define UI_BASE_L10N_L10N_UTIL_H_
11 #include <string>
12 #include <vector>
14 #include "base/strings/string16.h"
15 #include "ui/base/ui_base_export.h"
17 #if defined(OS_MACOSX)
18 #include "ui/base/l10n/l10n_util_mac.h"
19 #endif // OS_MACOSX
21 namespace l10n_util {
23 // Takes normalized locale as |locale|. Returns language part (before '-').
24 UI_BASE_EXPORT std::string GetLanguage(const std::string& locale);
26 // This method translates a generic locale name to one of the locally defined
27 // ones. This method returns true if it succeeds.
28 UI_BASE_EXPORT bool CheckAndResolveLocale(const std::string& locale,
29 std::string* resolved_locale);
31 // This method is responsible for determining the locale as defined below. In
32 // nearly all cases you shouldn't call this, rather use GetApplicationLocale
33 // defined on browser_process.
35 // Returns the locale used by the Application. First we use the value from the
36 // command line (--lang), second we try the value in the prefs file (passed in
37 // as |pref_locale|), finally, we fall back on the system locale. We only return
38 // a value if there's a corresponding resource DLL for the locale. Otherwise,
39 // we fall back to en-us. |set_icu_locale| determines whether the resulting
40 // locale is set as the default ICU locale before returning it.
41 UI_BASE_EXPORT std::string GetApplicationLocale(const std::string& pref_locale,
42 bool set_icu_locale);
44 // Convenience version of GetApplicationLocale() that sets the resulting locale
45 // as the default ICU locale before returning it.
46 UI_BASE_EXPORT std::string GetApplicationLocale(const std::string& pref_locale);
48 // Returns true if a display name for |locale| is available in the locale
49 // |display_locale|.
50 UI_BASE_EXPORT bool IsLocaleNameTranslated(const char* locale,
51 const std::string& display_locale);
53 // Given a locale code, return true if the OS is capable of supporting it.
54 // For instance, Oriya is not well supported on Windows XP and we return
55 // false for "or".
56 bool IsLocaleSupportedByOS(const std::string& locale);
58 // This method returns the display name of the locale code in |display_locale|.
60 // For example, for |locale| = "fr" and |display_locale| = "en",
61 // it returns "French". To get the display name of
62 // |locale| in the UI language of Chrome, |display_locale| can be
63 // set to the return value of g_browser_process->GetApplicationLocale()
64 // in the UI thread.
65 // If |is_for_ui| is true, U+200F is appended so that it can be
66 // rendered properly in a RTL Chrome.
67 UI_BASE_EXPORT base::string16 GetDisplayNameForLocale(
68 const std::string& locale,
69 const std::string& display_locale,
70 bool is_for_ui);
72 // Returns the display name of the |country_code| in |display_locale|.
73 UI_BASE_EXPORT base::string16 GetDisplayNameForCountry(
74 const std::string& country_code,
75 const std::string& display_locale);
77 // Converts all - into _, to be consistent with ICU and file system names.
78 UI_BASE_EXPORT std::string NormalizeLocale(const std::string& locale);
80 // Produce a vector of parent locales for given locale.
81 // It includes the current locale in the result.
82 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr.
83 UI_BASE_EXPORT void GetParentLocales(const std::string& current_locale,
84 std::vector<std::string>* parent_locales);
86 // Checks if a string is plausibly a syntactically-valid locale string,
87 // for cases where we want the valid input to be a locale string such as
88 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or
89 // 'de_DE@collation=phonebook', but we don't want to limit it to
90 // locales that Chrome actually knows about, so 'xx-YY' should be
91 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not.
92 // Case-insensitive. Based on BCP 47, see:
93 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
94 UI_BASE_EXPORT bool IsValidLocaleSyntax(const std::string& locale);
97 // Mac Note: See l10n_util_mac.h for some NSString versions and other support.
100 // Pulls resource string from the string bundle and returns it.
101 UI_BASE_EXPORT std::string GetStringUTF8(int message_id);
102 UI_BASE_EXPORT base::string16 GetStringUTF16(int message_id);
104 // Get a resource string and replace $i with replacements[i] for all
105 // i < replacements.size(). Additionally, $$ is replaced by $.
106 // If non-NULL |offsets| will be replaced with the start points of the replaced
107 // strings.
108 UI_BASE_EXPORT base::string16 GetStringFUTF16(
109 int message_id,
110 const std::vector<base::string16>& replacements,
111 std::vector<size_t>* offsets);
113 // Convenience wrappers for the above.
114 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
115 const base::string16& a);
116 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
117 const base::string16& a,
118 const base::string16& b);
119 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
120 const base::string16& a,
121 const base::string16& b,
122 const base::string16& c);
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 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 const base::string16& e);
134 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
135 const base::string16& a);
136 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
137 const base::string16& a,
138 const base::string16& b);
139 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
140 const base::string16& a,
141 const base::string16& b,
142 const base::string16& c);
143 UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
144 const base::string16& a,
145 const base::string16& b,
146 const base::string16& c,
147 const base::string16& d);
149 // Variants that return the offset(s) of the replaced parameters. The
150 // vector based version returns offsets ordered by parameter. For example if
151 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the
152 // offset of b regardless of where the parameters end up in the string.
153 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
154 const base::string16& a,
155 size_t* offset);
156 UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
157 const base::string16& a,
158 const base::string16& b,
159 std::vector<size_t>* offsets);
161 // Convenience functions to get a string with a single number as a parameter.
162 UI_BASE_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a);
163 base::string16 GetStringFUTF16Int(int message_id, int64 a);
165 // Get a resource string using |number| with a locale-specific plural rule.
166 // |message_id| points to a message in the ICU syntax.
167 // See http://userguide.icu-project.org/formatparse/messages and
168 // go/plurals (Google internal).
169 UI_BASE_EXPORT base::string16 GetPluralStringFUTF16(int message_id, int number);
170 UI_BASE_EXPORT std::string GetPluralStringFUTF8(int message_id, int number);
172 // Get a string when you only care about 'single vs multiple' distinction.
173 // The message pointed to by |message_id| should be in ICU syntax
174 // (see the references above for Plural) with 'single', 'multiple', and
175 // 'other' (fallback) instead of 'male', 'female', and 'other' (fallback).
176 UI_BASE_EXPORT base::string16 GetSingleOrMultipleStringUTF16(int message_id,
177 bool is_multiple);
179 // In place sorting of base::string16 strings using collation rules for
180 // |locale|.
181 UI_BASE_EXPORT void SortStrings16(const std::string& locale,
182 std::vector<base::string16>* strings);
184 // Returns a vector of available locale codes. E.g., a vector containing
185 // en-US, es, fr, fi, pt-PT, pt-BR, etc.
186 UI_BASE_EXPORT const std::vector<std::string>& GetAvailableLocales();
188 // Returns a vector of locale codes usable for accept-languages.
189 UI_BASE_EXPORT void GetAcceptLanguagesForLocale(
190 const std::string& display_locale,
191 std::vector<std::string>* locale_codes);
193 // Returns the preferred size of the contents view of a window based on
194 // designer given constraints which might dependent on the language used.
195 UI_BASE_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id);
197 UI_BASE_EXPORT const char* const* GetAcceptLanguageListForTesting();
199 UI_BASE_EXPORT size_t GetAcceptLanguageListSizeForTesting();
201 } // namespace l10n_util
203 #endif // UI_BASE_L10N_L10N_UTIL_H_