1 // Copyright (c) 2010 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 APP_L10N_UTIL_H_
9 #define APP_L10N_UTIL_H_
17 #include "build/build_config.h"
19 #include "base/basictypes.h"
20 #include "base/scoped_ptr.h"
21 #include "base/string16.h"
22 #include "base/string_util.h"
24 #if defined(OS_MACOSX)
25 #include "app/l10n_util_mac.h"
30 // This method is responsible for determining the locale as defined below. In
31 // nearly all cases you shouldn't call this, rather use GetApplicationLocale
32 // defined on browser_process.
34 // Returns the locale used by the Application. First we use the value from the
35 // command line (--lang), second we try the value in the prefs file (passed in
36 // as |pref_locale|), finally, we fall back on the system locale. We only return
37 // a value if there's a corresponding resource DLL for the locale. Otherwise,
38 // we fall back to en-us.
39 std::string
GetApplicationLocale(const std::string
& pref_locale
);
41 // Given a locale code, return true if the OS is capable of supporting it.
42 // For instance, Oriya is not well supported on Windows XP and we return
44 bool IsLocaleSupportedByOS(const std::string
& locale
);
46 // This method returns the display name of the locale code in |display_locale|.
48 // For example, for |locale| = "fr" and |display_locale| = "en",
49 // it returns "French". To get the display name of
50 // |locale| in the UI language of Chrome, |display_locale| can be
51 // set to the return value of g_browser_process->GetApplicationLocale()
53 // If |is_for_ui| is true, U+200F is appended so that it can be
54 // rendered properly in a RTL Chrome.
55 string16
GetDisplayNameForLocale(const std::string
& locale
,
56 const std::string
& display_locale
,
60 // Mac Note: See l10n_util_mac.h for some NSString versions and other support.
63 // Pulls resource string from the string bundle and returns it.
64 std::wstring
GetString(int message_id
);
65 std::string
GetStringUTF8(int message_id
);
66 string16
GetStringUTF16(int message_id
);
68 // Get a resource string and replace $1-$2-$3 with |a| and |b|
69 // respectively. Additionally, $$ is replaced by $.
70 string16
GetStringFUTF16(int message_id
,
72 string16
GetStringFUTF16(int message_id
,
75 string16
GetStringFUTF16(int message_id
,
79 string16
GetStringFUTF16(int message_id
,
84 #if defined(WCHAR_T_IS_UTF16)
85 inline std::wstring
GetStringF(int message_id
,
86 const std::wstring
& a
) {
87 return GetStringFUTF16(message_id
, a
);
89 inline std::wstring
GetStringF(int message_id
,
90 const std::wstring
& a
,
91 const std::wstring
& b
) {
92 return GetStringFUTF16(message_id
, a
, b
);
94 inline std::wstring
GetStringF(int message_id
,
95 const std::wstring
& a
,
96 const std::wstring
& b
,
97 const std::wstring
& c
) {
98 return GetStringFUTF16(message_id
, a
, b
, c
);
100 inline std::wstring
GetStringF(int message_id
,
101 const std::wstring
& a
,
102 const std::wstring
& b
,
103 const std::wstring
& c
,
104 const std::wstring
& d
) {
105 return GetStringFUTF16(message_id
, a
, b
, c
, d
);
108 std::wstring
GetStringF(int message_id
,
109 const std::wstring
& a
);
110 std::wstring
GetStringF(int message_id
,
111 const std::wstring
& a
,
112 const std::wstring
& b
);
113 std::wstring
GetStringF(int message_id
,
114 const std::wstring
& a
,
115 const std::wstring
& b
,
116 const std::wstring
& c
);
117 std::wstring
GetStringF(int message_id
,
118 const std::wstring
& a
,
119 const std::wstring
& b
,
120 const std::wstring
& c
,
121 const std::wstring
& d
);
123 std::string
GetStringFUTF8(int message_id
,
125 std::string
GetStringFUTF8(int message_id
,
128 std::string
GetStringFUTF8(int message_id
,
132 std::string
GetStringFUTF8(int message_id
,
138 // Variants that return the offset(s) of the replaced parameters. The
139 // vector based version returns offsets ordered by parameter. For example if
140 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the
141 // offset of b regardless of where the parameters end up in the string.
142 std::wstring
GetStringF(int message_id
,
143 const std::wstring
& a
,
145 std::wstring
GetStringF(int message_id
,
146 const std::wstring
& a
,
147 const std::wstring
& b
,
148 std::vector
<size_t>* offsets
);
149 string16
GetStringFUTF16(int message_id
,
152 string16
GetStringFUTF16(int message_id
,
155 std::vector
<size_t>* offsets
);
157 // Convenience formatters for a single number.
158 std::wstring
GetStringF(int message_id
, int a
);
159 std::wstring
GetStringF(int message_id
, int64 a
);
161 // Truncates the string to length characters. This breaks the string at
162 // the first word break before length, adding the horizontal ellipsis
163 // character (unicode character 0x2026) to render ...
164 // The supplied string is returned if the string has length characters or
166 std::wstring
TruncateString(const std::wstring
& string
, size_t length
);
168 // Returns the lower case equivalent of string.
169 string16
ToLower(const string16
& string
);
171 // Returns the upper case equivalent of string.
172 string16
ToUpper(const string16
& string
);
174 // In place sorting of std::wstring strings using collation rules for |locale|.
175 void SortStrings(const std::string
& locale
,
176 std::vector
<std::wstring
>* strings
);
178 // In place sorting of string16 strings using collation rules for |locale|.
179 void SortStrings16(const std::string
& locale
,
180 std::vector
<string16
>* strings
);
182 // Returns a vector of available locale codes. E.g., a vector containing
183 // en-US, es, fr, fi, pt-PT, pt-BR, etc.
184 const std::vector
<std::string
>& GetAvailableLocales();
186 // Returns a vector of locale codes usable for accept-languages.
187 void GetAcceptLanguagesForLocale(const std::string
& display_locale
,
188 std::vector
<std::string
>* locale_codes
);
191 } // namespace l10n_util
193 #endif // APP_L10N_UTIL_H_