1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/language/core/common/locale_util.h"
#include <string_view>
#include "base/command_line.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace language {
namespace {
typedef testing::Test LocaleUtilTest;
TEST_F(LocaleUtilTest, SplitIntoMainAndTail) {
typedef std::pair<std::string_view, std::string_view> StringPiecePair;
EXPECT_EQ(StringPiecePair("", ""), SplitIntoMainAndTail(""));
EXPECT_EQ(StringPiecePair("en", ""), SplitIntoMainAndTail("en"));
EXPECT_EQ(StringPiecePair("ogard543i", ""),
SplitIntoMainAndTail("ogard543i"));
EXPECT_EQ(StringPiecePair("en", "-AU"), SplitIntoMainAndTail("en-AU"));
EXPECT_EQ(StringPiecePair("es", "-419"), SplitIntoMainAndTail("es-419"));
EXPECT_EQ(StringPiecePair("en", "-AU-2009"),
SplitIntoMainAndTail("en-AU-2009"));
}
TEST_F(LocaleUtilTest, ConvertToActualUILocale) {
std::string locale;
//---------------------------------------------------------------------------
// Languages that are enabled as display UI.
//---------------------------------------------------------------------------
locale = "en-US";
bool is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("en-US", locale);
locale = "it";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("it", locale);
locale = "fr-FR";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("fr", locale);
//---------------------------------------------------------------------------
// Languages that are converted to their fallback version.
//---------------------------------------------------------------------------
// All Latin American Spanish languages fall back to "es-419".
for (const char* es_locale : {"es-AR", "es-CL", "es-CO", "es-CR", "es-HN",
"es-MX", "es-PE", "es-US", "es-UY", "es-VE"}) {
locale = es_locale;
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui) << es_locale;
#if BUILDFLAG(IS_IOS)
// iOS uses a different name for es-419 (es-MX).
EXPECT_EQ("es-MX", locale) << es_locale;
#else
EXPECT_EQ("es-419", locale) << es_locale;
#endif
}
// English falls back to US.
locale = "en";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
#if BUILDFLAG(IS_APPLE)
// On Apple platforms, "en" is used instead of "en-US".
EXPECT_EQ("en", locale);
#else
EXPECT_EQ("en-US", locale);
#endif
// All other regional English languages fall back to UK.
for (const char* en_locale :
{"en-AU", "en-CA", "en-GB-oxendict", "en-IN", "en-NZ", "en-ZA"}) {
locale = en_locale;
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui) << en_locale;
EXPECT_EQ("en-GB", locale) << en_locale;
}
locale = "pt";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
#if BUILDFLAG(IS_IOS)
// On iOS, "pt" is used instead of "pt-BR".
EXPECT_EQ("pt", locale);
#else
EXPECT_EQ("pt-BR", locale);
#endif
locale = "it-CH";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("it", locale);
locale = "no";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("nb", locale);
locale = "it-IT";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("it", locale);
locale = "de-DE";
is_ui = ConvertToActualUILocale(&locale);
EXPECT_TRUE(is_ui);
EXPECT_EQ("de", locale);
//---------------------------------------------------------------------------
// Languages that cannot be used as display UI.
//---------------------------------------------------------------------------
// This only matters for ChromeOS and Windows, as they are the only systems
// where users can set the display UI.
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
locale = "sd"; // Sindhi
is_ui = ConvertToActualUILocale(&locale);
EXPECT_FALSE(is_ui);
locale = "ga"; // Irish
is_ui = ConvertToActualUILocale(&locale);
EXPECT_FALSE(is_ui);
locale = "ky"; // Kyrgyz
is_ui = ConvertToActualUILocale(&locale);
EXPECT_FALSE(is_ui);
#endif
}
} // namespace
} // namespace language
|