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 Java API doesn't support scripts. |
490 display_name.resize(actual_size); | 495 if (!is_zh) { |
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; | |
503 | |
504 string16 display_name; | |
505 int actual_size = uloc_getDisplayName(locale_code.c_str(), | |
506 display_locale.c_str(), | |
507 WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); | |
508 DCHECK(U_SUCCESS(error)); | |
509 display_name.resize(actual_size); | |
510 } | |
511 | |
491 // Add an RTL mark so parentheses are properly placed. | 512 // Add an RTL mark so parentheses are properly placed. |
492 if (is_for_ui && base::i18n::IsRTL()) | 513 if (is_for_ui && base::i18n::IsRTL()) |
493 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); | 514 display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); |
494 return display_name; | 515 return display_name; |
495 } | 516 } |
496 | 517 |
518 string16 GetDisplayNameForCountry(const std::string& country_code, | |
519 const std::string& display_locale) { | |
520 return GetDisplayNameForLocale("_" + country_code, display_locale, false); | |
jungshik at Google
2012/05/04 17:46:03
I would use "und_" (for unknown language) instead
wangxianzhu
2012/05/04 18:13:42
Just tried using "und_", but the returned display
jungshik at Google
2012/05/04 18:30:44
Thanks for trying out and sorry for wasting your t
| |
521 } | |
522 | |
497 std::string NormalizeLocale(const std::string& locale) { | 523 std::string NormalizeLocale(const std::string& locale) { |
498 std::string normalized_locale(locale); | 524 std::string normalized_locale(locale); |
499 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); | 525 std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); |
500 | 526 |
501 return normalized_locale; | 527 return normalized_locale; |
502 } | 528 } |
503 | 529 |
504 void GetParentLocales(const std::string& current_locale, | 530 void GetParentLocales(const std::string& current_locale, |
505 std::vector<std::string>* parent_locales) { | 531 std::vector<std::string>* parent_locales) { |
506 std::string locale(NormalizeLocale(current_locale)); | 532 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) { | 846 for (size_t i = 0; i < arraysize(kAcceptLanguageList); ++i) { |
821 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) | 847 if (!IsLocaleNameTranslated(kAcceptLanguageList[i], display_locale)) |
822 // TODO(jungshik) : Put them at the of the list with language codes | 848 // TODO(jungshik) : Put them at the of the list with language codes |
823 // enclosed by brackets instead of skipping. | 849 // enclosed by brackets instead of skipping. |
824 continue; | 850 continue; |
825 locale_codes->push_back(kAcceptLanguageList[i]); | 851 locale_codes->push_back(kAcceptLanguageList[i]); |
826 } | 852 } |
827 } | 853 } |
828 | 854 |
829 } // namespace l10n_util | 855 } // namespace l10n_util |
OLD | NEW |