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 #include "build/build_config.h"
7 #if defined(OS_POSIX) && !defined(OS_MACOSX)
11 #include "base/basictypes.h"
12 #include "base/environment.h"
13 #include "base/file_util.h"
14 #include "base/i18n/case_conversion.h"
15 #include "base/i18n/rtl.h"
16 #include "base/path_service.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/test/scoped_path_override.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "testing/platform_test.h"
23 #include "third_party/icu/source/common/unicode/locid.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/l10n/l10n_util_collator.h"
26 #include "ui/base/ui_base_paths.h"
29 #include "base/win/windows_version.h"
32 #if !defined(OS_MACOSX)
33 #include "ui/base/test/data/resource.h"
36 using base::ASCIIToUTF16
;
37 using base::UTF8ToUTF16
;
43 explicit StringWrapper(const base::string16
& string
) : string_(string
) {}
44 const base::string16
& string() const { return string_
; }
47 base::string16 string_
;
49 DISALLOW_COPY_AND_ASSIGN(StringWrapper
);
54 class L10nUtilTest
: public PlatformTest
{
58 // TODO(beng): disabled until app strings move to app.
59 TEST_F(L10nUtilTest
, DISABLED_GetString
) {
60 std::string s
= l10n_util::GetStringUTF8(IDS_SIMPLE
);
61 EXPECT_EQ(std::string("Hello World!"), s
);
63 s
= l10n_util::GetStringFUTF8(IDS_PLACEHOLDERS
,
64 UTF8ToUTF16("chrome"),
66 EXPECT_EQ(std::string("Hello, chrome. Your number is 10."), s
);
68 base::string16 s16
= l10n_util::GetStringFUTF16Int(IDS_PLACEHOLDERS_2
, 20);
69 EXPECT_EQ(UTF8ToUTF16("You owe me $20."), s16
);
71 #endif // defined(OS_WIN)
73 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
74 // On Mac, we are disabling this test because GetApplicationLocale() as an
75 // API isn't something that we'll easily be able to unit test in this manner.
76 // The meaning of that API, on the Mac, is "the locale used by Cocoa's main
77 // nib file", which clearly can't be stubbed by a test app that doesn't use
80 // On Android, we are disabling this test since GetApplicationLocale() just
81 // returns the system's locale, which, similarly, is not easily unit tested.
83 #if defined(OS_POSIX) && defined(USE_GLIB) && !defined(OS_CHROMEOS)
84 const bool kPlatformHasDefaultLocale
= 1;
85 const bool kUseLocaleFromEnvironment
= 1;
86 const bool kSupportsLocalePreference
= 0;
88 const bool kPlatformHasDefaultLocale
= 1;
89 const bool kUseLocaleFromEnvironment
= 0;
90 const bool kSupportsLocalePreference
= 1;
92 const bool kPlatformHasDefaultLocale
= 0;
93 const bool kUseLocaleFromEnvironment
= 0;
94 const bool kSupportsLocalePreference
= 1;
97 void SetDefaultLocaleForTest(const std::string
& tag
, base::Environment
* env
) {
98 if (kUseLocaleFromEnvironment
)
99 env
->SetVar("LANGUAGE", tag
);
101 base::i18n::SetICUDefaultLocale(tag
);
104 TEST_F(L10nUtilTest
, GetAppLocale
) {
105 scoped_ptr
<base::Environment
> env
;
106 // Use a temporary locale dir so we don't have to actually build the locale
107 // pak files for this test.
108 base::ScopedPathOverride
locale_dir_override(ui::DIR_LOCALES
);
109 base::FilePath new_locale_dir
;
110 ASSERT_TRUE(PathService::Get(ui::DIR_LOCALES
, &new_locale_dir
));
111 // Make fake locale files.
112 std::string filenames
[] = {
128 for (size_t i
= 0; i
< arraysize(filenames
); ++i
) {
129 base::FilePath filename
= new_locale_dir
.AppendASCII(
130 filenames
[i
] + ".pak");
131 base::WriteFile(filename
, "", 0);
134 // Keep a copy of ICU's default locale before we overwrite it.
135 const std::string original_locale
= base::i18n::GetConfiguredLocale();
137 if (kPlatformHasDefaultLocale
&& kUseLocaleFromEnvironment
) {
138 env
.reset(base::Environment::Create());
140 // Test the support of LANGUAGE environment variable.
141 base::i18n::SetICUDefaultLocale("en-US");
142 env
->SetVar("LANGUAGE", "xx:fr_CA");
143 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
145 env
->SetVar("LANGUAGE", "xx:yy:en_gb.utf-8@quot");
146 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
148 env
->SetVar("LANGUAGE", "xx:zh-hk");
149 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
151 // We emulate gettext's behavior here, which ignores LANG/LC_MESSAGES/LC_ALL
152 // when LANGUAGE is specified. If no language specified in LANGUAGE is
154 // then just fallback to the default language, which is en-US for us.
155 base::i18n::SetICUDefaultLocale("fr-FR");
156 env
->SetVar("LANGUAGE", "xx:yy");
157 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
159 env
->SetVar("LANGUAGE", "/fr:zh_CN");
160 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string()));
162 // Test prioritization of the different environment variables.
163 env
->SetVar("LANGUAGE", "fr");
164 env
->SetVar("LC_ALL", "es");
165 env
->SetVar("LC_MESSAGES", "he");
166 env
->SetVar("LANG", "nb");
167 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
168 env
->UnSetVar("LANGUAGE");
169 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
170 env
->UnSetVar("LC_ALL");
171 EXPECT_EQ("he", l10n_util::GetApplicationLocale(std::string()));
172 env
->UnSetVar("LC_MESSAGES");
173 EXPECT_EQ("nb", l10n_util::GetApplicationLocale(std::string()));
174 env
->UnSetVar("LANG");
176 SetDefaultLocaleForTest("ca", env
.get());
177 EXPECT_EQ("ca", l10n_util::GetApplicationLocale(std::string()));
179 SetDefaultLocaleForTest("ca-ES", env
.get());
180 EXPECT_EQ("ca", l10n_util::GetApplicationLocale(std::string()));
182 SetDefaultLocaleForTest("ca@valencia", env
.get());
183 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
185 SetDefaultLocaleForTest("ca_ES@valencia", env
.get());
186 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
188 SetDefaultLocaleForTest("ca_ES.UTF8@valencia", env
.get());
189 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
192 SetDefaultLocaleForTest("en-US", env
.get());
193 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
195 SetDefaultLocaleForTest("xx", env
.get());
196 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
198 if (!kPlatformHasDefaultLocale
) {
199 // ChromeOS & embedded use only browser prefs in GetApplicationLocale(),
200 // ignoring the environment, and default to en-US. Other platforms honor
201 // the default locale from the OS or environment.
202 SetDefaultLocaleForTest("en-GB", env
.get());
203 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));
205 SetDefaultLocaleForTest("en-US", env
.get());
206 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-GB"));
208 SetDefaultLocaleForTest("en-US", env
.get());
209 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-AU"));
211 SetDefaultLocaleForTest("en-US", env
.get());
212 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-NZ"));
214 SetDefaultLocaleForTest("en-US", env
.get());
215 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-CA"));
217 SetDefaultLocaleForTest("en-US", env
.get());
218 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-ZA"));
220 // Most platforms have an OS-provided locale. This locale is preferred.
221 SetDefaultLocaleForTest("en-GB", env
.get());
222 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
224 SetDefaultLocaleForTest("fr-CA", env
.get());
225 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
227 SetDefaultLocaleForTest("es-MX", env
.get());
228 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(std::string()));
230 SetDefaultLocaleForTest("es-AR", env
.get());
231 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(std::string()));
233 SetDefaultLocaleForTest("es-ES", env
.get());
234 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
236 SetDefaultLocaleForTest("es", env
.get());
237 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
239 SetDefaultLocaleForTest("zh-HK", env
.get());
240 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
242 SetDefaultLocaleForTest("zh-MO", env
.get());
243 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
245 SetDefaultLocaleForTest("zh-SG", env
.get());
246 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string()));
248 SetDefaultLocaleForTest("en-CA", env
.get());
249 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
251 SetDefaultLocaleForTest("en-AU", env
.get());
252 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
254 SetDefaultLocaleForTest("en-NZ", env
.get());
255 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
257 SetDefaultLocaleForTest("en-ZA", env
.get());
258 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
261 if (kSupportsLocalePreference
) {
262 // On windows, the user can override the locale in preferences.
263 base::i18n::SetICUDefaultLocale("en-US");
264 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr"));
265 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr-CA"));
267 base::i18n::SetICUDefaultLocale("en-US");
268 // Aliases iw, no, tl to he, nb, fil.
269 EXPECT_EQ("he", l10n_util::GetApplicationLocale("iw"));
270 EXPECT_EQ("nb", l10n_util::GetApplicationLocale("no"));
271 EXPECT_EQ("fil", l10n_util::GetApplicationLocale("tl"));
272 // es-419 and es-XX (where XX is not Spain) should be
273 // mapped to es-419 (Latin American Spanish).
274 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-419"));
275 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es-ES"));
276 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-AR"));
278 base::i18n::SetICUDefaultLocale("es-AR");
279 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es"));
281 base::i18n::SetICUDefaultLocale("zh-HK");
282 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("zh-CN"));
284 base::i18n::SetICUDefaultLocale("he");
285 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("en"));
289 // Amharic should be blocked unless OS is Vista or newer.
290 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
291 base::i18n::SetICUDefaultLocale("am");
292 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));
293 base::i18n::SetICUDefaultLocale("en-GB");
294 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("am"));
296 base::i18n::SetICUDefaultLocale("am");
297 EXPECT_EQ("am", l10n_util::GetApplicationLocale(""));
298 base::i18n::SetICUDefaultLocale("en-GB");
299 EXPECT_EQ("am", l10n_util::GetApplicationLocale("am"));
301 #endif // defined(OS_WIN)
304 base::i18n::SetICUDefaultLocale(original_locale
);
306 #endif // !defined(OS_MACOSX)
308 TEST_F(L10nUtilTest
, SortStringsUsingFunction
) {
309 std::vector
<StringWrapper
*> strings
;
310 strings
.push_back(new StringWrapper(UTF8ToUTF16("C")));
311 strings
.push_back(new StringWrapper(UTF8ToUTF16("d")));
312 strings
.push_back(new StringWrapper(UTF8ToUTF16("b")));
313 strings
.push_back(new StringWrapper(UTF8ToUTF16("a")));
314 l10n_util::SortStringsUsingMethod("en-US",
316 &StringWrapper::string
);
317 ASSERT_TRUE(UTF8ToUTF16("a") == strings
[0]->string());
318 ASSERT_TRUE(UTF8ToUTF16("b") == strings
[1]->string());
319 ASSERT_TRUE(UTF8ToUTF16("C") == strings
[2]->string());
320 ASSERT_TRUE(UTF8ToUTF16("d") == strings
[3]->string());
321 STLDeleteElements(&strings
);
325 * Helper method for validating strings that require direcitonal markup.
326 * Checks that parentheses are enclosed in appropriate direcitonal markers.
328 void CheckUiDisplayNameForLocale(const std::string
& locale
,
329 const std::string
& display_locale
,
331 EXPECT_EQ(true, base::i18n::IsRTL());
332 base::string16 result
= l10n_util::GetDisplayNameForLocale(locale
,
334 /* is_for_ui */ true);
336 bool rtl_direction
= true;
337 for (size_t i
= 0; i
< result
.length() - 1; i
++) {
338 base::char16 ch
= result
.at(i
);
340 case base::i18n::kLeftToRightMark
:
341 case base::i18n::kLeftToRightEmbeddingMark
:
342 rtl_direction
= false;
344 case base::i18n::kRightToLeftMark
:
345 case base::i18n::kRightToLeftEmbeddingMark
:
346 rtl_direction
= true;
350 EXPECT_EQ(is_rtl
, rtl_direction
);
355 TEST_F(L10nUtilTest
, GetDisplayNameForLocale
) {
356 // TODO(jungshik): Make this test more extensive.
357 // Test zh-CN and zh-TW are treated as zh-Hans and zh-Hant.
358 base::string16 result
=
359 l10n_util::GetDisplayNameForLocale("zh-CN", "en", false);
360 EXPECT_EQ(ASCIIToUTF16("Chinese (Simplified Han)"), result
);
362 result
= l10n_util::GetDisplayNameForLocale("zh-TW", "en", false);
363 EXPECT_EQ(ASCIIToUTF16("Chinese (Traditional Han)"), result
);
365 result
= l10n_util::GetDisplayNameForLocale("pt-BR", "en", false);
366 EXPECT_EQ(ASCIIToUTF16("Portuguese (Brazil)"), result
);
368 result
= l10n_util::GetDisplayNameForLocale("es-419", "en", false);
369 EXPECT_EQ(ASCIIToUTF16("Spanish (Latin America)"), result
);
371 result
= l10n_util::GetDisplayNameForLocale("-BR", "en", false);
372 EXPECT_EQ(ASCIIToUTF16("Brazil"), result
);
374 result
= l10n_util::GetDisplayNameForLocale("xyz-xyz", "en", false);
375 EXPECT_EQ(ASCIIToUTF16("xyz (XYZ)"), result
);
377 // Check for directional markers when using RTL languages to ensure that
378 // direction neutral characters such as parentheses are properly formatted.
380 // Keep a copy of ICU's default locale before we overwrite it.
381 const std::string original_locale
= base::i18n::GetConfiguredLocale();
383 base::i18n::SetICUDefaultLocale("he");
384 CheckUiDisplayNameForLocale("en-US", "en", false);
385 CheckUiDisplayNameForLocale("en-US", "he", true);
388 base::i18n::SetICUDefaultLocale(original_locale
);
390 // ToUpper and ToLower should work with embedded NULLs.
391 const size_t length_with_null
= 4;
392 base::char16 buf_with_null
[length_with_null
] = { 0, 'a', 0, 'b' };
393 base::string16
string16_with_null(buf_with_null
, length_with_null
);
395 base::string16 upper_with_null
= base::i18n::ToUpper(string16_with_null
);
396 ASSERT_EQ(length_with_null
, upper_with_null
.size());
397 EXPECT_TRUE(upper_with_null
[0] == 0 && upper_with_null
[1] == 'A' &&
398 upper_with_null
[2] == 0 && upper_with_null
[3] == 'B');
400 base::string16 lower_with_null
= base::i18n::ToLower(upper_with_null
);
401 ASSERT_EQ(length_with_null
, upper_with_null
.size());
402 EXPECT_TRUE(lower_with_null
[0] == 0 && lower_with_null
[1] == 'a' &&
403 lower_with_null
[2] == 0 && lower_with_null
[3] == 'b');
406 TEST_F(L10nUtilTest
, GetDisplayNameForCountry
) {
407 base::string16 result
= l10n_util::GetDisplayNameForCountry("BR", "en");
408 EXPECT_EQ(ASCIIToUTF16("Brazil"), result
);
410 result
= l10n_util::GetDisplayNameForCountry("419", "en");
411 EXPECT_EQ(ASCIIToUTF16("Latin America"), result
);
413 result
= l10n_util::GetDisplayNameForCountry("xyz", "en");
414 EXPECT_EQ(ASCIIToUTF16("XYZ"), result
);
417 TEST_F(L10nUtilTest
, GetParentLocales
) {
418 std::vector
<std::string
> locales
;
419 const std::string
top_locale("sr_Cyrl_RS");
420 l10n_util::GetParentLocales(top_locale
, &locales
);
422 ASSERT_EQ(3U, locales
.size());
423 EXPECT_EQ("sr_Cyrl_RS", locales
[0]);
424 EXPECT_EQ("sr_Cyrl", locales
[1]);
425 EXPECT_EQ("sr", locales
[2]);
428 TEST_F(L10nUtilTest
, IsValidLocaleSyntax
) {
429 // Test valid locales.
430 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en"));
431 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr"));
432 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("de"));
433 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("pt"));
434 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh"));
435 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fil"));
436 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("haw"));
437 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en-US"));
438 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_US"));
439 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_GB"));
440 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("pt-BR"));
441 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_CN"));
442 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hans"));
443 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hans_CN"));
444 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hant"));
445 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hant_TW"));
446 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr_CA"));
447 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("i-klingon"));
448 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("es-419"));
449 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE_PREEURO"));
450 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE_u_cu_IEP"));
451 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE@currency=IEP"));
452 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr@x=y"));
453 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zn_CN@foo=bar"));
454 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(
455 "fr@collation=phonebook;calendar=islamic-civil"));
456 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(
457 "sr_Latn_RS_REVISED@currency=USD"));
459 // Test invalid locales.
460 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax(std::string()));
461 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("x"));
462 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("12"));
463 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("456"));
464 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("a1"));
465 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("enUS"));
466 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("zhcn"));
467 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en.US"));
468 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en#US"));
469 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("-en-US"));
470 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US-"));
471 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("123-en-US"));
472 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("Latin"));
473 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("German"));
474 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("pt--BR"));
475 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("sl-macedonia"));
476 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("@"));
477 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@"));
478 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x"));
479 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x="));
480 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@=y"));