From 91e70f239e166956c0db2d99cfb229c6b7d94598 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 27 Jul 2023 15:02:56 +0200 Subject: Give QLocale::uiLanguages() a separator parameter It has always returned dash-joined forms of the locale names, and callers who need an underscore-joined form have been obliged to replace('-', '_') before using them. Given that everything it adds to the list comes from QLocaleId methods that accept a separator, it's trivial to let it offer the same choice to its callers and save them this hassle. Amended code in QTranslater and QMimeType to save them that hassle. [ChangeLog][CoreLib][QLocale] QLocale::uiLanguages() now lets the caller choose what separator to use between the tags that make up each locale-identifier in the list returned. Change-Id: I91fcd0b988d9a64e0e9ad9e851f6cb8c1be8ae50 Reviewed-by: Marc Mutz Reviewed-by: Thiago Macieira --- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 50 ++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) (limited to 'tests/auto/corelib/text/qlocale') diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index c7017b8511..78f3ce6359 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -3602,11 +3602,51 @@ void tst_QLocale::uiLanguages() // Compare mySystemLocale(), which tests the same for a custom system locale. QFETCH(const QLocale, locale); QFETCH(const QStringList, all); - auto reporter = qScopeGuard([&locale]() { - qDebug("\n\t%s", qPrintable(locale.uiLanguages().join(u"\n\t"))); - }); - QCOMPARE(locale.uiLanguages(), all); - reporter.dismiss(); + const auto expected = [all](QChar sep) { + QStringList adjusted; + for (QString name : all) + adjusted << name.replace(u'-', sep); + return adjusted; + }; + + { + // By default tags are joined with a dash: + const QStringList actual = locale.uiLanguages(); + auto reporter = qScopeGuard([&actual]() { + qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1))); + }); + QCOMPARE(actual, all); + reporter.dismiss(); + } + { + // We also support joining with an underscore: + const QStringList actual = locale.uiLanguages(QLocale::TagSeparator::Underscore); + auto reporter = qScopeGuard([&actual]() { + qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1))); + }); + QCOMPARE(actual, expected(u'_')); + reporter.dismiss(); + } + { + // Or, in fact, any ASCII character: + const QStringList actual = locale.uiLanguages(QLocale::TagSeparator{'|'}); + auto reporter = qScopeGuard([&actual]() { + qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1))); + }); + QCOMPARE(actual, expected(u'|')); + reporter.dismiss(); + } + { + // Non-ASCII separator (here, y-umlaut) is unsupported. + QTest::ignoreMessage(QtWarningMsg, "QLocale::uiLanguages(): " + "Using non-ASCII separator '\u00ff' (ff) is unsupported"); + const QStringList actual = locale.uiLanguages(QLocale::TagSeparator{'\xff'}); + auto reporter = qScopeGuard([&actual]() { + qDebug("\n\t%ls", qUtf16Printable(actual.join("\n\t"_L1))); + }); + QCOMPARE(actual, QStringList{}); + reporter.dismiss(); + } } void tst_QLocale::weekendDays() -- cgit v1.2.3