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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 } | 296 } |
297 locales->push_back(locale_name); | 297 locales->push_back(locale_name); |
298 } | 298 } |
299 | 299 |
300 // Manually add 'es-419' to the list. See the comment in IsDuplicateName(). | 300 // Manually add 'es-419' to the list. See the comment in IsDuplicateName(). |
301 locales->push_back("es-419"); | 301 locales->push_back("es-419"); |
302 return locales; | 302 return locales; |
303 } | 303 } |
304 }; | 304 }; |
305 | 305 |
| 306 // Extract language, country and variant, but ignore keywords. For example, |
| 307 // en-US, ca@valencia, ca-ES@valencia. |
| 308 std::string GetLocaleString(const icu::Locale& locale) { |
| 309 const char* language = locale.getLanguage(); |
| 310 const char* country = locale.getCountry(); |
| 311 const char* variant = locale.getVariant(); |
| 312 |
| 313 std::string result = |
| 314 (language != NULL && *language != '\0') ? language : "und"; |
| 315 |
| 316 if (country != NULL && *country != '\0') { |
| 317 result += '-'; |
| 318 result += country; |
| 319 } |
| 320 |
| 321 if (variant != NULL && *variant != '\0') { |
| 322 std::string variant_str(variant); |
| 323 base::StringToLowerASCII(&variant_str); |
| 324 result += '@' + variant_str; |
| 325 } |
| 326 |
| 327 return result; |
| 328 } |
| 329 |
306 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits> | 330 base::LazyInstance<std::vector<std::string>, AvailableLocalesTraits> |
307 g_available_locales = LAZY_INSTANCE_INITIALIZER; | 331 g_available_locales = LAZY_INSTANCE_INITIALIZER; |
308 | 332 |
309 } // namespace | 333 } // namespace |
310 | 334 |
311 namespace l10n_util { | 335 namespace l10n_util { |
312 | 336 |
| 337 // Convert the ICU default locale to a string. |
| 338 std::string GetConfiguredLocale() { |
| 339 return GetLocaleString(icu::Locale::getDefault()); |
| 340 } |
| 341 |
| 342 // Convert the ICU canonicalized locale to a string. |
| 343 std::string GetCanonicalLocale(const char* locale) { |
| 344 return GetLocaleString(icu::Locale::createCanonical(locale)); |
| 345 } |
| 346 |
313 std::string GetCanonicalLocale(const std::string& locale) { | 347 std::string GetCanonicalLocale(const std::string& locale) { |
314 return base::i18n::GetCanonicalLocale(locale.c_str()); | 348 return GetCanonicalLocale(locale.c_str()); |
315 } | 349 } |
316 | 350 |
317 std::string GetLanguage(const std::string& locale) { | 351 std::string GetLanguage(const std::string& locale) { |
318 const std::string::size_type hyphen_pos = locale.find('-'); | 352 const std::string::size_type hyphen_pos = locale.find('-'); |
319 return std::string(locale, 0, hyphen_pos); | 353 return std::string(locale, 0, hyphen_pos); |
320 } | 354 } |
321 | 355 |
322 bool CheckAndResolveLocale(const std::string& locale, | 356 bool CheckAndResolveLocale(const std::string& locale, |
323 std::string* resolved_locale) { | 357 std::string* resolved_locale) { |
324 #if defined(OS_MACOSX) | 358 #if defined(OS_MACOSX) |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 candidates.push_back(GetCanonicalLocale(pref_locale)); | 471 candidates.push_back(GetCanonicalLocale(pref_locale)); |
438 | 472 |
439 // Next, try the overridden locale. | 473 // Next, try the overridden locale. |
440 const std::vector<std::string>& languages = l10n_util::GetLocaleOverrides(); | 474 const std::vector<std::string>& languages = l10n_util::GetLocaleOverrides(); |
441 if (!languages.empty()) { | 475 if (!languages.empty()) { |
442 candidates.reserve(candidates.size() + languages.size()); | 476 candidates.reserve(candidates.size() + languages.size()); |
443 std::transform(languages.begin(), languages.end(), | 477 std::transform(languages.begin(), languages.end(), |
444 std::back_inserter(candidates), &GetCanonicalLocale); | 478 std::back_inserter(candidates), &GetCanonicalLocale); |
445 } else { | 479 } else { |
446 // If no override was set, defer to ICU | 480 // If no override was set, defer to ICU |
447 candidates.push_back(base::i18n::GetConfiguredLocale()); | 481 candidates.push_back(GetConfiguredLocale()); |
448 } | 482 } |
449 | 483 |
450 #elif defined(OS_ANDROID) | 484 #elif defined(OS_ANDROID) |
451 | 485 |
452 // On Android, query java.util.Locale for the default locale. | 486 // On Android, query java.util.Locale for the default locale. |
453 candidates.push_back(base::android::GetDefaultLocale()); | 487 candidates.push_back(base::android::GetDefaultLocale()); |
454 | 488 |
455 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS) | 489 #elif defined(USE_GLIB) && !defined(OS_CHROMEOS) |
456 | 490 |
457 // GLib implements correct environment variable parsing with | 491 // GLib implements correct environment variable parsing with |
458 // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG. | 492 // the precedence order: LANGUAGE, LC_ALL, LC_MESSAGES and LANG. |
459 // We used to use our custom parsing code along with ICU for this purpose. | 493 // We used to use our custom parsing code along with ICU for this purpose. |
460 // If we have a port that does not depend on GTK, we have to | 494 // If we have a port that does not depend on GTK, we have to |
461 // restore our custom code for that port. | 495 // restore our custom code for that port. |
462 const char* const* languages = g_get_language_names(); | 496 const char* const* languages = g_get_language_names(); |
463 DCHECK(languages); // A valid pointer is guaranteed. | 497 DCHECK(languages); // A valid pointer is guaranteed. |
464 DCHECK(*languages); // At least one entry, "C", is guaranteed. | 498 DCHECK(*languages); // At least one entry, "C", is guaranteed. |
465 | 499 |
466 for (; *languages != NULL; ++languages) { | 500 for (; *languages != NULL; ++languages) { |
467 candidates.push_back(base::i18n::GetCanonicalLocale(*languages)); | 501 candidates.push_back(GetCanonicalLocale(*languages)); |
468 } | 502 } |
469 | 503 |
470 #else | 504 #else |
471 | 505 |
472 // By default, use the application locale preference. This applies to ChromeOS | 506 // By default, use the application locale preference. This applies to ChromeOS |
473 // and linux systems without glib. | 507 // and linux systems without glib. |
474 if (!pref_locale.empty()) | 508 if (!pref_locale.empty()) |
475 candidates.push_back(pref_locale); | 509 candidates.push_back(pref_locale); |
476 | 510 |
477 #endif | 511 #endif |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 | 912 |
879 const char* const* GetAcceptLanguageListForTesting() { | 913 const char* const* GetAcceptLanguageListForTesting() { |
880 return kAcceptLanguageList; | 914 return kAcceptLanguageList; |
881 } | 915 } |
882 | 916 |
883 size_t GetAcceptLanguageListSizeForTesting() { | 917 size_t GetAcceptLanguageListSizeForTesting() { |
884 return arraysize(kAcceptLanguageList); | 918 return arraysize(kAcceptLanguageList); |
885 } | 919 } |
886 | 920 |
887 } // namespace l10n_util | 921 } // namespace l10n_util |
OLD | NEW |