| 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> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/compiler_specific.h" |
| 13 #include "base/file_util.h" | 14 #include "base/file_util.h" |
| 14 #include "base/i18n/file_util_icu.h" | 15 #include "base/i18n/file_util_icu.h" |
| 15 #include "base/i18n/rtl.h" | 16 #include "base/i18n/rtl.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/path_service.h" | 18 #include "base/path_service.h" |
| 18 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 19 #include "base/string_number_conversions.h" | 20 #include "base/string_number_conversions.h" |
| 20 #include "base/string_split.h" | 21 #include "base/string_split.h" |
| 22 #include "base/string_util.h" |
| 21 #include "base/sys_string_conversions.h" | 23 #include "base/sys_string_conversions.h" |
| 22 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
| 23 #include "build/build_config.h" | 25 #include "build/build_config.h" |
| 24 #include "ui/base/l10n/l10n_util_collator.h" | 26 #include "ui/base/l10n/l10n_util_collator.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/base/ui_base_paths.h" | 28 #include "ui/base/ui_base_paths.h" |
| 27 #include "unicode/rbbi.h" | 29 #include "unicode/rbbi.h" |
| 28 #include "unicode/uloc.h" | 30 #include "unicode/uloc.h" |
| 29 | 31 |
| 32 #if defined(OS_ANDROID) |
| 33 #include "base/android/locale_utils.h" |
| 34 #endif |
| 35 |
| 30 #if defined(OS_LINUX) | 36 #if defined(OS_LINUX) |
| 31 #include <glib.h> | 37 #include <glib.h> |
| 32 #endif | 38 #endif |
| 33 | 39 |
| 34 #if defined(OS_WIN) | 40 #if defined(OS_WIN) |
| 35 #include "ui/base/l10n/l10n_util_win.h" | 41 #include "ui/base/l10n/l10n_util_win.h" |
| 36 #endif // OS_WIN | 42 #endif // OS_WIN |
| 37 | 43 |
| 38 namespace { | 44 namespace { |
| 39 | 45 |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 // because they're not accessible with other any API. | 478 // because they're not accessible with other any API. |
| 473 // 4. Patch ICU to special-case zh-Hans/zh-Hant for us. | 479 // 4. Patch ICU to special-case zh-Hans/zh-Hant for us. |
| 474 // #1 and #2 wouldn't work if display_locale != current UI locale although | 480 // #1 and #2 wouldn't work if display_locale != current UI locale although |
| 475 // we can think of additional hack to work around the problem. | 481 // we can think of additional hack to work around the problem. |
| 476 // #3 can be potentially expensive. | 482 // #3 can be potentially expensive. |
| 477 if (locale_code == "zh-CN") | 483 if (locale_code == "zh-CN") |
| 478 locale_code = "zh-Hans"; | 484 locale_code = "zh-Hans"; |
| 479 else if (locale_code == "zh-TW") | 485 else if (locale_code == "zh-TW") |
| 480 locale_code = "zh-Hant"; | 486 locale_code = "zh-Hant"; |
| 481 | 487 |
| 482 UErrorCode error = U_ZERO_ERROR; | 488 string16 display_name; |
| 483 const int kBufferSize = 1024; | 489 #if defined(OS_ANDROID) |
| 490 // Use Java API to get locale display name so that we can remove most of |
| 491 // the lang data from icu data to reduce binary size, except for zh-Hans and |
| 492 // zh-Hant because the current Android Java API doesn't support scripts. |
| 493 // TODO(wangxianzhu): remove the special handling of zh-Hans and zh-Hant once |
| 494 // Android Java API supports scripts. |
| 495 if (!StartsWithASCII(locale_code, "zh-Han", true)) { |
| 496 display_name = base::android::GetDisplayNameForLocale(locale_code, |
| 497 display_locale); |
| 498 } else |
| 499 #endif |
| 500 { |
| 501 UErrorCode error = U_ZERO_ERROR; |
| 502 const int kBufferSize = 1024; |
| 484 | 503 |
| 485 string16 display_name; | 504 int actual_size = uloc_getDisplayName( |
| 486 int actual_size = uloc_getDisplayName(locale_code.c_str(), | 505 locale_code.c_str(), display_locale.c_str(), |
| 487 display_locale.c_str(), | 506 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); |
| 488 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); | 507 DCHECK(U_SUCCESS(error)); |
| 489 DCHECK(U_SUCCESS(error)); | 508 display_name.resize(actual_size); |
| 490 display_name.resize(actual_size); | 509 } |
| 510 |
| 491 // Add an RTL mark so parentheses are properly placed. | 511 // Add an RTL mark so parentheses are properly placed. |
| 492 if (is_for_ui && base::i18n::IsRTL()) | 512 if (is_for_ui && base::i18n::IsRTL()) |
| 493 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); | 513 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); |
| 494 return display_name; | 514 return display_name; |
| 495 } | 515 } |
| 496 | 516 |
| 517 string16 GetDisplayNameForCountry(const std::string& country_code, |
| 518 const std::string& display_locale) { |
| 519 return GetDisplayNameForLocale("_" + country_code, display_locale, false); |
| 520 } |
| 521 |
| 497 std::string NormalizeLocale(const std::string& locale) { | 522 std::string NormalizeLocale(const std::string& locale) { |
| 498 std::string normalized_locale(locale); | 523 std::string normalized_locale(locale); |
| 499 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); | 524 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); |
| 500 | 525 |
| 501 return normalized_locale; | 526 return normalized_locale; |
| 502 } | 527 } |
| 503 | 528 |
| 504 void GetParentLocales(const std::string& current_locale, | 529 void GetParentLocales(const std::string& current_locale, |
| 505 std::vector<std::string>* parent_locales) { | 530 std::vector<std::string>* parent_locales) { |
| 506 std::string locale(NormalizeLocale(current_locale)); | 531 std::string locale(NormalizeLocale(current_locale)); |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { | 845 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { |
| 821 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) | 846 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) |
| 822 // TODO(jungshik) : Put them at the of the list with language codes | 847 // TODO(jungshik) : Put them at the of the list with language codes |
| 823 // enclosed by brackets instead of skipping. | 848 // enclosed by brackets instead of skipping. |
| 824 continue; | 849 continue; |
| 825 locale_codes->push_back(kAcceptLanguageList[i]); | 850 locale_codes->push_back(kAcceptLanguageList[i]); |
| 826 } | 851 } |
| 827 } | 852 } |
| 828 | 853 |
| 829 } // namespace l10n_util | 854 } // namespace l10n_util |
| OLD | NEW |