Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qlocale_wasm.cpp
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qlocale_p.h"
5
6#include <emscripten/val.h>
7
8#include <string>
9#include <vector>
10
11QT_BEGIN_NAMESPACE
12
13#ifndef QT_NO_SYSTEMLOCALE
14
15namespace {
16
17QStringList navigatorLanguages()
18{
19 using emscripten::val;
20 const val navigator = val::global("navigator");
21 const auto languages = emscripten::vecFromJSArray<std::string>(navigator["languages"]);
22 QStringList qtLanguages;
23 for (const std::string& language : languages)
24 qtLanguages.append(QString::fromStdString(language));
25 return qtLanguages;
26}
27
28}
29
30QVariant QSystemLocale::query(QueryType query, QVariant &&in) const
31{
32 Q_UNUSED(in);
33
34 switch (query) {
35 case QSystemLocale::UILanguages:
36 return QVariant(navigatorLanguages());
37 default:
38 break;
39 }
40
41 return QVariant();
42}
43
44QLocale QSystemLocale::fallbackLocale() const
45{
46 const QStringList languages = navigatorLanguages();
47 if (languages.isEmpty())
48 return QLocale(u"en-US");
49 return QLocale(languages[0]);
50}
51
52#endif // QT_NO_SYSTEMLOCALE
53
54QT_END_NAMESPACE