QCoreApplicationPrivate::initLocale(): report correct encoding

We override the old encoding because it wasn't UTF-8, then we use a
fresh call to nl_langinfo(CODESET) when reporting the encoding that
"is not UTF-8", except that we've just fixed that so it is now.

Store the old encoding in a std::string before we change it, so that
we can report what it was rather than what we changed it to.
Amends commit 3690c202f9

Pick-to: 6.5 6.6
Task-number: QTBUG-113371
Change-Id: I5f7c3648890cb0abf1d4769af24715686762c176
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Edward Welbourne 2023-05-22 09:10:58 +02:00
parent f7e201ba89
commit a7c87be888
1 changed files with 7 additions and 4 deletions

View File

@ -104,6 +104,7 @@
#include <algorithm>
#include <memory>
#include <string>
QT_BEGIN_NAMESPACE
@ -622,8 +623,10 @@ 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 *charEncoding = nl_langinfo(CODESET);
if (Q_UNLIKELY(qstricmp(charEncoding, "UTF-8") != 0 && qstricmp(charEncoding, "utf8") != 0)) {
// std::string's SSO usually saves this the need to allocate:
const std::string oldEncoding = nl_langinfo(CODESET);
if (!Q_LIKELY(qstricmp(oldEncoding.data(), "UTF-8") == 0
|| qstricmp(oldEncoding.data(), "utf8") == 0)) {
const QByteArray oldLocale = setlocale(LC_ALL, nullptr);
QByteArray newLocale;
bool warnOnOverride = true;
@ -658,14 +661,14 @@ void QCoreApplicationPrivate::initLocale()
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(), nl_langinfo(CODESET));
"for more information.", oldLocale.constData(), oldEncoding.data());
} else if (warnOnOverride) {
// 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(), nl_langinfo(CODESET), newLocale.constData());
oldLocale.constData(), oldEncoding.data(), newLocale.constData());
}
}
# endif // Platform choice