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