diff options
Diffstat (limited to 'ui/base/l10n')
-rw-r--r-- | ui/base/l10n/l10n_util.cc | 47 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util.h | 6 | ||||
-rw-r--r-- | ui/base/l10n/l10n_util_unittest.cc | 21 |
3 files changed, 61 insertions, 13 deletions
diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc index 6cf4608..f6c1b32 100644 --- a/ui/base/l10n/l10n_util.cc +++ b/ui/base/l10n/l10n_util.cc @@ -27,6 +27,10 @@ #include "unicode/rbbi.h" #include "unicode/uloc.h" +#if defined(OS_ANDROID) +#include "base/android/locale_utils.h" +#endif + #if defined(OS_LINUX) #include <glib.h> #endif @@ -474,26 +478,49 @@ string16 GetDisplayNameForLocale(const std::string& locale, // #1 and #2 wouldn't work if display_locale != current UI locale although // we can think of additional hack to work around the problem. // #3 can be potentially expensive. - if (locale_code == "zh-CN") + bool is_zh = false; + if (locale_code == "zh-CN") { locale_code = "zh-Hans"; - else if (locale_code == "zh-TW") + is_zh = true; + } else if (locale_code == "zh-TW") { locale_code = "zh-Hant"; - - UErrorCode error = U_ZERO_ERROR; - const int kBufferSize = 1024; + is_zh = true; + } string16 display_name; - int actual_size = uloc_getDisplayName(locale_code.c_str(), - display_locale.c_str(), - WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); - DCHECK(U_SUCCESS(error)); - display_name.resize(actual_size); +#if defined(OS_ANDROID) + // Use Java API to get locale display name so that we can remove most of + // the lang data from icu data to reduce binary size, except for zh-Hans and + // zh-Hant because the current Android Java API doesn't support scripts. + // TODO(wangxianzhu): remove the special handling of zh-Hans and zh-Hant once + // Android Java API supports scripts. + if (!is_zh) { + display_name = base::android::GetDisplayNameForLocale(locale_code, + display_locale); + } else +#endif + { + UErrorCode error = U_ZERO_ERROR; + const int kBufferSize = 1024; + + int actual_size = uloc_getDisplayName(locale_code.c_str(), + display_locale.c_str(), + WriteInto(&display_name, kBufferSize), kBufferSize - 1, &error); + DCHECK(U_SUCCESS(error)); + display_name.resize(actual_size); + } + // Add an RTL mark so parentheses are properly placed. if (is_for_ui && base::i18n::IsRTL()) display_name.push_back(static_cast<char16>(base::i18n::kRightToLeftMark)); return display_name; } +string16 GetDisplayNameForCountry(const std::string& country_code, + const std::string& display_locale) { + return GetDisplayNameForLocale("_" + country_code, display_locale, false); +} + std::string NormalizeLocale(const std::string& locale) { std::string normalized_locale(locale); std::replace(normalized_locale.begin(), normalized_locale.end(), '-', '_'); diff --git a/ui/base/l10n/l10n_util.h b/ui/base/l10n/l10n_util.h index 48ec68a..2eae6de 100644 --- a/ui/base/l10n/l10n_util.h +++ b/ui/base/l10n/l10n_util.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -51,6 +51,10 @@ UI_EXPORT string16 GetDisplayNameForLocale(const std::string& locale, const std::string& display_locale, bool is_for_ui); +// Returns the display name of the |country_code| in |display_locale|. +UI_EXPORT string16 GetDisplayNameForCountry(const std::string& country_code, + const std::string& display_locale); + // Converts all - into _, to be consistent with ICU and file system names. UI_EXPORT std::string NormalizeLocale(const std::string& locale); diff --git a/ui/base/l10n/l10n_util_unittest.cc b/ui/base/l10n/l10n_util_unittest.cc index 971d7ce..a9b51fd 100644 --- a/ui/base/l10n/l10n_util_unittest.cc +++ b/ui/base/l10n/l10n_util_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -303,7 +303,7 @@ TEST_F(L10nUtilTest, SortStringsUsingFunction) { STLDeleteElements(&strings); } -TEST_F(L10nUtilTest, LocaleDisplayName) { +TEST_F(L10nUtilTest, GetDisplayNameForLocale) { // TODO(jungshik): Make this test more extensive. // Test zh-CN and zh-TW are treated as zh-Hans and zh-Hant. string16 result = l10n_util::GetDisplayNameForLocale("zh-CN", "en", false); @@ -318,6 +318,12 @@ TEST_F(L10nUtilTest, LocaleDisplayName) { result = l10n_util::GetDisplayNameForLocale("es-419", "en", false); EXPECT_EQ(ASCIIToUTF16("Spanish (Latin America)"), result); + result = l10n_util::GetDisplayNameForLocale("-BR", "en", false); + EXPECT_EQ(ASCIIToUTF16("Brazil"), result); + + result = l10n_util::GetDisplayNameForLocale("xyz-xyz", "en", false); + EXPECT_EQ(ASCIIToUTF16("xyz (XYZ)"), result); + // ToUpper and ToLower should work with embedded NULLs. const size_t length_with_null = 4; char16 buf_with_null[length_with_null] = { 0, 'a', 0, 'b' }; @@ -334,6 +340,17 @@ TEST_F(L10nUtilTest, LocaleDisplayName) { lower_with_null[2] == 0 && lower_with_null[3] == 'b'); } +TEST_F(L10nUtilTest, GetDisplayNameForCountry) { + string16 result = l10n_util::GetDisplayNameForCountry("BR", "en"); + EXPECT_EQ(ASCIIToUTF16("Brazil"), result); + + result = l10n_util::GetDisplayNameForCountry("419", "en"); + EXPECT_EQ(ASCIIToUTF16("Latin America"), result); + + result = l10n_util::GetDisplayNameForCountry("xyz", "en"); + EXPECT_EQ(ASCIIToUTF16("XYZ"), result); +} + TEST_F(L10nUtilTest, GetParentLocales) { std::vector<std::string> locales; const std::string top_locale("sr_Cyrl_RS"); |