OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/base/l10n/l10n_util.h" | 5 #include "ui/base/l10n/l10n_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstdlib> | 8 #include <cstdlib> |
9 #include <iterator> | 9 #include <iterator> |
10 #include <string> | 10 #include <string> |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 g_available_locales = LAZY_INSTANCE_INITIALIZER; | 304 g_available_locales = LAZY_INSTANCE_INITIALIZER; |
305 | 305 |
306 } // namespace | 306 } // namespace |
307 | 307 |
308 namespace l10n_util { | 308 namespace l10n_util { |
309 | 309 |
310 std::string GetCanonicalLocale(const std::string& locale) { | 310 std::string GetCanonicalLocale(const std::string& locale) { |
311 return base::i18n::GetCanonicalLocale(locale.c_str()); | 311 return base::i18n::GetCanonicalLocale(locale.c_str()); |
312 } | 312 } |
313 | 313 |
| 314 std::string GetLanguage(const std::string& locale) { |
| 315 const std::string::size_type hyphen_pos = locale.find('-'); |
| 316 return std::string(locale, 0, hyphen_pos); |
| 317 } |
| 318 |
314 bool CheckAndResolveLocale(const std::string& locale, | 319 bool CheckAndResolveLocale(const std::string& locale, |
315 std::string* resolved_locale) { | 320 std::string* resolved_locale) { |
316 #if defined(OS_MACOSX) | 321 #if defined(OS_MACOSX) |
317 NOTIMPLEMENTED(); | 322 NOTIMPLEMENTED(); |
318 return false; | 323 return false; |
319 #else | 324 #else |
320 if (IsLocaleAvailable(locale)) { | 325 if (IsLocaleAvailable(locale)) { |
321 *resolved_locale = locale; | 326 *resolved_locale = locale; |
322 return true; | 327 return true; |
323 } | 328 } |
324 | 329 |
325 // If there's a variant, skip over it so we can try without the region | 330 // If there's a variant, skip over it so we can try without the region |
326 // code. For example, ca_ES@valencia should cause us to try ca@valencia | 331 // code. For example, ca_ES@valencia should cause us to try ca@valencia |
327 // before ca. | 332 // before ca. |
328 std::string::size_type variant_pos = locale.find('@'); | 333 std::string::size_type variant_pos = locale.find('@'); |
329 if (variant_pos != std::string::npos) | 334 if (variant_pos != std::string::npos) |
330 return false; | 335 return false; |
331 | 336 |
332 // If the locale matches language but not country, use that instead. | 337 // If the locale matches language but not country, use that instead. |
333 // TODO(jungshik) : Nothing is done about languages that Chrome | 338 // TODO(jungshik) : Nothing is done about languages that Chrome |
334 // does not support but available on Windows. We fall | 339 // does not support but available on Windows. We fall |
335 // back to en-US in GetApplicationLocale so that it's a not critical, | 340 // back to en-US in GetApplicationLocale so that it's a not critical, |
336 // but we can do better. | 341 // but we can do better. |
337 std::string::size_type hyphen_pos = locale.find('-'); | 342 const std::string lang(GetLanguage(locale)); |
338 std::string lang(locale, 0, hyphen_pos); | 343 if (lang.size() < locale.size()) { |
339 if (hyphen_pos != std::string::npos && hyphen_pos > 0) { | 344 std::string region(locale, lang.size() + 1); |
340 std::string region(locale, hyphen_pos + 1); | |
341 std::string tmp_locale(lang); | 345 std::string tmp_locale(lang); |
342 // Map es-RR other than es-ES to es-419 (Chrome's Latin American | 346 // Map es-RR other than es-ES to es-419 (Chrome's Latin American |
343 // Spanish locale). | 347 // Spanish locale). |
344 if (LowerCaseEqualsASCII(lang, "es") && | 348 if (LowerCaseEqualsASCII(lang, "es") && |
345 !LowerCaseEqualsASCII(region, "es")) { | 349 !LowerCaseEqualsASCII(region, "es")) { |
346 tmp_locale.append("-419"); | 350 tmp_locale.append("-419"); |
347 } else if (LowerCaseEqualsASCII(lang, "zh")) { | 351 } else if (LowerCaseEqualsASCII(lang, "zh")) { |
348 // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN. | 352 // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN. |
349 if (LowerCaseEqualsASCII(region, "hk") || | 353 if (LowerCaseEqualsASCII(region, "hk") || |
350 LowerCaseEqualsASCII(region, "mo")) { // Macao | 354 LowerCaseEqualsASCII(region, "mo")) { // Macao |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
870 } | 874 } |
871 } | 875 } |
872 | 876 |
873 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { | 877 int GetLocalizedContentsWidthInPixels(int pixel_resource_id) { |
874 int width = 0; | 878 int width = 0; |
875 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); | 879 base::StringToInt(l10n_util::GetStringUTF8(pixel_resource_id), &width); |
876 DCHECK_GT(width, 0); | 880 DCHECK_GT(width, 0); |
877 return width; | 881 return width; |
878 } | 882 } |
879 | 883 |
| 884 const char* const* GetAcceptLanguageListForTesting() { |
| 885 return kAcceptLanguageList; |
| 886 } |
| 887 |
| 888 size_t GetAcceptLanguageListSizeForTesting() { |
| 889 return arraysize(kAcceptLanguageList); |
| 890 } |
| 891 |
880 } // namespace l10n_util | 892 } // namespace l10n_util |
OLD | NEW |