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