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