Chromium Code Reviews| 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" |
| 21 #include "base/sys_string_conversions.h" | 22 #include "base/sys_string_conversions.h" |
| 22 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
| 23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 24 #include "ui/base/l10n/l10n_util_collator.h" | 25 #include "ui/base/l10n/l10n_util_collator.h" |
| 25 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
| 26 #include "ui/base/ui_base_paths.h" | 27 #include "ui/base/ui_base_paths.h" |
| 27 #include "unicode/rbbi.h" | 28 #include "unicode/rbbi.h" |
| 28 #include "unicode/uloc.h" | 29 #include "unicode/uloc.h" |
| 29 | 30 |
| 31 #if defined(OS_ANDROID) | |
| 32 #include "base/android/locale_utils.h" | |
| 33 #endif | |
| 34 | |
| 30 #if defined(OS_LINUX) | 35 #if defined(OS_LINUX) |
| 31 #include <glib.h> | 36 #include <glib.h> |
| 32 #endif | 37 #endif |
| 33 | 38 |
| 34 #if defined(OS_WIN) | 39 #if defined(OS_WIN) |
| 35 #include "ui/base/l10n/l10n_util_win.h" | 40 #include "ui/base/l10n/l10n_util_win.h" |
| 36 #endif // OS_WIN | 41 #endif // OS_WIN |
| 37 | 42 |
| 38 namespace { | 43 namespace { |
| 39 | 44 |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 // the current results : Chinese (China) / Chinese (Taiwan). | 472 // the current results : Chinese (China) / Chinese (Taiwan). |
| 468 // TODO(jungshik): Do one of the following: | 473 // TODO(jungshik): Do one of the following: |
| 469 // 1. Special-case Chinese by getting the custom-translation for them | 474 // 1. Special-case Chinese by getting the custom-translation for them |
| 470 // 2. Recycle IDS_ENCODING_{SIMP,TRAD}_CHINESE. | 475 // 2. Recycle IDS_ENCODING_{SIMP,TRAD}_CHINESE. |
| 471 // 3. Get translations for two directly from the ICU resouce bundle | 476 // 3. Get translations for two directly from the ICU resouce bundle |
| 472 // because they're not accessible with other any API. | 477 // because they're not accessible with other any API. |
| 473 // 4. Patch ICU to special-case zh-Hans/zh-Hant for us. | 478 // 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 | 479 // #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. | 480 // we can think of additional hack to work around the problem. |
| 476 // #3 can be potentially expensive. | 481 // #3 can be potentially expensive. |
| 477 if (locale_code == "zh-CN") | 482 bool is_zh ALLOW_UNUSED = false; |
| 483 if (locale_code == "zh-CN") { | |
| 478 locale_code = "zh-Hans"; | 484 locale_code = "zh-Hans"; |
| 479 else if (locale_code == "zh-TW") | 485 is_zh = true; |
| 486 } else if (locale_code == "zh-TW") { | |
| 480 locale_code = "zh-Hant"; | 487 locale_code = "zh-Hant"; |
| 481 | 488 is_zh = true; |
| 482 UErrorCode error = U_ZERO_ERROR; | 489 } |
| 483 const int kBufferSize = 1024; | |
| 484 | 490 |
| 485 string16 display_name; | 491 string16 display_name; |
| 486 int actual_size = uloc_getDisplayName(locale_code.c_str(), | 492 #if defined(OS_ANDROID) |
| 487 display_locale.c_str(), | 493 // Use Java API to get locale display name so that we can remove most of |
| 488 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); | 494 // the lang data from icu data to reduce binary size, except for zh-Hans and |
| 489 DCHECK(U_SUCCESS(error)); | 495 // zh-Hant because the current Android Java API doesn't support scripts. |
| 490 display_name.resize(actual_size); | 496 // TODO(wangxianzhu): remove the special handling of zh-Hans and zh-Hant once |
| 497 // Android Java API supports scripts. | |
| 498 if (!is_zh) { | |
|
Ilya Sherman
2012/05/05 00:42:03
nit: Would it be equivalent to check that the loca
Xianzhu
2012/05/05 01:03:53
Changed to use StartsWithASCII(locale_code, "zh-Ha
| |
| 499 display_name = base::android::GetDisplayNameForLocale(locale_code, | |
| 500 display_locale); | |
| 501 } else | |
| 502 #endif | |
| 503 { | |
| 504 UErrorCode error = U_ZERO_ERROR; | |
| 505 const int kBufferSize = 1024; | |
| 506 | |
| 507 int actual_size = uloc_getDisplayName(locale_code.c_str(), | |
| 508 display_locale.c_str(), | |
| 509 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); | |
| 510 DCHECK(U_SUCCESS(error)); | |
| 511 display_name.resize(actual_size); | |
| 512 } | |
| 513 | |
| 491 // Add an RTL mark so parentheses are properly placed. | 514 // Add an RTL mark so parentheses are properly placed. |
| 492 if (is_for_ui && base::i18n::IsRTL()) | 515 if (is_for_ui && base::i18n::IsRTL()) |
| 493 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); | 516 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); |
| 494 return display_name; | 517 return display_name; |
| 495 } | 518 } |
| 496 | 519 |
| 520 string16 GetDisplayNameForCountry(const std::string& country_code, | |
| 521 const std::string& display_locale) { | |
| 522 return GetDisplayNameForLocale("_" + country_code, display_locale, false); | |
| 523 } | |
| 524 | |
| 497 std::string NormalizeLocale(const std::string& locale) { | 525 std::string NormalizeLocale(const std::string& locale) { |
| 498 std::string normalized_locale(locale); | 526 std::string normalized_locale(locale); |
| 499 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); | 527 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); |
| 500 | 528 |
| 501 return normalized_locale; | 529 return normalized_locale; |
| 502 } | 530 } |
| 503 | 531 |
| 504 void GetParentLocales(const std::string& current_locale, | 532 void GetParentLocales(const std::string& current_locale, |
| 505 std::vector<std::string>* parent_locales) { | 533 std::vector<std::string>* parent_locales) { |
| 506 std::string locale(NormalizeLocale(current_locale)); | 534 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) { | 848 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { |
| 821 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) | 849 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) |
| 822 // TODO(jungshik) : Put them at the of the list with language codes | 850 // TODO(jungshik) : Put them at the of the list with language codes |
| 823 // enclosed by brackets instead of skipping. | 851 // enclosed by brackets instead of skipping. |
| 824 continue; | 852 continue; |
| 825 locale_codes->push_back(kAcceptLanguageList[i]); | 853 locale_codes->push_back(kAcceptLanguageList[i]); |
| 826 } | 854 } |
| 827 } | 855 } |
| 828 | 856 |
| 829 } // namespace l10n_util | 857 } // namespace l10n_util |
| OLD | NEW |