From e323d46cdaecffebb3f9fa55934e4eb4868611cf Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 20 Feb 2025 11:02:22 +0100 Subject: [PATCH] Distinguish system locale from corresponding CLDR-derived one Previously it was displayed as if it had been constructed from its language, script and territory; but it is distinct from the locale constructed in this way and may produce different results. Report the system locale as QLocale::system() with its language, script and country within /*...*/ following it, instead of as QLocale(...) with these as its parameters. Add a test of the debug output, verifying that it has the form intended. [ChangeLog][Important Behavior Changes][QLocale] Message logging now distinguishes the system locale from the corresponding locale - generated from its language, script and territory - based on CLDR data. Pick-to: 6.9 Fixes: QTBUG-133922 Change-Id: Ic6fb137821fb7bf29d0f6446a46225cadd54b82a Reviewed-by: Mate Barany --- src/corelib/text/qlocale.cpp | 8 +++-- .../auto/corelib/text/qlocale/tst_qlocale.cpp | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index e7fa6891af3..ef1c9fe5a9a 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -5437,10 +5437,12 @@ QString QLocale::nativeCountryName() const QDebug operator<<(QDebug dbg, const QLocale &l) { QDebugStateSaver saver(dbg); + const bool isSys = l == QLocale::system(); dbg.nospace().noquote() - << "QLocale(" << QLocale::languageToString(l.language()) - << ", " << QLocale::scriptToString(l.script()) - << ", " << QLocale::territoryToString(l.territory()) << ')'; + << (isSys ? "QLocale::system()/* " : "QLocale(") + << QLocale::languageToString(l.language()) << ", " + << QLocale::scriptToString(l.script()) << ", " + << QLocale::territoryToString(l.territory()) << (isSys ? " */" : ")"); return dbg; } #endif diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 2302cda4830..05ecab5ba3b 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -178,6 +178,7 @@ private slots: void unixLocaleName(); void testNames_data(); void testNames(); + void debugOutput(); private: QString m_decimal, m_thousand, m_sdate, m_ldate, m_time; QString m_sysapp; @@ -3410,6 +3411,34 @@ void tst_QLocale::testNames() } } +void tst_QLocale::debugOutput() +{ + // Test operator<<(QDebug, const QLocale &) works as intended: + QTest::failOnWarning(); + { + const QLocale en(QLocale::English, QLocale::LatinScript, QLocale::UnitedStates); + QTest::ignoreMessage(QtMsgType::QtWarningMsg, + "QLocale(English, Latin, United States)"); + qWarning() << en; + } + { + const auto params = [](const QLocale &loc) { + return (QLocale::languageToString(loc.language()) + + u", " + QLocale::scriptToString(loc.script()) + + u", " + QLocale::territoryToString(loc.territory())).toLatin1(); + }; + const QLocale sys = QLocale::system(); + QTest::ignoreMessage(QtMsgType::QtWarningMsg, + ("QLocale::system()/* " + params(sys) + " */").constData()); + // QTBUG-133922: system and its CLDR counterpart should differ. + qWarning() << sys; + const QLocale match(sys.language(), sys.script(), sys.territory()); + QTest::ignoreMessage(QtMsgType::QtWarningMsg, + ("QLocale(" + params(match) + ')').constData()); + qWarning() << match; + } +} + void tst_QLocale::dayName_data() { QTest::addColumn("locale_name");