Tidy up QCoreApplicationPrivate::initLocale()'s #if-ery
Explain each exception cleanly and as itself, thereby avoiding the need for long and tangled #if-ery conditions. Make sure to setlocale(LC_ALL, "") everywhere we think we have initialized the locale, including Integrity - it plainly has setlocale(), since we call it for LC_CTYPE - since we should at least give it the chance to set its implementation-defined default locale, instead of the standard-defined POSIX locale in use on entry to main(). Done-with: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Change-Id: Iab00984ba45dfc9a324b6a3c12e3d330b655a5a9 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> (cherry picked from commit 91bea4470ea25c1e6fe7e46559a37bdd7b703b63) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
aec4a7d1fd
commit
ea84a9fba9
@ -598,22 +598,30 @@ void QCoreApplicationPrivate::initConsole()
|
|||||||
|
|
||||||
void QCoreApplicationPrivate::initLocale()
|
void QCoreApplicationPrivate::initLocale()
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_UNIX) && !defined(QT_BOOTSTRAPPED)
|
#if defined(QT_BOOTSTRAPPED)
|
||||||
|
// Don't try to control bootstrap library locale or encoding.
|
||||||
|
#elif defined(Q_OS_UNIX)
|
||||||
Q_CONSTINIT static bool qt_locale_initialized = false;
|
Q_CONSTINIT static bool qt_locale_initialized = false;
|
||||||
if (qt_locale_initialized)
|
if (qt_locale_initialized)
|
||||||
return;
|
return;
|
||||||
qt_locale_initialized = true;
|
qt_locale_initialized = true;
|
||||||
|
|
||||||
# ifdef Q_OS_INTEGRITY
|
// By default the portable "C"/POSIX locale is selected and active.
|
||||||
setlocale(LC_CTYPE, "UTF-8");
|
// Apply the locale from the environment, via setlocale(), which will
|
||||||
# else
|
// read LC_ALL, LC_<category>, and LANG, in order (for each category).
|
||||||
# if defined(Q_OS_QNX) || (defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__)
|
|
||||||
// Android 6 still lacks nl_langinfo(), as does QNX, so we just assume it's
|
|
||||||
// always UTF-8 on these platforms.
|
|
||||||
auto nl_langinfo = [](int) { return "UTF-8"; };
|
|
||||||
# endif // QNX or Android NDK < 26, "O".
|
|
||||||
|
|
||||||
const char *locale = setlocale(LC_ALL, "");
|
const char *locale = 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.
|
||||||
|
# if defined(Q_OS_INTEGRITY)
|
||||||
|
setlocale(LC_CTYPE, "UTF-8");
|
||||||
|
# elif defined(Q_OS_QNX)
|
||||||
|
// QNX has no nl_langinfo, so we can't check.
|
||||||
|
// FIXME: Shouldn't we still setlocale("UTF-8")?
|
||||||
|
# elif defined(Q_OS_ANDROID) && __ANDROID_API__ < __ANDROID_API_O__
|
||||||
|
// 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);
|
const char *codec = nl_langinfo(CODESET);
|
||||||
if (Q_UNLIKELY(qstricmp(codec, "UTF-8") != 0 && qstricmp(codec, "utf8") != 0)) {
|
if (Q_UNLIKELY(qstricmp(codec, "UTF-8") != 0 && qstricmp(codec, "utf8") != 0)) {
|
||||||
QByteArray oldLocale = locale;
|
QByteArray oldLocale = locale;
|
||||||
@ -625,8 +633,8 @@ void QCoreApplicationPrivate::initLocale()
|
|||||||
newLocale += ".UTF-8";
|
newLocale += ".UTF-8";
|
||||||
newLocale = setlocale(LC_CTYPE, newLocale);
|
newLocale = setlocale(LC_CTYPE, newLocale);
|
||||||
|
|
||||||
// if locale doesn't exist, try some fallbacks
|
// If that locale doesn't exist, try some fallbacks:
|
||||||
# ifdef Q_OS_DARWIN
|
# if defined(Q_OS_DARWIN)
|
||||||
if (newLocale.isEmpty())
|
if (newLocale.isEmpty())
|
||||||
newLocale = setlocale(LC_CTYPE, "UTF-8");
|
newLocale = setlocale(LC_CTYPE, "UTF-8");
|
||||||
# endif
|
# endif
|
||||||
@ -640,7 +648,7 @@ void QCoreApplicationPrivate::initLocale()
|
|||||||
"reconfigure your locale. See the locale(1) manual for more information.",
|
"reconfigure your locale. See the locale(1) manual for more information.",
|
||||||
codec, oldLocale.constData(), newLocale.constData());
|
codec, oldLocale.constData(), newLocale.constData());
|
||||||
}
|
}
|
||||||
# endif // Integrity
|
# endif // Platform choice
|
||||||
#endif // Unix
|
#endif // Unix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user