diff options
author | Tasuku Suzuki <tasuku.suzuki@qt.io> | 2017-12-27 20:34:03 +0900 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2017-12-29 17:26:33 +0100 |
commit | 4b7d58e71e8864a90d3b567f21029968d6e8edef (patch) | |
tree | 9f25bb4a64713bef08b020d090075c5f06cc7924 /app/translator.cpp | |
parent | d084bc1f764833566f3e8eafc9fc297c3fa9ee08 (diff) |
Add basic translation support
Added French and Japanese translations
Change-Id: I58eca5ffaad2367a4dd26b6472bdbcd45c957061
Signed-off-by: Tasuku Suzuki <tasuku.suzuki@qt.io>
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'app/translator.cpp')
-rw-r--r-- | app/translator.cpp | 52 |
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; + } +} + |