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"
40 explicit StringWrapper(const string16& string) : string_(string) {}
41 const string16& string() const { return string_; }
46 DISALLOW_COPY_AND_ASSIGN(StringWrapper);
51 class L10nUtilTest : public PlatformTest {
55 // TODO(beng): disabled until app strings move to app.
56 TEST_F(L10nUtilTest, DISABLED_GetString) {
57 std::string s = l10n_util::GetStringUTF8(IDS_SIMPLE);
58 EXPECT_EQ(std::string("Hello World!"), s);
60 s = l10n_util::GetStringFUTF8(IDS_PLACEHOLDERS,
61 UTF8ToUTF16("chrome"),
63 EXPECT_EQ(std::string("Hello, chrome. Your number is 10."), s);
65 string16 s16 = l10n_util::GetStringFUTF16Int(IDS_PLACEHOLDERS_2, 20);
66 EXPECT_EQ(UTF8ToUTF16("You owe me $20."), s16);
68 #endif // defined(OS_WIN)
70 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
71 // On Mac, we are disabling this test because GetApplicationLocale() as an
72 // API isn't something that we'll easily be able to unit test in this manner.
73 // The meaning of that API, on the Mac, is "the locale used by Cocoa's main
74 // nib file", which clearly can't be stubbed by a test app that doesn't use
77 // On Android, we are disabling this test since GetApplicationLocale() just
78 // returns the system's locale, which, similarly, is not easily unit tested.
80 void SetDefaultLocaleForTest(const std::string& tag, base::Environment* env) {
81 #if defined(OS_POSIX) && !defined(OS_CHROMEOS)
82 env->SetVar("LANGUAGE", tag);
84 base::i18n::SetICUDefaultLocale(tag);
88 TEST_F(L10nUtilTest, GetAppLocale) {
89 scoped_ptr<base::Environment> env;
90 // Use a temporary locale dir so we don't have to actually build the locale
91 // pak files for this test.
92 base::ScopedPathOverride locale_dir_override(ui::DIR_LOCALES);
93 base::FilePath new_locale_dir;
94 ASSERT_TRUE(PathService::Get(ui::DIR_LOCALES, &new_locale_dir));
95 // Make fake locale files.
96 std::string filenames[] = {
112 for (size_t i = 0; i < arraysize(filenames); ++i) {
113 base::FilePath filename = new_locale_dir.AppendASCII(
114 filenames[i] + ".pak");
115 file_util::WriteFile(filename, "", 0);
118 // Keep a copy of ICU's default locale before we overwrite it.
119 const std::string original_locale = base::i18n::GetConfiguredLocale();
121 #if defined(OS_POSIX) && !defined(OS_CHROMEOS)
122 env.reset(base::Environment::Create());
124 // Test the support of LANGUAGE environment variable.
125 base::i18n::SetICUDefaultLocale("en-US");
126 env->SetVar("LANGUAGE", "xx:fr_CA");
127 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
129 env->SetVar("LANGUAGE", "xx:yy:en_gb.utf-8@quot");
130 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
132 env->SetVar("LANGUAGE", "xx:zh-hk");
133 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
135 // We emulate gettext's behavior here, which ignores LANG/LC_MESSAGES/LC_ALL
136 // when LANGUAGE is specified. If no language specified in LANGUAGE is valid,
137 // then just fallback to the default language, which is en-US for us.
138 base::i18n::SetICUDefaultLocale("fr-FR");
139 env->SetVar("LANGUAGE", "xx:yy");
140 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
142 env->SetVar("LANGUAGE", "/fr:zh_CN");
143 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string()));
145 // Test prioritization of the different environment variables.
146 env->SetVar("LANGUAGE", "fr");
147 env->SetVar("LC_ALL", "es");
148 env->SetVar("LC_MESSAGES", "he");
149 env->SetVar("LANG", "nb");
150 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
151 env->UnSetVar("LANGUAGE");
152 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
153 env->UnSetVar("LC_ALL");
154 EXPECT_EQ("he", l10n_util::GetApplicationLocale(std::string()));
155 env->UnSetVar("LC_MESSAGES");
156 EXPECT_EQ("nb", l10n_util::GetApplicationLocale(std::string()));
157 env->UnSetVar("LANG");
159 SetDefaultLocaleForTest("ca", env.get());
160 EXPECT_EQ("ca", l10n_util::GetApplicationLocale(std::string()));
162 SetDefaultLocaleForTest("ca-ES", env.get());
163 EXPECT_EQ("ca", l10n_util::GetApplicationLocale(std::string()));
165 SetDefaultLocaleForTest("ca@valencia", env.get());
166 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
168 SetDefaultLocaleForTest("ca_ES@valencia", env.get());
169 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
171 SetDefaultLocaleForTest("ca_ES.UTF8@valencia", env.get());
172 EXPECT_EQ("ca@valencia", l10n_util::GetApplicationLocale(std::string()));
173 #endif // defined(OS_POSIX) && !defined(OS_CHROMEOS)
175 SetDefaultLocaleForTest("en-US", env.get());
176 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
178 SetDefaultLocaleForTest("xx", env.get());
179 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(std::string()));
181 #if defined(OS_CHROMEOS)
182 // ChromeOS honors preferred locale first in GetApplicationLocale(),
183 // defaulting to en-US, while other targets first honor other signals.
184 base::i18n::SetICUDefaultLocale("en-GB");
185 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));
187 base::i18n::SetICUDefaultLocale("en-US");
188 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-GB"));
190 base::i18n::SetICUDefaultLocale("en-US");
191 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-AU"));
193 base::i18n::SetICUDefaultLocale("en-US");
194 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-NZ"));
196 base::i18n::SetICUDefaultLocale("en-US");
197 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-CA"));
199 base::i18n::SetICUDefaultLocale("en-US");
200 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-ZA"));
201 #else // !defined(OS_CHROMEOS)
202 SetDefaultLocaleForTest("en-GB", env.get());
203 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
205 SetDefaultLocaleForTest("fr-CA", env.get());
206 EXPECT_EQ("fr", l10n_util::GetApplicationLocale(std::string()));
208 SetDefaultLocaleForTest("es-MX", env.get());
209 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(std::string()));
211 SetDefaultLocaleForTest("es-AR", env.get());
212 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale(std::string()));
214 SetDefaultLocaleForTest("es-ES", env.get());
215 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
217 SetDefaultLocaleForTest("es", env.get());
218 EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string()));
220 SetDefaultLocaleForTest("zh-HK", env.get());
221 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
223 SetDefaultLocaleForTest("zh-MO", env.get());
224 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string()));
226 SetDefaultLocaleForTest("zh-SG", env.get());
227 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string()));
229 SetDefaultLocaleForTest("en-CA", env.get());
230 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
232 SetDefaultLocaleForTest("en-AU", env.get());
233 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
235 SetDefaultLocaleForTest("en-NZ", env.get());
236 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
238 SetDefaultLocaleForTest("en-ZA", env.get());
239 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string()));
240 #endif // defined(OS_CHROMEOS)
243 // We don't allow user prefs for locale on linux/mac.
244 base::i18n::SetICUDefaultLocale("en-US");
245 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr"));
246 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr-CA"));
248 base::i18n::SetICUDefaultLocale("en-US");
249 // Aliases iw, no, tl to he, nb, fil.
250 EXPECT_EQ("he", l10n_util::GetApplicationLocale("iw"));
251 EXPECT_EQ("nb", l10n_util::GetApplicationLocale("no"));
252 EXPECT_EQ("fil", l10n_util::GetApplicationLocale("tl"));
253 // es-419 and es-XX (where XX is not Spain) should be
254 // mapped to es-419 (Latin American Spanish).
255 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-419"));
256 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es-ES"));
257 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-AR"));
259 base::i18n::SetICUDefaultLocale("es-AR");
260 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es"));
262 base::i18n::SetICUDefaultLocale("zh-HK");
263 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("zh-CN"));
265 base::i18n::SetICUDefaultLocale("he");
266 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("en"));
268 // Amharic should be blocked unless OS is Vista or newer.
269 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
270 base::i18n::SetICUDefaultLocale("am");
271 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale(""));
272 base::i18n::SetICUDefaultLocale("en-GB");
273 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("am"));
275 base::i18n::SetICUDefaultLocale("am");
276 EXPECT_EQ("am", l10n_util::GetApplicationLocale(""));
277 base::i18n::SetICUDefaultLocale("en-GB");
278 EXPECT_EQ("am", l10n_util::GetApplicationLocale("am"));
280 #endif // defined(OS_WIN)
283 base::i18n::SetICUDefaultLocale(original_locale);
285 #endif // !defined(OS_MACOSX)
287 TEST_F(L10nUtilTest, SortStringsUsingFunction) {
288 std::vector<StringWrapper*> strings;
289 strings.push_back(new StringWrapper(UTF8ToUTF16("C")));
290 strings.push_back(new StringWrapper(UTF8ToUTF16("d")));
291 strings.push_back(new StringWrapper(UTF8ToUTF16("b")));
292 strings.push_back(new StringWrapper(UTF8ToUTF16("a")));
293 l10n_util::SortStringsUsingMethod("en-US",
295 &StringWrapper::string);
296 ASSERT_TRUE(UTF8ToUTF16("a") == strings[0]->string());
297 ASSERT_TRUE(UTF8ToUTF16("b") == strings[1]->string());
298 ASSERT_TRUE(UTF8ToUTF16("C") == strings[2]->string());
299 ASSERT_TRUE(UTF8ToUTF16("d") == strings[3]->string());
300 STLDeleteElements(&strings);
304 * Helper method for validating strings that require direcitonal markup.
305 * Checks that parentheses are enclosed in appropriate direcitonal markers.
307 void CheckUiDisplayNameForLocale(const std::string& locale,
308 const std::string& display_locale,
310 EXPECT_EQ(true, base::i18n::IsRTL());
311 string16 result = l10n_util::GetDisplayNameForLocale(locale,
313 /* is_for_ui */ true);
315 bool rtl_direction = true;
316 for (size_t i = 0; i < result.length() - 1; i++) {
317 char16 ch = result.at(i);
319 case base::i18n::kLeftToRightMark:
320 case base::i18n::kLeftToRightEmbeddingMark:
321 rtl_direction = false;
323 case base::i18n::kRightToLeftMark:
324 case base::i18n::kRightToLeftEmbeddingMark:
325 rtl_direction = true;
329 EXPECT_EQ(is_rtl, rtl_direction);
334 TEST_F(L10nUtilTest, GetDisplayNameForLocale) {
335 // TODO(jungshik): Make this test more extensive.
336 // Test zh-CN and zh-TW are treated as zh-Hans and zh-Hant.
337 string16 result = l10n_util::GetDisplayNameForLocale("zh-CN", "en", false);
338 EXPECT_EQ(ASCIIToUTF16("Chinese (Simplified Han)"), result);
340 result = l10n_util::GetDisplayNameForLocale("zh-TW", "en", false);
341 EXPECT_EQ(ASCIIToUTF16("Chinese (Traditional Han)"), result);
343 result = l10n_util::GetDisplayNameForLocale("pt-BR", "en", false);
344 EXPECT_EQ(ASCIIToUTF16("Portuguese (Brazil)"), result);
346 result = l10n_util::GetDisplayNameForLocale("es-419", "en", false);
347 EXPECT_EQ(ASCIIToUTF16("Spanish (Latin America)"), result);
349 result = l10n_util::GetDisplayNameForLocale("-BR", "en", false);
350 EXPECT_EQ(ASCIIToUTF16("Brazil"), result);
352 result = l10n_util::GetDisplayNameForLocale("xyz-xyz", "en", false);
353 EXPECT_EQ(ASCIIToUTF16("xyz (XYZ)"), result);
355 #if !defined(TOOLKIT_GTK)
356 // Check for directional markers when using RTL languages to ensure that
357 // direction neutral characters such as parentheses are properly formatted.
359 // Keep a copy of ICU's default locale before we overwrite it.
360 const std::string original_locale = base::i18n::GetConfiguredLocale();
362 base::i18n::SetICUDefaultLocale("he");
363 CheckUiDisplayNameForLocale("en-US", "en", false);
364 CheckUiDisplayNameForLocale("en-US", "he", true);
367 base::i18n::SetICUDefaultLocale(original_locale);
370 // ToUpper and ToLower should work with embedded NULLs.
371 const size_t length_with_null = 4;
372 char16 buf_with_null[length_with_null] = { 0, 'a', 0, 'b' };
373 string16 string16_with_null(buf_with_null, length_with_null);
375 string16 upper_with_null = base::i18n::ToUpper(string16_with_null);
376 ASSERT_EQ(length_with_null, upper_with_null.size());
377 EXPECT_TRUE(upper_with_null[0] == 0 && upper_with_null[1] == 'A' &&
378 upper_with_null[2] == 0 && upper_with_null[3] == 'B');
380 string16 lower_with_null = base::i18n::ToLower(upper_with_null);
381 ASSERT_EQ(length_with_null, upper_with_null.size());
382 EXPECT_TRUE(lower_with_null[0] == 0 && lower_with_null[1] == 'a' &&
383 lower_with_null[2] == 0 && lower_with_null[3] == 'b');
386 TEST_F(L10nUtilTest, GetDisplayNameForCountry) {
387 string16 result = l10n_util::GetDisplayNameForCountry("BR", "en");
388 EXPECT_EQ(ASCIIToUTF16("Brazil"), result);
390 result = l10n_util::GetDisplayNameForCountry("419", "en");
391 EXPECT_EQ(ASCIIToUTF16("Latin America"), result);
393 result = l10n_util::GetDisplayNameForCountry("xyz", "en");
394 EXPECT_EQ(ASCIIToUTF16("XYZ"), result);
397 TEST_F(L10nUtilTest, GetParentLocales) {
398 std::vector<std::string> locales;
399 const std::string top_locale("sr_Cyrl_RS");
400 l10n_util::GetParentLocales(top_locale, &locales);
402 ASSERT_EQ(3U, locales.size());
403 EXPECT_EQ("sr_Cyrl_RS", locales[0]);
404 EXPECT_EQ("sr_Cyrl", locales[1]);
405 EXPECT_EQ("sr", locales[2]);
408 TEST_F(L10nUtilTest, IsValidLocaleSyntax) {
409 // Test valid locales.
410 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en"));
411 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr"));
412 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("de"));
413 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("pt"));
414 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh"));
415 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fil"));
416 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("haw"));
417 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en-US"));
418 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_US"));
419 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_GB"));
420 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("pt-BR"));
421 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_CN"));
422 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hans"));
423 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hans_CN"));
424 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hant"));
425 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zh_Hant_TW"));
426 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr_CA"));
427 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("i-klingon"));
428 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("es-419"));
429 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE_PREEURO"));
430 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE_u_cu_IEP"));
431 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("en_IE@currency=IEP"));
432 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("fr@x=y"));
433 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax("zn_CN@foo=bar"));
434 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(
435 "fr@collation=phonebook;calendar=islamic-civil"));
436 EXPECT_TRUE(l10n_util::IsValidLocaleSyntax(
437 "sr_Latn_RS_REVISED@currency=USD"));
439 // Test invalid locales.
440 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax(std::string()));
441 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("x"));
442 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("12"));
443 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("456"));
444 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("a1"));
445 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("enUS"));
446 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("zhcn"));
447 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en.US"));
448 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en#US"));
449 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("-en-US"));
450 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US-"));
451 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("123-en-US"));
452 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("Latin"));
453 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("German"));
454 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("pt--BR"));
455 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("sl-macedonia"));
456 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("@"));
457 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@"));
458 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x"));
459 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x="));
460 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@=y"));