From 4b7d58e71e8864a90d3b567f21029968d6e8edef Mon Sep 17 00:00:00 2001 From: Tasuku Suzuki Date: Wed, 27 Dec 2017 20:34:03 +0900 Subject: Add basic translation support Added French and Japanese translations Change-Id: I58eca5ffaad2367a4dd26b6472bdbcd45c957061 Signed-off-by: Tasuku Suzuki Signed-off-by: Romain Forlot --- app/HVAC.qml | 12 +++++++--- app/HeatDegree.qml | 8 +++---- app/app.pro | 9 ++++++- app/main.cpp | 10 ++++++-- app/translations.pri | 16 +++++++++++++ app/translations/hvac_fr_FR.ts | 53 ++++++++++++++++++++++++++++++++++++++++++ app/translations/hvac_ja_JP.ts | 53 ++++++++++++++++++++++++++++++++++++++++++ app/translator.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ app/translator.h | 32 +++++++++++++++++++++++++ 9 files changed, 235 insertions(+), 10 deletions(-) create mode 100644 app/translations.pri create mode 100644 app/translations/hvac_fr_FR.ts create mode 100644 app/translations/hvac_ja_JP.ts create mode 100644 app/translator.cpp create mode 100644 app/translator.h diff --git a/app/HVAC.qml b/app/HVAC.qml index b3be443..c375f2f 100644 --- a/app/HVAC.qml +++ b/app/HVAC.qml @@ -18,11 +18,17 @@ import QtQuick 2.6 import QtQuick.Layouts 1.1 import QtQuick.Controls 2.0 import AGL.Demo.Controls 1.0 +import Translator 1.0 import 'api' as API ApplicationWindow { id: root + Translator { + id: translator +// language: 'ja_JP' + } + API.Binding { id: binding url: bindingAddress @@ -57,7 +63,7 @@ ApplicationWindow { anchors.left: fanSpeedSlider.left anchors.top: fanSpeedSlider.bottom font.pixelSize: 32 - text: 'FAN SPEED' + text: translator.translate(qsTr('FAN SPEED'), translator.language) } } } @@ -89,7 +95,7 @@ ApplicationWindow { Label { anchors.centerIn: parent color: parent.checked ? '#00ADDC' : '#848286' - text: 'A/C' + text: translator.translate(qsTr('A/C'), translator.language) font.pixelSize: parent.height / 3 } onCheckedChanged: { @@ -102,7 +108,7 @@ ApplicationWindow { Label { anchors.centerIn: parent color: parent.checked ? '#00ADDC' : '#848286' - text: 'AUTO' + text: translator.translate(qsTr('AUTO'), translator.language) font.pixelSize: parent.height / 3 } onCheckedChanged: { diff --git a/app/HeatDegree.qml b/app/HeatDegree.qml index aa5770a..a9bc83c 100644 --- a/app/HeatDegree.qml +++ b/app/HeatDegree.qml @@ -28,11 +28,11 @@ ListView { property int degree: currentIndex > -1 ? model.get(currentIndex).value : -1 model: ListModel { Component.onCompleted: { - append({value: 15, modelData: 'LO'}) + append({value: 15, modelData: translator.translate(qsTr('LO'), translator.language)}) for (var d = 16; d < 30; d++) { - append({value: d, modelData: d.toFixed(0) + '\u00b0'}) + append({value: d, modelData: translator.translate(qsTr('%1\u00b0'), translator.language).arg(d.toFixed(0))}) } - append({value: 30, modelData: 'HI'}) + append({value: 30, modelData: translator.translate(qsTr('HI'), translator.language)}) } } delegate: Label { @@ -41,7 +41,7 @@ ListView { horizontalAlignment: Label.AlignHCenter verticalAlignment: Label.AlignVCenter text: model.modelData - font.pixelSize: height * 0.8 + font.pixelSize: height * 0.7 color: (ListView.view.enabled && ListView.isCurrentItem) ? '#00ADDC' : 'white' } diff --git a/app/app.pro b/app/app.pro index da4e8a4..edd08aa 100644 --- a/app/app.pro +++ b/app/app.pro @@ -1,10 +1,17 @@ TARGET = hvac QT = quick qml -SOURCES = main.cpp +HEADERS += \ + translator.h + +SOURCES = main.cpp \ + translator.cpp RESOURCES += \ hvac.qrc \ images/images.qrc include(app.pri) + +LANGUAGES = ja_JP fr_FR +include(translations.pri) diff --git a/app/main.cpp b/app/main.cpp index b793ef3..211f52b 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -22,6 +22,8 @@ #include #include +#include "translator.h" + #ifdef HAVE_LIBHOMESCREEN #include #endif @@ -46,13 +48,17 @@ int main(int argc, char *argv[]) parser.process(app); QStringList positionalArguments = parser.positionalArguments(); + qmlRegisterType("Translator", 1, 0, "Translator"); + QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); QUrl bindingAddress; + int port; + QString secret; if (positionalArguments.length() == 2) { - int port = positionalArguments.takeFirst().toInt(); - QString secret = positionalArguments.takeFirst(); + port = positionalArguments.takeFirst().toInt(); + secret = positionalArguments.takeFirst(); bindingAddress.setScheme(QStringLiteral("ws")); bindingAddress.setHost(QStringLiteral("localhost")); bindingAddress.setPort(port); diff --git a/app/translations.pri b/app/translations.pri new file mode 100644 index 0000000..81bd94b --- /dev/null +++ b/app/translations.pri @@ -0,0 +1,16 @@ +defineReplace(prependAll) { + for(a,$$1):result += $$2$${a}$$3 + return($$result) +} + +qtPrepareTool(QMAKE_LRELEASE, lrelease) +TRANSLATIONS = $$prependAll(LANGUAGES, $$PWD/translations/$${TARGET}_,.ts) + +qm.depends = $${TRANSLATIONS} +qm.input = TRANSLATIONS +qm.output = $$OUT_PWD/../package/root/translations/${QMAKE_FILE_BASE}.qm +qm.commands = $$QMAKE_LRELEASE ${QMAKE_FILE_IN} -qm ${QMAKE_FILE_OUT} +qm.name = LRELEASE ${QMAKE_FILE_IN} +qm.CONFIG += no_link +QMAKE_EXTRA_COMPILERS += qm +PRE_TARGETDEPS += compiler_qm_make_all diff --git a/app/translations/hvac_fr_FR.ts b/app/translations/hvac_fr_FR.ts new file mode 100644 index 0000000..241a6f9 --- /dev/null +++ b/app/translations/hvac_fr_FR.ts @@ -0,0 +1,53 @@ + + + + + HVAC + + + FAN SPEED + Vitesse de ventilation + + + + A/C + + + + + AUTO + + + + + HeatDegree + + + LO + BAS + + + + %1° + + + + + HI + HAU + + + + main + + + port for binding + Port du binding + + + + secret for binding + Token de sécurité du binding + + + diff --git a/app/translations/hvac_ja_JP.ts b/app/translations/hvac_ja_JP.ts new file mode 100644 index 0000000..a3a78a8 --- /dev/null +++ b/app/translations/hvac_ja_JP.ts @@ -0,0 +1,53 @@ + + + + + HVAC + + + FAN SPEED + ファンの回転数 + + + + A/C + + + + + AUTO + + + + + HeatDegree + + + LO + + + + + %1° + + + + + HI + + + + + main + + + port for binding + binding 用のポート + + + + secret for binding + binding 用の secret + + + 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 +#include +#include +#include +#include + +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; + } +} + diff --git a/app/translator.h b/app/translator.h new file mode 100644 index 0000000..82c5872 --- /dev/null +++ b/app/translator.h @@ -0,0 +1,32 @@ +#ifndef TRANSLATOR_H +#define TRANSLATOR_H + +#include + +class QTranslator; + +class Translator : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged) +public: + explicit Translator(QObject *parent = nullptr); + + QString language() const; + + Q_INVOKABLE QString translate(const QString &string, const QString &language) const; +public slots: + void setLanguage(const QString &language); + +signals: + void languageChanged(const QString &language); + +private slots: + void setTranslator(const QString &language); + +private: + QString m_language; + QTranslator *m_translator; +}; + +#endif // TRANSLATOR_H -- cgit 1.2.3-korg