FOSSredder 0.5.0
Loading...
Searching...
No Matches
LanguageService.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include <QObject>
9#include <QString>
10#include <QTranslator>
11#include <QVariantList>
12
13class QApplication;
14class QQmlEngine;
15
16namespace ui {
17
21class LanguageService : public QObject {
22 Q_OBJECT
23 Q_PROPERTY(QString currentLanguage READ currentLanguage WRITE
25 Q_PROPERTY(QVariantList availableLanguages READ availableLanguages NOTIFY
27
28public:
31 explicit LanguageService(QApplication* application, QQmlEngine* engine,
32 QObject* parent = nullptr);
33
37 QString currentLanguage() const {
38 return currentLanguage_;
39 }
40
45 void setCurrentLanguage(const QString& languageCode);
46
50 QVariantList availableLanguages() const {
51 return availableLanguages_;
52 }
53
59 Q_INVOKABLE bool applyLanguage(const QString& languageCode);
60
61signals:
66
71
72private:
73 void refreshAvailableLanguages();
74 bool isLanguageAvailable(const QString& languageCode) const;
75 QString normalizeLanguageCode(const QString& languageCode) const;
76 QString translationFileName(const QString& languageCode) const;
77 bool translationFileExists(const QString& languageCode) const;
78 bool applyCurrentLanguage(const QString& languageCode);
79 bool loadTranslation(const QString& languageCode);
80 void retranslateUi();
81 void persistLanguage(const QString& languageCode);
82 QString persistedLanguage() const;
83
84 QApplication* application_ = nullptr;
85 QQmlEngine* engine_ = nullptr;
86 QTranslator translator_;
87 QVariantList availableLanguages_;
88 QString currentLanguage_;
89};
90
91} // namespace ui
void setCurrentLanguage(const QString &languageCode)
Switch the active UI language if the requested translation is available.
QVariantList availableLanguages() const
Return the available language options for the UI.
void availableLanguagesChanged()
Emitted when the available language list changes.
QVariantList availableLanguages
LanguageService(QApplication *application, QQmlEngine *engine, QObject *parent=nullptr)
Create a language service bound to the application and QML engine.
void currentLanguageChanged()
Emitted when the active language changes.
Q_INVOKABLE bool applyLanguage(const QString &languageCode)
Applies a language code from QML.