summaryrefslogtreecommitdiffstats
path: root/app/translator.cpp
diff options
context:
space:
mode:
authorTasuku Suzuki <tasuku.suzuki@qt.io>2017-11-20 17:19:14 +0900
committerTasuku Suzuki <tasuku.suzuki@qt.io>2017-11-29 09:54:57 +0000
commit464e6c7f6056cec8a1d150216338d58a827307e6 (patch)
tree30b7e6321b3bbff40dfc230c3f68248b409be261 /app/translator.cpp
parent16ab0b453ac9a2341342b927c14ceb426e39ff05 (diff)
I18N: add basic functionality and a few translations
Added very first i18n support in the simple application. Placed language buttons at the bottom to change current language. They should be removed once current language comes from somewhere. Laying out issues should be fixed later. Change-Id: Ib5d646784820cb2753edd5a588a8826ad7d20539 Signed-off-by: Tasuku Suzuki <tasuku.suzuki@qt.io> Signed-off-by: Tadao Tanikawa <tanikawa.tadao@jp.panasonic.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;
+ }
+}
+