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
21namespace l10n_util {
22
23// The same as base::i18n::GetCanonicalLocale(const char*), but takes
24// std::string as an argument.
25UI_BASE_EXPORT std::string GetCanonicalLocale(const std::string& locale);
26
27// Takes normalized locale as |locale|. Returns language part (before '-').
28UI_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.
32UI_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.
45UI_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.
50UI_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|.
54UI_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".
60bool 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.
71UI_BASE_EXPORT base::string16 GetDisplayNameForLocale(
72    const std::string& locale,
73    const std::string& display_locale,
74    bool is_for_ui);
75
76// Returns the display name of the |country_code| in |display_locale|.
77UI_BASE_EXPORT base::string16 GetDisplayNameForCountry(
78    const std::string& country_code,
79    const std::string& display_locale);
80
81// Converts all - into _, to be consistent with ICU and file system names.
82UI_BASE_EXPORT std::string NormalizeLocale(const std::string& locale);
83
84// Produce a vector of parent locales for given locale.
85// It includes the current locale in the result.
86// sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr.
87UI_BASE_EXPORT void GetParentLocales(const std::string& current_locale,
88                                     std::vector<std::string>* parent_locales);
89
90// Checks if a string is plausibly a syntactically-valid locale string,
91// for cases where we want the valid input to be a locale string such as
92// 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or
93// 'de_DE@collation=phonebook', but we don't want to limit it to
94// locales that Chrome actually knows about, so 'xx-YY' should be
95// accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not.
96// Case-insensitive. Based on BCP 47, see:
97//   http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers
98UI_BASE_EXPORT bool IsValidLocaleSyntax(const std::string& locale);
99
100//
101// Mac Note: See l10n_util_mac.h for some NSString versions and other support.
102//
103
104// Pulls resource string from the string bundle and returns it.
105UI_BASE_EXPORT std::string GetStringUTF8(int message_id);
106UI_BASE_EXPORT base::string16 GetStringUTF16(int message_id);
107
108// Get a resource string and replace $i with replacements[i] for all
109// i < replacements.size(). Additionally, $$ is replaced by $.
110// If non-NULL |offsets| will be replaced with the start points of the replaced
111// strings.
112UI_BASE_EXPORT base::string16 GetStringFUTF16(
113    int message_id,
114    const std::vector<base::string16>& replacements,
115    std::vector<size_t>* offsets);
116
117// Convenience wrappers for the above.
118UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
119                                              const base::string16& a);
120UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
121                                              const base::string16& a,
122                                              const base::string16& b);
123UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
124                                              const base::string16& a,
125                                              const base::string16& b,
126                                              const base::string16& c);
127UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
128                                              const base::string16& a,
129                                              const base::string16& b,
130                                              const base::string16& c,
131                                              const base::string16& d);
132UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
133                                              const base::string16& a,
134                                              const base::string16& b,
135                                              const base::string16& c,
136                                              const base::string16& d,
137                                              const base::string16& e);
138UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
139                                          const base::string16& a);
140UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
141                                          const base::string16& a,
142                                          const base::string16& b);
143UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
144                                          const base::string16& a,
145                                          const base::string16& b,
146                                          const base::string16& c);
147UI_BASE_EXPORT std::string GetStringFUTF8(int message_id,
148                                          const base::string16& a,
149                                          const base::string16& b,
150                                          const base::string16& c,
151                                          const base::string16& d);
152
153// Variants that return the offset(s) of the replaced parameters. The
154// vector based version returns offsets ordered by parameter. For example if
155// invoked with a and b offsets[0] gives the offset for a and offsets[1] the
156// offset of b regardless of where the parameters end up in the string.
157UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
158                                              const base::string16& a,
159                                              size_t* offset);
160UI_BASE_EXPORT base::string16 GetStringFUTF16(int message_id,
161                                              const base::string16& a,
162                                              const base::string16& b,
163                                              std::vector<size_t>* offsets);
164
165// Convenience functions to get a string with a single number as a parameter.
166UI_BASE_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a);
167base::string16 GetStringFUTF16Int(int message_id, int64 a);
168
169// Get a resource string using |number| to decide which of |message_ids| should
170// be used. |message_ids| must be size 6 and in order: default, singular, zero,
171// two, few, many.
172UI_BASE_EXPORT base::string16 GetPluralStringFUTF16(
173    const std::vector<int>& message_ids,
174    int number);
175UI_BASE_EXPORT std::string GetPluralStringFUTF8(
176    const std::vector<int>& message_ids,
177    int number);
178
179// In place sorting of base::string16 strings using collation rules for
180// |locale|.
181UI_BASE_EXPORT void SortStrings16(const std::string& locale,
182                                  std::vector<base::string16>* strings);
183
184// Returns a vector of available locale codes. E.g., a vector containing
185// en-US, es, fr, fi, pt-PT, pt-BR, etc.
186UI_BASE_EXPORT const std::vector<std::string>& GetAvailableLocales();
187
188// Returns a vector of locale codes usable for accept-languages.
189UI_BASE_EXPORT void GetAcceptLanguagesForLocale(
190    const std::string& display_locale,
191    std::vector<std::string>* locale_codes);
192
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.
195UI_BASE_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id);
196
197UI_BASE_EXPORT const char* const* GetAcceptLanguageListForTesting();
198
199UI_BASE_EXPORT size_t GetAcceptLanguageListSizeForTesting();
200
201}  // namespace l10n_util
202
203#endif  // UI_BASE_L10N_L10N_UTIL_H_
204