From f2865c1e1ba1a7e3800c9cea4caaab8a3850ade3 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 7 Mar 2017 20:44:16 -0500 Subject: [PATCH] When resolving the locale, use pt-PT for unsupported pt-** This also maps "pt" -> "pt-BR", and "zh"->"zh-CN". Chrome has two .pak files for Portuguese: pt-PT and pt-BR. This is different from most locales in that there is no plain "pt.pak", which would be selected by normal locale resolving logic. The other exceptions are zh and en, both of which already have special-case fallback logic (with the exception of a bare "zh", which this CL addresses). This change adds equivalent fallback logic for pt-PT. BUG=691594 Review-Url: https://codereview.chromium.org/2733303002 Cr-Commit-Position: refs/heads/master@{#455326} (cherry picked from commit 6f16f9098ad2c1da54ac2ca6ea3b203c177b6119) Review-Url: https://codereview.chromium.org/2737553006 . Cr-Commit-Position: refs/branch-heads/3029@{#57} Cr-Branched-From: 939b32ee5ba05c396eef3fd992822fcca9a2e262-refs/heads/master@{#454471} --- ui/base/l10n/l10n_util.cc | 11 ++++++---- ui/base/l10n/l10n_util_unittest.cc | 35 +++++++++++++++++++----------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/ui/base/l10n/l10n_util.cc b/ui/base/l10n/l10n_util.cc index ad0ad0ed9d12..440ab1a04d4d 100644 --- a/ui/base/l10n/l10n_util.cc +++ b/ui/base/l10n/l10n_util.cc @@ -357,6 +357,10 @@ bool CheckAndResolveLocale(const std::string& locale, if (base::LowerCaseEqualsASCII(lang, "es") && !base::LowerCaseEqualsASCII(region, "es")) { tmp_locale.append("-419"); + } else if (base::LowerCaseEqualsASCII(lang, "pt")) { + // Map pt-RR other than pt-BR to pt-PT. Note that "pt" by itself maps to + // pt-BR (logic below). + tmp_locale.append("-PT"); } else if (base::LowerCaseEqualsASCII(lang, "zh")) { // Map zh-HK and zh-MO to zh-TW. Otherwise, zh-FOO is mapped to zh-CN. if (base::LowerCaseEqualsASCII(region, "hk") || @@ -387,14 +391,13 @@ bool CheckAndResolveLocale(const std::string& locale, } // Google updater uses no, tl, iw and en for our nb, fil, he, and en-US. + // Note that pt-RR is mapped to pt-PT above, but we want pt -> pt-BR here. struct { const char* source; const char* dest; } alias_map[] = { - {"no", "nb"}, - {"tl", "fil"}, - {"iw", "he"}, - {"en", "en-US"}, + {"en", "en-US"}, {"iw", "he"}, {"no", "nb"}, + {"pt", "pt-BR"}, {"tl", "fil"}, {"zh", "zh-CN"}, }; for (const auto& alias : alias_map) { if (base::LowerCaseEqualsASCII(lang, alias.source)) { diff --git a/ui/base/l10n/l10n_util_unittest.cc b/ui/base/l10n/l10n_util_unittest.cc index ab88a40ccb37..ee81f036888f 100644 --- a/ui/base/l10n/l10n_util_unittest.cc +++ b/ui/base/l10n/l10n_util_unittest.cc @@ -113,19 +113,8 @@ TEST_F(L10nUtilTest, GetAppLocale) { ASSERT_TRUE(PathService::Get(ui::DIR_LOCALES, &new_locale_dir)); // Make fake locale files. std::string filenames[] = { - "en-US", - "en-GB", - "fr", - "es-419", - "es", - "zh-TW", - "zh-CN", - "he", - "fil", - "nb", - "am", - "ca", - "ca@valencia", + "am", "ca", "ca@valencia", "en-GB", "en-US", "es", "es-419", "fil", + "fr", "he", "nb", "pt-BR", "pt-PT", "zh-CN", "zh-TW", }; for (size_t i = 0; i < arraysize(filenames); ++i) { @@ -267,6 +256,22 @@ TEST_F(L10nUtilTest, GetAppLocale) { EXPECT_EQ("es", l10n_util::GetApplicationLocale(std::string())); EXPECT_STREQ("es", icu::Locale::getDefault().getLanguage()); + SetDefaultLocaleForTest("pt-PT", env.get()); + EXPECT_EQ("pt-PT", l10n_util::GetApplicationLocale(std::string())); + EXPECT_STREQ("pt", icu::Locale::getDefault().getLanguage()); + + SetDefaultLocaleForTest("pt-BR", env.get()); + EXPECT_EQ("pt-BR", l10n_util::GetApplicationLocale(std::string())); + EXPECT_STREQ("pt", icu::Locale::getDefault().getLanguage()); + + SetDefaultLocaleForTest("pt-AO", env.get()); + EXPECT_EQ("pt-PT", l10n_util::GetApplicationLocale(std::string())); + EXPECT_STREQ("pt", icu::Locale::getDefault().getLanguage()); + + SetDefaultLocaleForTest("pt", env.get()); + EXPECT_EQ("pt-BR", l10n_util::GetApplicationLocale(std::string())); + EXPECT_STREQ("pt", icu::Locale::getDefault().getLanguage()); + SetDefaultLocaleForTest("zh-HK", env.get()); EXPECT_EQ("zh-TW", l10n_util::GetApplicationLocale(std::string())); EXPECT_STREQ("zh", icu::Locale::getDefault().getLanguage()); @@ -279,6 +284,10 @@ TEST_F(L10nUtilTest, GetAppLocale) { EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string())); EXPECT_STREQ("zh", icu::Locale::getDefault().getLanguage()); + SetDefaultLocaleForTest("zh", env.get()); + EXPECT_EQ("zh-CN", l10n_util::GetApplicationLocale(std::string())); + EXPECT_STREQ("zh", icu::Locale::getDefault().getLanguage()); + SetDefaultLocaleForTest("en-CA", env.get()); EXPECT_EQ("en-GB", l10n_util::GetApplicationLocale(std::string())); EXPECT_STREQ("en", icu::Locale::getDefault().getLanguage());