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