summaryrefslogtreecommitdiffstats
path: root/app
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
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')
-rw-r--r--app/Dashboard.qml53
-rw-r--r--app/app.pro10
-rw-r--r--app/main.cpp13
-rw-r--r--app/translations.pri16
-rw-r--r--app/translations/dashboard_fr_FR.ts88
-rw-r--r--app/translations/dashboard_ja_JP.ts88
-rw-r--r--app/translations/dashboard_zh_CN.ts88
-rw-r--r--app/translator.cpp52
-rw-r--r--app/translator.h32
9 files changed, 419 insertions, 21 deletions
diff --git a/app/Dashboard.qml b/app/Dashboard.qml
index fd56521..091234d 100644
--- a/app/Dashboard.qml
+++ b/app/Dashboard.qml
@@ -18,10 +18,15 @@ import QtQuick 2.6
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0
import QtWebSockets 1.0
+import Translator 1.0
ApplicationWindow {
id: root
+ Translator {
+ id: translator
+ }
+
WebSocket {
property string api_str: "api/canivi"
property string verb_str: "subscribe"
@@ -109,8 +114,8 @@ ApplicationWindow {
anchors.rightMargin: -20
anchors.top: car.top
anchors.topMargin: 150
- title: 'LEFT FRONT TIRE'
- pressure: '23.1 PSI'
+ title: translator.translate(qsTr('LEFT FRONT TIRE'), translator.language)
+ pressure: translator.translate(qsTr('%1 PSI').arg(23.1), translator.language)
}
TirePressure {
@@ -118,8 +123,8 @@ ApplicationWindow {
anchors.rightMargin: -20
anchors.bottom: car.bottom
anchors.bottomMargin: 120
- title: 'LEFT REAR TIRE'
- pressure: '31.35 PSI'
+ title: translator.translate(qsTr('LEFT REAR TIRE'), translator.language)
+ pressure: translator.translate(qsTr('%1 PSI').arg(31.35), translator.language)
}
TirePressure {
@@ -128,8 +133,8 @@ ApplicationWindow {
anchors.leftMargin: -20
anchors.top: car.top
anchors.topMargin: 150
- title: 'RIGHT FRONT TIRE'
- pressure: '24.2 PSI'
+ title: translator.translate(qsTr('RIGHT FRONT TIRE'), translator.language)
+ pressure: translator.translate(qsTr('%1 PSI').arg(24.2), translator.language)
}
TirePressure {
@@ -138,8 +143,8 @@ ApplicationWindow {
anchors.leftMargin: -20
anchors.bottom : car.bottom
anchors.bottomMargin: 120
- title: 'RIGHT REAR TIRE'
- pressure: '33.0 PSI'
+ title: translator.translate(qsTr('RIGHT REAR TIRE'), translator.language)
+ pressure: translator.translate(qsTr('%1 PSI').arg(33.0), translator.language)
}
RowLayout {
@@ -160,7 +165,7 @@ ApplicationWindow {
anchors.left: parent.left
anchors.top: parent.bottom
anchors.topMargin: 10
- text: '(RPM)'
+ text: translator.translate(qsTr('(RPM)'), translator.language)
font.pixelSize: 26
}
}
@@ -190,7 +195,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignRight
- text: 'LEVEL:'
+ text: translator.translate(qsTr('LEVEL:'), translator.language)
font.pixelSize: 24
}
Label {
@@ -199,7 +204,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignLeft
- text: '9 GALLONS'
+ text: translator.translate(qsTr('%1 GALLONS').arg(9), translator.language)
font.pixelSize: 24
color: '#66FF99'
}
@@ -209,7 +214,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignRight
- text: 'RANGE:'
+ text: translator.translate(qsTr('RANGE:'), translator.language)
font.pixelSize: 24
}
Label {
@@ -218,7 +223,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignLeft
- text: '229 MI'
+ text: translator.translate(qsTr('%1 MI').arg(9), translator.language)
font.pixelSize: 24
color: '#66FF99'
}
@@ -228,7 +233,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignRight
- text: 'AVG:'
+ text: translator.translate(qsTr('AVG:'), translator.language)
font.pixelSize: 24
}
Label {
@@ -237,7 +242,7 @@ ApplicationWindow {
Layout.fillHeight: true
verticalAlignment: Label.AlignVCenter
horizontalAlignment: Label.AlignLeft
- text: '25.5 MPG'
+ text: translator.translate(qsTr('%1 MPG').arg(25.5), translator.language)
font.pixelSize: 24
color: '#66FF99'
}
@@ -248,9 +253,25 @@ ApplicationWindow {
anchors.left: parent.left
anchors.top: parent.bottom
anchors.topMargin: 10
- text: 'FUEL'
+ text: translator.translate(qsTr('FUEL'), translator.language)
font.pixelSize: 26
}
}
}
+
+ RowLayout {
+// visible: false
+ anchors.left: parent.left
+ anchors.bottom: parent.bottom
+ anchors.right: parent.right
+ Repeater {
+ model: ['C', 'fr_FR', 'ja_JP', 'zh_CN']
+
+ Button {
+ text: model.modelData
+ onClicked: translator.language = model.modelData
+ Layout.fillWidth: true
+ }
+ }
+ }
}
diff --git a/app/app.pro b/app/app.pro
index 9599de1..1aa4da0 100644
--- a/app/app.pro
+++ b/app/app.pro
@@ -1,7 +1,12 @@
TARGET = dashboard
QT = quickcontrols2
-SOURCES = main.cpp
+
+HEADERS += \
+ translator.h
+
+SOURCES = main.cpp \
+ translator.cpp
CONFIG += link_pkgconfig
PKGCONFIG += libhomescreen qlibwindowmanager
@@ -11,3 +16,6 @@ RESOURCES += \
images/images.qrc
include(app.pri)
+
+LANGUAGES = ja_JP fr_FR zh_CN
+include(translations.pri)
diff --git a/app/main.cpp b/app/main.cpp
index 9649b82..d7f15b5 100644
--- a/app/main.cpp
+++ b/app/main.cpp
@@ -14,14 +14,17 @@
* limitations under the License.
*/
-#include <QUrlQuery>
-#include <QQmlContext>
#include <QtCore/QDebug>
-#include <QCommandLineParser>
+#include <QtCore/QCommandLineParser>
+#include <QtCore/QUrlQuery>
#include <QtGui/QGuiApplication>
+#include <QtQml/QQmlContext>
#include <QtQml/QQmlApplicationEngine>
+#include <QtQuick/QQuickWindow>
#include <QtQuickControls2/QQuickStyle>
-#include <QQuickWindow>
+
+#include "translator.h"
+
#include <libhomescreen.hpp>
#include <qlibwindowmanager.h>
@@ -45,6 +48,8 @@ int main(int argc, char *argv[])
parser.process(app);
QStringList positionalArguments = parser.positionalArguments();
+ qmlRegisterType<Translator>("Translator", 1, 0, "Translator");
+
QQmlApplicationEngine engine;
if (positionalArguments.length() == 2) {
int port = positionalArguments.takeFirst().toInt();
diff --git a/app/translations.pri b/app/translations.pri
new file mode 100644
index 0000000..d5616ef
--- /dev/null
+++ b/app/translations.pri
@@ -0,0 +1,16 @@
+defineReplace(prependAll) {
+ for(a,$$1):result += $$2$${a}$$3
+ return($$result)
+}
+
+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 = $$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/dashboard_fr_FR.ts b/app/translations/dashboard_fr_FR.ts
new file mode 100644
index 0000000..2b7cf6e
--- /dev/null
+++ b/app/translations/dashboard_fr_FR.ts
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="fr_FR">
+<context>
+ <name>Dashboard</name>
+ <message>
+ <location filename="../Dashboard.qml" line="120"/>
+ <source>LEFT FRONT TIRE</source>
+ <translation>Pneu Avant Gauche</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="121"/>
+ <location filename="../Dashboard.qml" line="130"/>
+ <location filename="../Dashboard.qml" line="140"/>
+ <location filename="../Dashboard.qml" line="150"/>
+ <source>%1 PSI</source>
+ <translation>Pression %1</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="129"/>
+ <source>LEFT REAR TIRE</source>
+ <translation>Pneu Arrière Gauche</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="139"/>
+ <source>RIGHT FRONT TIRE</source>
+ <translation>Pneu Avant Droit</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="149"/>
+ <source>RIGHT REAR TIRE</source>
+ <translation>Pneu Arrière Droit</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="171"/>
+ <source>(RPM)</source>
+ <translation>Tr/mn</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="201"/>
+ <source>LEVEL:</source>
+ <translation>Niveau:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="210"/>
+ <source>%1 GALLONS</source>
+ <translation>%1 Litres</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="220"/>
+ <source>RANGE:</source>
+ <translation>Autonomie:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="229"/>
+ <source>%1 MI</source>
+ <translation>%1 Km</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="239"/>
+ <source>AVG:</source>
+ <translation>Moyenne:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="248"/>
+ <source>%1 MPG</source>
+ <translation>%1 l/100km</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="259"/>
+ <source>FUEL</source>
+ <translation>Carburant</translation>
+ </message>
+</context>
+<context>
+ <name>main</name>
+ <message>
+ <location filename="../main.cpp" line="50"/>
+ <source>port for binding</source>
+ <translation>Port du Binder</translation>
+ </message>
+ <message>
+ <location filename="../main.cpp" line="51"/>
+ <source>secret for binding</source>
+ <translation>Secret Binder</translation>
+ </message>
+</context>
+</TS>
diff --git a/app/translations/dashboard_ja_JP.ts b/app/translations/dashboard_ja_JP.ts
new file mode 100644
index 0000000..06b25dc
--- /dev/null
+++ b/app/translations/dashboard_ja_JP.ts
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="ja_JP">
+<context>
+ <name>Dashboard</name>
+ <message>
+ <location filename="../Dashboard.qml" line="115"/>
+ <source>LEFT FRONT TIRE</source>
+ <translation>左前輪</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="116"/>
+ <location filename="../Dashboard.qml" line="125"/>
+ <location filename="../Dashboard.qml" line="135"/>
+ <location filename="../Dashboard.qml" line="145"/>
+ <source>%1 PSI</source>
+ <translation>空気圧 %1</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="124"/>
+ <source>LEFT REAR TIRE</source>
+ <translation>左後輪</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="134"/>
+ <source>RIGHT FRONT TIRE</source>
+ <translation>右前輪</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="144"/>
+ <source>RIGHT REAR TIRE</source>
+ <translation>右後輪</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="166"/>
+ <source>(RPM)</source>
+ <translation>(RPM)</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="196"/>
+ <source>LEVEL:</source>
+ <translation>レベル:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="205"/>
+ <source>%1 GALLONS</source>
+ <translation>%1 ガロン</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="215"/>
+ <source>RANGE:</source>
+ <translation>レンジ:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="224"/>
+ <source>%1 MI</source>
+ <translation>%1 MI</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="234"/>
+ <source>AVG:</source>
+ <translation>平均:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="243"/>
+ <source>%1 MPG</source>
+ <translation>%1 MPG</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="254"/>
+ <source>FUEL</source>
+ <translation>燃料</translation>
+ </message>
+</context>
+<context>
+ <name>main</name>
+ <message>
+ <location filename="../main.cpp" line="49"/>
+ <source>port for binding</source>
+ <translation type="unfinished"></translation>
+ </message>
+ <message>
+ <location filename="../main.cpp" line="50"/>
+ <source>secret for binding</source>
+ <translation type="unfinished"></translation>
+ </message>
+</context>
+</TS>
diff --git a/app/translations/dashboard_zh_CN.ts b/app/translations/dashboard_zh_CN.ts
new file mode 100644
index 0000000..ece07f9
--- /dev/null
+++ b/app/translations/dashboard_zh_CN.ts
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE TS>
+<TS version="2.1" language="fr_FR">
+<context>
+ <name>Dashboard</name>
+ <message>
+ <location filename="../Dashboard.qml" line="120"/>
+ <source>LEFT FRONT TIRE</source>
+ <translation>左前车胎</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="121"/>
+ <location filename="../Dashboard.qml" line="130"/>
+ <location filename="../Dashboard.qml" line="140"/>
+ <location filename="../Dashboard.qml" line="150"/>
+ <source>%1 PSI</source>
+ <translation>%1 磅每平方英寸(PSI)</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="129"/>
+ <source>LEFT REAR TIRE</source>
+ <translation>左后车胎</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="139"/>
+ <source>RIGHT FRONT TIRE</source>
+ <translation>右前车胎</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="149"/>
+ <source>RIGHT REAR TIRE</source>
+ <translation>右后车胎</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="171"/>
+ <source>(RPM)</source>
+ <translation>引擎转速</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="201"/>
+ <source>LEVEL:</source>
+ <translation>剩余油量:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="210"/>
+ <source>%1 GALLONS</source>
+ <translation>%1 加仑</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="220"/>
+ <source>RANGE:</source>
+ <translation>续航里程:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="229"/>
+ <source>%1 MI</source>
+ <translation>%1 英里</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="239"/>
+ <source>AVG:</source>
+ <translation type="unfinished">平均油耗:</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="248"/>
+ <source>%1 MPG</source>
+ <translation type="unfinished">%1 英里每加仑(MPG)</translation>
+ </message>
+ <message>
+ <location filename="../Dashboard.qml" line="259"/>
+ <source>FUEL</source>
+ <translation>燃油</translation>
+ </message>
+</context>
+<context>
+ <name>main</name>
+ <message>
+ <location filename="../main.cpp" line="50"/>
+ <source>port for binding</source>
+ <translation>Binder端口</translation>
+ </message>
+ <message>
+ <location filename="../main.cpp" line="51"/>
+ <source>secret for binding</source>
+ <translation>Binder令牌</translation>
+ </message>
+</context>
+</TS>
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;
+ }
+}
+
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 <QtCore/QObject>
+
+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