summaryrefslogtreecommitdiffstats
path: root/app/translator.cpp
diff options
context:
space:
mode:
authorzheng_wenlong <wenlong_zheng@nexty-ele.com>2019-04-05 16:25:31 +0900
committerzheng_wenlong <wenlong_zheng@nexty-ele.com>2019-04-10 09:30:15 +0900
commit008e2c6e164d5ec5b93d68ba649ca1d9c8f28079 (patch)
treed8ed4442448a4c20c5d5610b05ddbd8377fa0e0f /app/translator.cpp
Add demo3 dashboard source code. [Patch Set 2] Update LICENSE file. BUG-AGL: SPEC-2261 Change-Id: Iccd58c03317dfe961fac2f42a8d719d4e70bbfcd Signed-off-by: zheng_wenlong <wenlong_zheng@nexty-ele.com>
Diffstat (limited to 'app/translator.cpp')
-rw-r--r--app/translator.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/app/translator.cpp b/app/translator.cpp
new file mode 100644
index 0000000..9b67f13
--- /dev/null
+++ b/app/translator.cpp
@@ -0,0 +1,52 @@
+#include "translator.h"
+
+#include <QtCore/QCoreApplication>
+#include <QtCore/QLocale>
+#include <QtCore/QTranslator>
+#include <QtCore/QDir>
+#include <QtCore/QDebug>
+
+Translator::Translator(QObject *parent)
+ : QObject(parent)
+ , m_language(QStringLiteral("C"))
+ , m_translator(nullptr)
+{
+}
+
+QString Translator::translate(const QString &string, const QString &language) const
+{
+ Q_UNUSED(language)
+ return string;
+}
+
+QString Translator::language() const
+{
+ return m_language;
+}
+
+void Translator::setLanguage(const QString &language)
+{
+ if (m_language == language) return;
+ m_language = language;
+ setTranslator(language);
+ emit languageChanged(language);
+}
+
+void Translator::setTranslator(const QString &language)
+{
+ if (m_translator) {
+ QCoreApplication::removeTranslator(m_translator);
+ } else {
+ m_translator = new QTranslator(this);
+ }
+ QLocale locale(language);
+ QString fileName = QCoreApplication::instance()->applicationName().toLower();
+ qDebug() << "####" << QDir::currentPath() << QCoreApplication::applicationDirPath();
+ if (m_translator->load(locale, fileName, QStringLiteral("_"), QStringLiteral("%1/../translations").arg(QCoreApplication::applicationDirPath()))) {
+ QCoreApplication::installTranslator(m_translator);
+ } else {
+ delete m_translator;
+ m_translator = nullptr;
+ }
+}
+