From 51c5694d4abebdc4df9b8c210d08fb98e584a391 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Mon, 5 Sep 2016 16:32:30 +0200 Subject: [PATCH] feat(app): supports translations --- tests/languages/en.ts | 11 ++++++++++ tests/languages/fr.ts | 11 ++++++++++ tests/linphone.pro | 13 +++++++++++- tests/resources.qrc | 5 +++++ tests/src/app.cpp | 17 ++++++++++++++++ tests/src/app.hpp | 16 +++++++++++++++ tests/src/main.cpp | 7 +++++-- tests/src/views/main_window.cpp | 2 +- .../views/{main_window.h => main_window.hpp} | 1 - tests/ui/main_window.qml | 20 ++++++++++++++----- 10 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 tests/languages/en.ts create mode 100644 tests/languages/fr.ts create mode 100644 tests/src/app.cpp create mode 100644 tests/src/app.hpp rename tests/src/views/{main_window.h => main_window.hpp} (98%) diff --git a/tests/languages/en.ts b/tests/languages/en.ts new file mode 100644 index 000000000..b87c6bee9 --- /dev/null +++ b/tests/languages/en.ts @@ -0,0 +1,11 @@ + + + + + main_window + + helloWorld + Hello World! + + + diff --git a/tests/languages/fr.ts b/tests/languages/fr.ts new file mode 100644 index 000000000..609e23e2b --- /dev/null +++ b/tests/languages/fr.ts @@ -0,0 +1,11 @@ + + + + + main_window + + helloWorld + Bonjour le monde ! + + + diff --git a/tests/linphone.pro b/tests/linphone.pro index 59b7941ab..71675b9fa 100644 --- a/tests/linphone.pro +++ b/tests/linphone.pro @@ -4,11 +4,22 @@ TARGET = linphone TEMPLATE = app SOURCES += \ + src/app.cpp \ src/main.cpp \ src/views/main_window.cpp HEADERS += \ - src/views/main_window.h + src/app.hpp \ + src/views/main_window.hpp + +TRANSLATIONS = \ + languages/en.ts \ + languages/fr.ts + +lupdate_only{ + SOURCES = \ + ui/*.qml +} RESOURCES += \ resources.qrc diff --git a/tests/resources.qrc b/tests/resources.qrc index e522f90fc..6cf86537a 100644 --- a/tests/resources.qrc +++ b/tests/resources.qrc @@ -1,5 +1,10 @@ + + languages/en.qm + languages/fr.qm + + ui/main_window.qml diff --git a/tests/src/app.cpp b/tests/src/app.cpp new file mode 100644 index 000000000..7661ab156 --- /dev/null +++ b/tests/src/app.cpp @@ -0,0 +1,17 @@ +#include + +#include + +#include "app.hpp" + +#define LANGUAGES_PATH ":/languages/" + +App::App(int &argc, char **argv) : QGuiApplication(argc, argv) { + // Try to enable system translation by default. (else english) + if (m_translator.load(QString(LANGUAGES_PATH) + QLocale::system().name()) || + m_translator.load(LANGUAGES_PATH "en")) { + this->installTranslator(&m_translator); + } else { + qWarning() << "No translation found."; + } +} diff --git a/tests/src/app.hpp b/tests/src/app.hpp new file mode 100644 index 000000000..bf03a3fa5 --- /dev/null +++ b/tests/src/app.hpp @@ -0,0 +1,16 @@ +#ifndef APP_H_ +#define APP_H_ + +#include +#include + +class App : public QGuiApplication { +public: + App (int &argc, char **argv); + virtual ~App () {} + +private: + QTranslator m_translator; +}; + +#endif // APP_H_ diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 5232da415..8e3e5dca7 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -1,12 +1,15 @@ #include -#include #include +#include "app.hpp" + int main (int argc, char *argv[]) { - QGuiApplication app(argc, argv); + // Init main window. + App app(argc, argv); QQmlApplicationEngine engine(QUrl("qrc:/ui/main_window.qml")); + // File not found. if (engine.rootObjects().isEmpty()) exit(EXIT_FAILURE); diff --git a/tests/src/views/main_window.cpp b/tests/src/views/main_window.cpp index e1677444c..18f6aa6e5 100644 --- a/tests/src/views/main_window.cpp +++ b/tests/src/views/main_window.cpp @@ -1 +1 @@ -#include "main_window.h" +#include "main_window.hpp" diff --git a/tests/src/views/main_window.h b/tests/src/views/main_window.hpp similarity index 98% rename from tests/src/views/main_window.h rename to tests/src/views/main_window.hpp index bc5b820f4..4d049c8bc 100644 --- a/tests/src/views/main_window.h +++ b/tests/src/views/main_window.hpp @@ -1,5 +1,4 @@ #ifndef MAIN_WINDOW_H #define MAIN_WINDOW_H - #endif // MAIN_WINDOW diff --git a/tests/ui/main_window.qml b/tests/ui/main_window.qml index 5acc65cc0..22cd562f8 100644 --- a/tests/ui/main_window.qml +++ b/tests/ui/main_window.qml @@ -1,13 +1,23 @@ import QtQuick 2.5 -import QtQuick.Window 2.2 +import QtQuick.Controls 2.0 +import QtQuick.Layouts 1.3 -Window { +ApplicationWindow { + id: mainWindow visible: true - width: 800 - height: 600 + + header: ToolBar { + RowLayout { + anchors.fill: parent + } + } + + footer: TabBar { + + } Text { anchors.centerIn: parent - text: "Hello World!" + text: qsTr("helloWorld"); } }