summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2023-03-01 14:06:41 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-03-10 11:21:22 +0000
commit611777e084c503c7f3195adbdbfb10a5f74308f9 (patch)
treecb5d771165574d55cd66369ff9727c6c0f94eeab
parent888de8bcf83926ba3adec7f2c500fae0c0cd129f (diff)
downloadqtbase-611777e084c503c7f3195adbdbfb10a5f74308f9.tar.gz
QCoreApplication::initLocale(): Improve warning on failure
If we fail to set a UTF-8 locale, the warning claims Qt shall use a UTF-8 locale, for which it reports an empty string, when in fact it isn't using a UTF-8 locale, although it'll interact with the system as if it were. Also, the user is liable to understand "system locale encoding" as referring to the underlying system rather than whatever they may have configured in a local shell; and a non-native speaker assures me the use of "shall" is also apt to confuse; so reword the messages. As the old locale is only needed for the code path prints the warning we can defer resolving it until we know we're going to use it, by querying the locale at that point. Done-with: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Change-Id: Ie6a6e7a707200e58335fcb64cb1584f0c307895a Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit fff9395c2899841bdca07f400392595963d9ff93) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 96efb94b0c..d38cb74242 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -559,7 +559,7 @@ void QCoreApplicationPrivate::initLocale()
// By default the portable "C"/POSIX locale is selected and active.
// Apply the locale from the environment, via setlocale(), which will
// read LC_ALL, LC_<category>, and LANG, in order (for each category).
- const char *locale = setlocale(LC_ALL, "");
+ setlocale(LC_ALL, "");
// Next, let's ensure that LC_CTYPE is UTF-8, since QStringConverter's
// QLocal8Bit hard-codes this, and we need to be consistent.
@@ -572,9 +572,9 @@ void QCoreApplicationPrivate::initLocale()
// Android 6 still lacks nl_langinfo(), so we can't check.
// FIXME: Shouldn't we still setlocale("UTF-8")?
# else
- const char *codec = nl_langinfo(CODESET);
- if (Q_UNLIKELY(qstricmp(codec, "UTF-8") != 0 && qstricmp(codec, "utf8") != 0)) {
- QByteArray oldLocale = locale;
+ const char *charEncoding = nl_langinfo(CODESET);
+ if (Q_UNLIKELY(qstricmp(charEncoding, "UTF-8") != 0 && qstricmp(charEncoding, "utf8") != 0)) {
+ const QByteArray oldLocale = setlocale(LC_ALL, nullptr);
QByteArray newLocale = setlocale(LC_CTYPE, nullptr);
if (qsizetype dot = newLocale.indexOf('.'); dot != -1)
newLocale.truncate(dot); // remove encoding, if any
@@ -593,10 +593,20 @@ void QCoreApplicationPrivate::initLocale()
if (newLocale.isEmpty())
newLocale = setlocale(LC_CTYPE, "C.utf8");
- qWarning("Detected system locale encoding (%s, locale \"%s\") is not UTF-8.\n"
- "Qt shall use a UTF-8 locale (\"%s\") instead. If this causes problems,\n"
- "reconfigure your locale. See the locale(1) manual for more information.",
- codec, oldLocale.constData(), newLocale.constData());
+ if (newLocale.isEmpty()) {
+ // Failed to set a UTF-8 locale.
+ qWarning("Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
+ "Qt depends on a UTF-8 locale, but has failed to switch to one.\n"
+ "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
+ "for more information.", oldLocale.constData(), charEncoding);
+ } else {
+ // Let the user know we over-rode their configuration.
+ qWarning("Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n"
+ "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n"
+ "If this causes problems, reconfigure your locale. See the locale(1) manual\n"
+ "for more information.",
+ oldLocale.constData(), charEncoding, newLocale.constData());
+ }
}
# endif // Platform choice
#endif // Unix