Let QLocale::uiLanguages() use WinRT API when possible

This patch introduces support for the WinRT UI languages API.
We are using the Win32 API to get the list of preferred languages when
the system locale is used. However, this API returns an incomplete
list.
As Qt 6 supports Windows 10 and above, we can make use of the WinRT
API, if it's supported by the compiler. This API returns the full list,
as reported by the Windows system itself.
Note however, that this API can't be used with Clang and MinGW, so
we still have to fall back to Win32 API for these compilers. We also
do it if WinRT API returns an empty list of languages for some reason.

Fixes: QTBUG-94341
Pick-to: 6.2
Change-Id: I1d23c68d2ec298ae7835d0d18718876ff041aede
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Ivan Solovev 2021-09-17 18:18:40 +02:00
parent f6cd55445d
commit 51e8d3592a
2 changed files with 32 additions and 2 deletions

View File

@ -791,11 +791,16 @@ qt_internal_extend_target(Core CONDITION UNIX AND (NACL OR NOT APPLE)
text/qlocale_unix.cpp
)
qt_internal_extend_target(Core CONDITION WIN32 AND (NACL OR NOT APPLE)
qt_internal_extend_target(Core CONDITION WIN32
SOURCES
text/qlocale_win.cpp
)
qt_internal_extend_target(Core CONDITION WIN32 AND MSVC
LIBRARIES
runtimeobject
)
qt_internal_extend_target(Core CONDITION QT_FEATURE_icu
SOURCES
text/qcollator_icu.cpp

View File

@ -51,6 +51,20 @@
# include <time.h>
#endif
#if defined(Q_CC_MSVC)
# include <winrt/base.h>
// Workaround for Windows SDK bug.
// See https://github.com/microsoft/Windows.UI.Composition-Win32-Samples/issues/47
namespace winrt::impl
{
template <typename Async>
auto wait_for(Async const& async, Windows::Foundation::TimeSpan const& timeout);
}
# include <winrt/Windows.Foundation.h>
# include <winrt/Windows.Foundation.Collections.h>
# include <winrt/Windows.System.UserProfile.h>
#endif // defined(Q_CC_MSVC)
QT_BEGIN_NAMESPACE
static QByteArray getWinLocaleName(LCID id = LOCALE_USER_DEFAULT);
@ -615,6 +629,18 @@ QVariant QSystemLocalePrivate::toCurrencyString(const QSystemLocale::CurrencyToS
QVariant QSystemLocalePrivate::uiLanguages()
{
QStringList result;
#if defined(Q_CC_MSVC) // msvc supports WinRT calls
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::System::UserProfile;
auto languages = GlobalizationPreferences::Languages();
for (const auto &lang : languages)
result << QString::fromStdString(winrt::to_string(lang));
if (!result.isEmpty())
return result; // else just fall back to WIN32 API implementation
#endif // defined(Q_CC_MSVC)
// mingw and clang still have to use Win32 API
unsigned long cnt = 0;
QVarLengthArray<wchar_t, 64> buf(64);
# if !defined(QT_BOOTSTRAPPED) // Not present in MinGW 4.9/bootstrap builds.
@ -629,7 +655,6 @@ QVariant QSystemLocalePrivate::uiLanguages()
}
}
# endif // !QT_BOOTSTRAPPED
QStringList result;
result.reserve(cnt);
const wchar_t *str = buf.constData();
for (; cnt > 0; --cnt) {