Always include standard name in QLocale::uiLanguages()

Previously, for locales other than the system locale, no entry was
added to the list for the actual locale whose variants - with and
without likely sub-tags - were being appended. In most cases the
standard name will in fact coincide with the variant without likely
sub-tags, so this was unlikely to cause a problem, but it should be
present regardless.

At the same time, turn tst_QLocale::uiLanguages() into a data-driven
test and add another row to its table.

Change-Id: I5cb2d805d78fc3415d82b169caa6154b0f284708
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Edward Welbourne 2021-04-06 13:16:35 +02:00
parent b2871765ce
commit cce3445e70
2 changed files with 49 additions and 38 deletions

View File

@ -4270,6 +4270,9 @@ QStringList QLocale::uiLanguages() const
}
for (int i = locales.size(); i-- > 0; ) {
const QLocale &locale = locales.at(i);
const auto data = locale.d->m_data;
QLocaleId id = data->id();
int j;
QByteArray prior;
if (i < uiLanguages.size()) {
@ -4278,13 +4281,18 @@ QStringList QLocale::uiLanguages() const
prior = uiLanguages.at(i).toLatin1();
// Insert just after the entry we're supplementing:
j = i + 1;
} else if (id.language_id == C) {
// Attempt no likely sub-tag amendments to C:
uiLanguages.append(locale.name());
continue;
} else {
// Plain locale, not system locale; just append.
const QString name = locale.bcp47Name();
uiLanguages.append(name);
prior = name.toLatin1();
j = uiLanguages.size();
}
const auto data = locale.d->m_data;
QLocaleId id = data->id();
const QLocaleId max = id.withLikelySubtagsAdded();
const QLocaleId min = max.withLikelySubtagsRemoved();
id.script_id = 0; // For re-use as script-less variant.

View File

@ -128,6 +128,7 @@ private slots:
void ampm();
void currency();
void quoteString();
void uiLanguages_data();
void uiLanguages();
void weekendDays();
void listPatterns();
@ -2741,47 +2742,49 @@ void tst_QLocale::quoteString()
QCOMPARE(de_CH.quoteString(someText), QString::fromUtf8("\xe2\x80\x9e" "text" "\xe2\x80\x9c"));
QCOMPARE(de_CH.quoteString(someText, QLocale::AlternateQuotation),
QString::fromUtf8("\xe2\x80\x9a" "text" "\xe2\x80\x98"));
}
void tst_QLocale::uiLanguages_data()
{
QTest::addColumn<QLocale>("locale");
QTest::addColumn<QStringList>("all");
QTest::newRow("C") << QLocale::c() << QStringList{QString("C")};
QTest::newRow("en_US")
<< QLocale("en_US")
<< QStringList{QString("en"), QString("en-US"), QString("en-Latn-US")};
QTest::newRow("en_Latn_US")
<< QLocale("en_Latn_US") // Specifying the default script makes no difference
<< QStringList{QString("en"), QString("en-US"), QString("en-Latn-US")};
QTest::newRow("en_GB")
<< QLocale("en_GB")
<< QStringList{QString("en-GB"), QString("en-Latn-GB")};
QTest::newRow("en_Dsrt_US")
<< QLocale("en_Dsrt_US")
<< QStringList{QString("en-Dsrt"), QString("en-Dsrt-US")};
QTest::newRow("ru_RU")
<< QLocale("ru_RU")
<< QStringList{QString("ru"), QString("ru-RU"), QString("ru-Cyrl-RU")};
QTest::newRow("zh_Hant")
<< QLocale("zh_Hant")
<< QStringList{QString("zh-TW"), QString("zh-Hant-TW")};
QTest::newRow("zh_Hans_CN")
<< QLocale(QLocale::Chinese, QLocale::SimplifiedHanScript, QLocale::China)
<< QStringList{QString("zh"), QString("zh-CN"), QString("zh-Hans-CN")};
}
void tst_QLocale::uiLanguages()
{
const QLocale c(QLocale::C);
QCOMPARE(c.uiLanguages().size(), 1);
QCOMPARE(c.uiLanguages().at(0), QLatin1String("C"));
const QLocale en_US("en_US");
QCOMPARE(en_US.uiLanguages().size(), 3);
QCOMPARE(en_US.uiLanguages().at(0), QLatin1String("en"));
QCOMPARE(en_US.uiLanguages().at(1), QLatin1String("en-US"));
QCOMPARE(en_US.uiLanguages().at(2), QLatin1String("en-Latn-US"));
const QLocale en_Latn_US("en_Latn_US");
QCOMPARE(en_Latn_US.uiLanguages().size(), 3);
QCOMPARE(en_Latn_US.uiLanguages().at(0), QLatin1String("en"));
QCOMPARE(en_Latn_US.uiLanguages().at(1), QLatin1String("en-US"));
QCOMPARE(en_Latn_US.uiLanguages().at(2), QLatin1String("en-Latn-US"));
const QLocale en_GB("en_GB");
QCOMPARE(en_GB.uiLanguages().size(), 2);
QCOMPARE(en_GB.uiLanguages().at(0), QLatin1String("en-GB"));
QCOMPARE(en_GB.uiLanguages().at(1), QLatin1String("en-Latn-GB"));
const QLocale en_Dsrt_US("en_Dsrt_US");
QCOMPARE(en_Dsrt_US.uiLanguages().size(), 2);
QCOMPARE(en_Dsrt_US.uiLanguages().at(0), QLatin1String("en-Dsrt"));
QCOMPARE(en_Dsrt_US.uiLanguages().at(1), QLatin1String("en-Dsrt-US"));
const QLocale ru_RU("ru_RU");
QCOMPARE(ru_RU.uiLanguages().size(), 3);
QCOMPARE(ru_RU.uiLanguages().at(0), QLatin1String("ru"));
QCOMPARE(ru_RU.uiLanguages().at(1), QLatin1String("ru-RU"));
QCOMPARE(ru_RU.uiLanguages().at(2), QLatin1String("ru-Cyrl-RU"));
const QLocale zh_Hant("zh_Hant");
QCOMPARE(zh_Hant.uiLanguages().size(), 2);
QCOMPARE(zh_Hant.uiLanguages().at(0), QLatin1String("zh-TW"));
QCOMPARE(zh_Hant.uiLanguages().at(1), QLatin1String("zh-Hant-TW"));
QFETCH(const QLocale, locale);
QFETCH(const QStringList, all);
QCOMPARE(locale.uiLanguages(), all);
}
void tst_QLocale::weekendDays()