From f1465c621c11712991fabe5b8b81ccbb6eea4bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20K=C3=B6hne?= Date: Fri, 11 Dec 2020 16:40:37 +0100 Subject: QLocale: Allow direct conversion to language, country, and script codes Currently the codes are only exposed in aggregated form, i.e. through name(), bcp47Name(). There are use cases though where you are only interested in either language, country, or script codes. One example is in Qt Linguist. This patch therefore exposes the static languageToCode(), countryToCode(), scriptToCode() methods that were so far only available in the private API also in the public API. [ChangeLog][QtCore][QLocale] Added static languageToCode(), countryToCode() scriptToCode() methods that convert enum values to the respective ISO code strings. Fixes: QTBUG-39542 Fixes: QTBUG-64942 Change-Id: Ib1d5c3293e2f53245ba4c1fc8159275bcb290080 Reviewed-by: Edward Welbourne --- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 327cb7d714..04c54b903b 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -150,6 +150,8 @@ private slots: void numberGroupingIndia(); void numberFormatChakma(); + void lcsToCode(); + // *** ORDER-DEPENDENCY *** (This Is Bad.) // Test order is determined by order of declaration here: *all* tests that // QLocale::setDefault() *must* appear *after* all other tests ! @@ -3224,5 +3226,19 @@ void tst_QLocale::numberFormatChakma() QCOMPARE(chakma.toULongLong(strResult64), uint64); } +void tst_QLocale::lcsToCode() +{ + QCOMPARE(QLocale::languageToCode(QLocale::AnyLanguage), QString()); + QCOMPARE(QLocale::languageToCode(QLocale::C), QString("C")); + QCOMPARE(QLocale::languageToCode(QLocale::English), QString("en")); + + QCOMPARE(QLocale::countryToCode(QLocale::AnyCountry), QString()); + QCOMPARE(QLocale::countryToCode(QLocale::UnitedStates), QString("US")); + QCOMPARE(QLocale::countryToCode(QLocale::EuropeanUnion), QString("EU")); + + QCOMPARE(QLocale::scriptToCode(QLocale::AnyScript), QString()); + QCOMPARE(QLocale::scriptToCode(QLocale::SimplifiedHanScript), QString("Hans")); +} + QTEST_MAIN(tst_QLocale) #include "tst_qlocale.moc" -- cgit v1.2.3