diff --git a/src/corelib/text/qlocale_mac.mm b/src/corelib/text/qlocale_mac.mm index 7a41bdd8c14..9c1eb6dcfb8 100644 --- a/src/corelib/text/qlocale_mac.mm +++ b/src/corelib/text/qlocale_mac.mm @@ -54,29 +54,11 @@ ** Wrappers for Mac locale system functions */ -static QByteArray envVarLocale() -{ - QByteArray -#ifdef Q_OS_UNIX - lang = qgetenv("LC_ALL"); - if (lang.isEmpty()) - lang = qgetenv("LC_NUMERIC"); - if (lang.isEmpty()) -#endif - lang = qgetenv("LANG"); - return lang; -} - static QString getMacLocaleName() { - QString result = QString::fromLocal8Bit(envVarLocale()); - if (result.isEmpty() - || (result != QLatin1String("C") && !qt_splitLocaleName(result))) { - QCFType l = CFLocaleCopyCurrent(); - CFStringRef locale = CFLocaleGetIdentifier(l); - result = QString::fromCFString(locale); - } - return result; + QCFType l = CFLocaleCopyCurrent(); + CFStringRef locale = CFLocaleGetIdentifier(l); + return QString::fromCFString(locale); } static QString macMonthName(int month, QSystemLocale::QueryType type) @@ -423,12 +405,31 @@ static QVariant macQuoteString(QSystemLocale::QueryType type, QStringView str) return QLocale(getMacLocaleName()); } +template +static QVariant getLocaleValue(CFStringRef key) +{ + if (auto code = getCFLocaleValue(key); !code.isNull()) { + // If an invalid locale is requested with -AppleLocale, the system APIs + // will report invalid or empty locale values back to us, which codeToLanguage() + // and friends will fail to parse, resulting in returning QLocale::Any{L/C/S}. + // If this is the case, we fall down and return a null-variant, which + // QLocale's updateSystemPrivate() will interpret to use fallback logic. + if (auto value = CodeToValueFunction(code.toString())) + return value; + } + return QVariant(); +} + QVariant QSystemLocale::query(QueryType type, QVariant in) const { QMacAutoReleasePool pool; switch(type) { -// case Name: -// return getMacLocaleName(); + case LanguageId: + return getLocaleValue(kCFLocaleLanguageCode); + case CountryId: + return getLocaleValue(kCFLocaleCountryCode); + case ScriptId: + return getLocaleValue(kCFLocaleScriptCode); case DecimalPoint: return getCFLocaleValue(kCFLocaleDecimalSeparator); case GroupSeparator: diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 60cd4a9a166..83aa5b2c198 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -504,6 +504,7 @@ void tst_QLocale::defaulted_ctor() #if QT_CONFIG(process) static inline bool runSysApp(const QString &binary, + const QStringList &args, const QStringList &env, QString *output, QString *errorMessage) @@ -512,7 +513,7 @@ static inline bool runSysApp(const QString &binary, errorMessage->clear(); QProcess process; process.setEnvironment(env); - process.start(binary, QStringList()); + process.start(binary, args); process.closeWriteChannel(); if (!process.waitForStarted()) { *errorMessage = QLatin1String("Cannot start '") + binary @@ -535,8 +536,12 @@ static inline bool runSysAppTest(const QString &binary, QString *errorMessage) { QString output; + QStringList args; +#ifdef Q_OS_MACOS + args << "-AppleLocale" << requestedLocale; +#endif baseEnv.append(QStringLiteral("LANG=") + requestedLocale); - if (!runSysApp(binary, baseEnv, &output, errorMessage)) + if (!runSysApp(binary, args, baseEnv, &output, errorMessage)) return false; if (output.isEmpty()) { @@ -617,8 +622,13 @@ void tst_QLocale::emptyCtor_data() // Get default locale. QString defaultLoc; QString errorMessage; - if (runSysApp(m_sysapp, cleanEnv, &defaultLoc, &errorMessage)) { -#define ADD_CTOR_TEST(give) QTest::newRow(give) << defaultLoc; + if (runSysApp(m_sysapp, QStringList(), cleanEnv, &defaultLoc, &errorMessage)) { +#if defined(Q_OS_MACOS) + QString localeForInvalidLocale = "C"; +#else + QString localeForInvalidLocale = defaultLoc; +#endif +#define ADD_CTOR_TEST(give) QTest::newRow(give) << localeForInvalidLocale; ADD_CTOR_TEST("en/"); ADD_CTOR_TEST("asdfghj"); ADD_CTOR_TEST("123456");