OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 7 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/environment.h" | 12 #include "base/environment.h" |
13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
14 #include "base/i18n/case_conversion.h" | 14 #include "base/i18n/case_conversion.h" |
| 15 #include "base/i18n/rtl.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
17 #include "base/string_util.h" | 18 #include "base/string_util.h" |
18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/l10n/l10n_util_collator.h" | 23 #include "ui/base/l10n/l10n_util_collator.h" |
23 #include "ui/base/ui_base_paths.h" | 24 #include "ui/base/ui_base_paths.h" |
24 #include "unicode/locid.h" | 25 #include "unicode/locid.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 s = l10n_util::GetStringFUTF8(IDS_PLACEHOLDERS, | 59 s = l10n_util::GetStringFUTF8(IDS_PLACEHOLDERS, |
59 UTF8ToUTF16("chrome"), | 60 UTF8ToUTF16("chrome"), |
60 UTF8ToUTF16("10")); | 61 UTF8ToUTF16("10")); |
61 EXPECT_EQ(std::string("Hello, chrome. Your number is 10."), s); | 62 EXPECT_EQ(std::string("Hello, chrome. Your number is 10."), s); |
62 | 63 |
63 string16 s16 = l10n_util::GetStringFUTF16Int(IDS_PLACEHOLDERS_2, 20); | 64 string16 s16 = l10n_util::GetStringFUTF16Int(IDS_PLACEHOLDERS_2, 20); |
64 EXPECT_EQ(UTF8ToUTF16("You owe me $20."), s16); | 65 EXPECT_EQ(UTF8ToUTF16("You owe me $20."), s16); |
65 } | 66 } |
66 #endif // defined(OS_WIN) | 67 #endif // defined(OS_WIN) |
67 | 68 |
68 void SetICUDefaultLocale(const std::string& locale_string) { | |
69 icu::Locale locale(locale_string.c_str()); | |
70 UErrorCode error_code = U_ZERO_ERROR; | |
71 icu::Locale::setDefault(locale, error_code); | |
72 EXPECT_TRUE(U_SUCCESS(error_code)); | |
73 } | |
74 | |
75 #if !defined(OS_MACOSX) | 69 #if !defined(OS_MACOSX) |
76 // We are disabling this test on MacOS because GetApplicationLocale() as an | 70 // We are disabling this test on MacOS because GetApplicationLocale() as an |
77 // API isn't something that we'll easily be able to unit test in this manner. | 71 // API isn't something that we'll easily be able to unit test in this manner. |
78 // The meaning of that API, on the Mac, is "the locale used by Cocoa's main | 72 // The meaning of that API, on the Mac, is "the locale used by Cocoa's main |
79 // nib file", which clearly can't be stubbed by a test app that doesn't use | 73 // nib file", which clearly can't be stubbed by a test app that doesn't use |
80 // Cocoa. | 74 // Cocoa. |
81 | 75 |
82 void SetDefaultLocaleForTest(const std::string& tag, base::Environment* env) { | 76 void SetDefaultLocaleForTest(const std::string& tag, base::Environment* env) { |
83 #if defined(OS_POSIX) && !defined(OS_CHROMEOS) | 77 #if defined(OS_POSIX) && !defined(OS_CHROMEOS) |
84 env->SetVar("LANGUAGE", tag); | 78 env->SetVar("LANGUAGE", tag); |
85 #else | 79 #else |
86 SetICUDefaultLocale(tag); | 80 base::i18n::SetICUDefaultLocale(tag); |
87 #endif | 81 #endif |
88 } | 82 } |
89 | 83 |
90 TEST_F(L10nUtilTest, GetAppLocale) { | 84 TEST_F(L10nUtilTest, GetAppLocale) { |
91 scoped_ptr<base::Environment> env; | 85 scoped_ptr<base::Environment> env; |
92 // Use a temporary locale dir so we don't have to actually build the locale | 86 // Use a temporary locale dir so we don't have to actually build the locale |
93 // pak files for this test. | 87 // pak files for this test. |
94 FilePath orig_locale_dir; | 88 FilePath orig_locale_dir; |
95 PathService::Get(ui::DIR_LOCALES, &orig_locale_dir); | 89 PathService::Get(ui::DIR_LOCALES, &orig_locale_dir); |
96 FilePath new_locale_dir; | 90 FilePath new_locale_dir; |
(...skipping 24 matching lines...) Expand all Loading... |
121 file_util::WriteFile(filename, "", 0); | 115 file_util::WriteFile(filename, "", 0); |
122 } | 116 } |
123 | 117 |
124 // Keep a copy of ICU's default locale before we overwrite it. | 118 // Keep a copy of ICU's default locale before we overwrite it. |
125 icu::Locale locale = icu::Locale::getDefault(); | 119 icu::Locale locale = icu::Locale::getDefault(); |
126 | 120 |
127 #if defined(OS_POSIX) && !defined(OS_CHROMEOS) | 121 #if defined(OS_POSIX) && !defined(OS_CHROMEOS) |
128 env.reset(base::Environment::Create()); | 122 env.reset(base::Environment::Create()); |
129 | 123 |
130 // Test the support of LANGUAGE environment variable. | 124 // Test the support of LANGUAGE environment variable. |
131 SetICUDefaultLocale("en-US"); | 125 base::i18n::SetICUDefaultLocale("en-US"); |
132 env->SetVar("LANGUAGE", "xx:fr_CA"); | 126 env->SetVar("LANGUAGE", "xx:fr_CA"); |
133 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("")); | 127 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("")); |
134 | 128 |
135 env->SetVar("LANGUAGE", "xx:yy:en_gb.utf-8@quot"); | 129 env->SetVar("LANGUAGE", "xx:yy:en_gb.utf-8@quot"); |
136 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); | 130 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); |
137 | 131 |
138 env->SetVar("LANGUAGE", "xx:zh-hk"); | 132 env->SetVar("LANGUAGE", "xx:zh-hk"); |
139 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale("")); | 133 EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale("")); |
140 | 134 |
141 // We emulate gettext's behavior here, which ignores LANG/LC_MESSAGES/LC_ALL | 135 // We emulate gettext's behavior here, which ignores LANG/LC_MESSAGES/LC_ALL |
142 // when LANGUAGE is specified. If no language specified in LANGUAGE is valid, | 136 // when LANGUAGE is specified. If no language specified in LANGUAGE is valid, |
143 // then just fallback to the default language, which is en-US for us. | 137 // then just fallback to the default language, which is en-US for us. |
144 SetICUDefaultLocale("fr-FR"); | 138 base::i18n::SetICUDefaultLocale("fr-FR"); |
145 env->SetVar("LANGUAGE", "xx:yy"); | 139 env->SetVar("LANGUAGE", "xx:yy"); |
146 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); | 140 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); |
147 | 141 |
148 env->SetVar("LANGUAGE", "/fr:zh_CN"); | 142 env->SetVar("LANGUAGE", "/fr:zh_CN"); |
149 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("")); | 143 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("")); |
150 | 144 |
151 // Test prioritization of the different environment variables. | 145 // Test prioritization of the different environment variables. |
152 env->SetVar("LANGUAGE", "fr"); | 146 env->SetVar("LANGUAGE", "fr"); |
153 env->SetVar("LC_ALL", "es"); | 147 env->SetVar("LC_ALL", "es"); |
154 env->SetVar("LC_MESSAGES", "he"); | 148 env->SetVar("LC_MESSAGES", "he"); |
(...skipping 25 matching lines...) Expand all Loading... |
180 | 174 |
181 SetDefaultLocaleForTest("en-US", env.get()); | 175 SetDefaultLocaleForTest("en-US", env.get()); |
182 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); | 176 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); |
183 | 177 |
184 SetDefaultLocaleForTest("xx", env.get()); | 178 SetDefaultLocaleForTest("xx", env.get()); |
185 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); | 179 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); |
186 | 180 |
187 #if defined(OS_CHROMEOS) | 181 #if defined(OS_CHROMEOS) |
188 // ChromeOS honors preferred locale first in GetApplicationLocale(), | 182 // ChromeOS honors preferred locale first in GetApplicationLocale(), |
189 // defaulting to en-US, while other targets first honor other signals. | 183 // defaulting to en-US, while other targets first honor other signals. |
190 SetICUDefaultLocale("en-GB"); | 184 base::i18n::SetICUDefaultLocale("en-GB"); |
191 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); | 185 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); |
192 | 186 |
193 SetICUDefaultLocale("en-US"); | 187 base::i18n::SetICUDefaultLocale("en-US"); |
194 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-GB")); | 188 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-GB")); |
195 | 189 |
196 SetICUDefaultLocale("en-US"); | 190 base::i18n::SetICUDefaultLocale("en-US"); |
197 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-AU")); | 191 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-AU")); |
198 | 192 |
199 SetICUDefaultLocale("en-US"); | 193 base::i18n::SetICUDefaultLocale("en-US"); |
200 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-NZ")); | 194 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-NZ")); |
201 | 195 |
202 SetICUDefaultLocale("en-US"); | 196 base::i18n::SetICUDefaultLocale("en-US"); |
203 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-CA")); | 197 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-CA")); |
204 | 198 |
205 SetICUDefaultLocale("en-US"); | 199 base::i18n::SetICUDefaultLocale("en-US"); |
206 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-ZA")); | 200 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("en-ZA")); |
207 #else // defined(OS_CHROMEOS) | 201 #else // defined(OS_CHROMEOS) |
208 SetDefaultLocaleForTest("en-GB", env.get()); | 202 SetDefaultLocaleForTest("en-GB", env.get()); |
209 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); | 203 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); |
210 | 204 |
211 SetDefaultLocaleForTest("fr-CA", env.get()); | 205 SetDefaultLocaleForTest("fr-CA", env.get()); |
212 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("")); | 206 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("")); |
213 | 207 |
214 SetDefaultLocaleForTest("es-MX", env.get()); | 208 SetDefaultLocaleForTest("es-MX", env.get()); |
215 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("")); | 209 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("")); |
(...skipping 24 matching lines...) Expand all Loading... |
240 | 234 |
241 SetDefaultLocaleForTest("en-NZ", env.get()); | 235 SetDefaultLocaleForTest("en-NZ", env.get()); |
242 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); | 236 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); |
243 | 237 |
244 SetDefaultLocaleForTest("en-ZA", env.get()); | 238 SetDefaultLocaleForTest("en-ZA", env.get()); |
245 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); | 239 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("")); |
246 #endif // defined (OS_CHROMEOS) | 240 #endif // defined (OS_CHROMEOS) |
247 | 241 |
248 #if defined(OS_WIN) | 242 #if defined(OS_WIN) |
249 // We don't allow user prefs for locale on linux/mac. | 243 // We don't allow user prefs for locale on linux/mac. |
250 SetICUDefaultLocale("en-US"); | 244 base::i18n::SetICUDefaultLocale("en-US"); |
251 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr")); | 245 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr")); |
252 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr-CA")); | 246 EXPECT_EQ("fr", l10n_util::GetApplicationLocale("fr-CA")); |
253 | 247 |
254 SetICUDefaultLocale("en-US"); | 248 base::i18n::SetICUDefaultLocale("en-US"); |
255 // Aliases iw, no, tl to he, nb, fil. | 249 // Aliases iw, no, tl to he, nb, fil. |
256 EXPECT_EQ("he", l10n_util::GetApplicationLocale("iw")); | 250 EXPECT_EQ("he", l10n_util::GetApplicationLocale("iw")); |
257 EXPECT_EQ("nb", l10n_util::GetApplicationLocale("no")); | 251 EXPECT_EQ("nb", l10n_util::GetApplicationLocale("no")); |
258 EXPECT_EQ("fil", l10n_util::GetApplicationLocale("tl")); | 252 EXPECT_EQ("fil", l10n_util::GetApplicationLocale("tl")); |
259 // es-419 and es-XX (where XX is not Spain) should be | 253 // es-419 and es-XX (where XX is not Spain) should be |
260 // mapped to es-419 (Latin American Spanish). | 254 // mapped to es-419 (Latin American Spanish). |
261 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-419")); | 255 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-419")); |
262 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es-ES")); | 256 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es-ES")); |
263 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-AR")); | 257 EXPECT_EQ("es-419", l10n_util::GetApplicationLocale("es-AR")); |
264 | 258 |
265 SetICUDefaultLocale("es-AR"); | 259 base::i18n::SetICUDefaultLocale("es-AR"); |
266 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es")); | 260 EXPECT_EQ("es", l10n_util::GetApplicationLocale("es")); |
267 | 261 |
268 SetICUDefaultLocale("zh-HK"); | 262 base::i18n::SetICUDefaultLocale("zh-HK"); |
269 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("zh-CN")); | 263 EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale("zh-CN")); |
270 | 264 |
271 SetICUDefaultLocale("he"); | 265 base::i18n::SetICUDefaultLocale("he"); |
272 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("en")); | 266 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("en")); |
273 | 267 |
274 // Amharic should be blocked unless OS is Vista or newer. | 268 // Amharic should be blocked unless OS is Vista or newer. |
275 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | 269 if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
276 SetICUDefaultLocale("am"); | 270 base::i18n::SetICUDefaultLocale("am"); |
277 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); | 271 EXPECT_EQ("en-US", l10n_util::GetApplicationLocale("")); |
278 SetICUDefaultLocale("en-GB"); | 272 base::i18n::SetICUDefaultLocale("en-GB"); |
279 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("am")); | 273 EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale("am")); |
280 } else { | 274 } else { |
281 SetICUDefaultLocale("am"); | 275 base::i18n::SetICUDefaultLocale("am"); |
282 EXPECT_EQ("am", l10n_util::GetApplicationLocale("")); | 276 EXPECT_EQ("am", l10n_util::GetApplicationLocale("")); |
283 SetICUDefaultLocale("en-GB"); | 277 base::i18n::SetICUDefaultLocale("en-GB"); |
284 EXPECT_EQ("am", l10n_util::GetApplicationLocale("am")); | 278 EXPECT_EQ("am", l10n_util::GetApplicationLocale("am")); |
285 } | 279 } |
286 #endif // defined(OS_WIN) | 280 #endif // defined(OS_WIN) |
287 | 281 |
288 // Clean up. | 282 // Clean up. |
289 PathService::Override(ui::DIR_LOCALES, orig_locale_dir); | 283 PathService::Override(ui::DIR_LOCALES, orig_locale_dir); |
290 file_util::Delete(new_locale_dir, true); | 284 file_util::Delete(new_locale_dir, true); |
291 UErrorCode error_code = U_ZERO_ERROR; | 285 UErrorCode error_code = U_ZERO_ERROR; |
292 icu::Locale::setDefault(locale, error_code); | 286 icu::Locale::setDefault(locale, error_code); |
293 } | 287 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("Latin")); | 392 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("Latin")); |
399 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("German")); | 393 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("German")); |
400 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("pt--BR")); | 394 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("pt--BR")); |
401 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("sl-macedonia")); | 395 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("sl-macedonia")); |
402 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("@")); | 396 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("@")); |
403 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@")); | 397 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@")); |
404 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x")); | 398 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x")); |
405 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x=")); | 399 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@x=")); |
406 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@=y")); | 400 EXPECT_FALSE(l10n_util::IsValidLocaleSyntax("en-US@=y")); |
407 } | 401 } |
OLD | NEW |