summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2021-02-05 19:57:31 +0100
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2021-02-11 15:01:45 +0100
commitd8158c6c93fc2a83f5acacc7baaf1a840951cf1e (patch)
treeb64535ff57ed56a1076c90ccc9b25462075a4c4f
parent62af65551f3d4c775ff7f7353feacac177650e81 (diff)
macOS: Be honest about the system locale
The system locale of a macOS application is not affected by environment variables like LANG. Yet, we were reporting a name determined from environment variables as the fallbackUiLocale(), rather than one based on the language and country of the actual system locale. This lead, via the usual CLDR likely-subtag fallback, to claiming the system locale's name, language, script and country were those obtained from these environment variables, even when they were at odds with the actual locale being used by the system, which was being used for some queries. Worse yet, any data not supplied by these queries was being obtained from the same CLDR locale as the name, making for an inconsistent mix of locale data. While we cannot avoid the likely-subtag fallback step for fallback data, it is more consistent to use the actual system locale's name as start-point for that fallback. At the same time, add support for the language, script and country queries, so that the QLocale::system() describes itself faithfully, instead of claiming to be the locale that results from that fallback. If we want to support LANG or other environment variable overrides, they should be handled by the layer above the system locale, by changing the default locale of the Qt application, as if the user had called QLocale::setDefault(). [ChangeLog][QtCore][QLocale] QLocale::system() on macOS no longer pretends to support LANG or other environment variables as overrides, as this is not a feature that the system locale on macOS supports. To override the locale of an application, use QLocale::setDefault(), or pass -AppleLocale en_US. Fixes: QTBUG-90971 Change-Id: Ibdaf5ff9a2050f61233a88eabf3c29094f7757f1 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r--src/corelib/text/qlocale_mac.mm47
-rw-r--r--tests/auto/corelib/text/qlocale/tst_qlocale.cpp18
2 files changed, 38 insertions, 27 deletions
diff --git a/src/corelib/text/qlocale_mac.mm b/src/corelib/text/qlocale_mac.mm
index 7a41bdd8c1..9c1eb6dcfb 100644
--- a/src/corelib/text/qlocale_mac.mm
+++ b/src/corelib/text/qlocale_mac.mm
@@ -54,29 +54,11 @@ QT_BEGIN_NAMESPACE
** 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<CFLocaleRef> l = CFLocaleCopyCurrent();
- CFStringRef locale = CFLocaleGetIdentifier(l);
- result = QString::fromCFString(locale);
- }
- return result;
+ QCFType<CFLocaleRef> l = CFLocaleCopyCurrent();
+ CFStringRef locale = CFLocaleGetIdentifier(l);
+ return QString::fromCFString(locale);
}
static QString macMonthName(int month, QSystemLocale::QueryType type)
@@ -423,12 +405,31 @@ QLocale QSystemLocale::fallbackLocale() const
return QLocale(getMacLocaleName());
}
+template <auto CodeToValueFunction>
+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<QLocalePrivate::codeToLanguage>(kCFLocaleLanguageCode);
+ case CountryId:
+ return getLocaleValue<QLocalePrivate::codeToCountry>(kCFLocaleCountryCode);
+ case ScriptId:
+ return getLocaleValue<QLocalePrivate::codeToScript>(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 60cd4a9a16..83aa5b2c19 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");