tests / package-smoke-linux (push) Skipped
tests / package-smoke-windows (push) Skipped
tests / package-smoke-macos (push) Skipped
tests / linux (push) Successful in 2m22s
tests / macos (push) Successful in 2m29s
tests / windows (push) Successful in 4m31s
Signed-off-by: galeon <dertiellis@gmail.com>
71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <QAtomicInt>
|
|
#include <QHash>
|
|
#include <QJSValue>
|
|
#include <QList>
|
|
#include <QObject>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
#include <QVariantList>
|
|
#include <QtQml/qqmlregistration.h>
|
|
|
|
class QQmlEngine;
|
|
class QJSEngine;
|
|
|
|
class Localization : public QObject
|
|
{
|
|
Q_OBJECT
|
|
QML_NAMED_ELEMENT(Loc)
|
|
QML_SINGLETON
|
|
|
|
Q_PROPERTY(QJSValue t READ t NOTIFY languageChanged)
|
|
Q_PROPERTY(QString language READ language NOTIFY languageChanged)
|
|
Q_PROPERTY(QString resolvedLanguage READ resolvedLanguage NOTIFY languageChanged)
|
|
|
|
public:
|
|
static Localization &instance();
|
|
|
|
static Localization *create(QQmlEngine *qmlEngine, QJSEngine *jsEngine);
|
|
|
|
bool load();
|
|
|
|
QJSValue t() const { return m_tree; }
|
|
QString language() const { return m_language; }
|
|
QString resolvedLanguage() const;
|
|
|
|
void setLanguage(const QString &code);
|
|
|
|
Q_INVOKABLE QVariantList availableLanguages() const;
|
|
|
|
[[nodiscard]] QString textFor(const char *key) const;
|
|
[[nodiscard]] QStringList listFor(const char *key) const;
|
|
|
|
signals:
|
|
void languageChanged();
|
|
|
|
private:
|
|
explicit Localization(QObject *parent = nullptr);
|
|
|
|
void rebuildTree();
|
|
|
|
QHash<QString, QStringList> m_singles;
|
|
QHash<QString, QList<QStringList>> m_lists;
|
|
QStringList m_codes;
|
|
QHash<QString, QString> m_displayNames;
|
|
|
|
QAtomicInt m_index{0};
|
|
QString m_language{QStringLiteral("system")};
|
|
|
|
QJSEngine *m_engine = nullptr;
|
|
QJSValue m_tree;
|
|
};
|
|
|
|
namespace Loc {
|
|
|
|
[[nodiscard]] QString text(const char *key);
|
|
|
|
[[nodiscard]] QStringList list(const char *key);
|
|
|
|
}
|