From d9811525821aae5a034d0a5157cca0120f3843a2 Mon Sep 17 00:00:00 2001 From: Shrikant Dhumal Date: Fri, 30 Sep 2016 11:58:26 -0700 Subject: Created internationalization and localization app Change-Id: If3bee4300b7188c8bb1fd566af8efef68c56229b Reviewed-by: Alistair Adams --- plugins/comtqci18ndemo/comtqci18ndemo.cpp | 99 +++++++++++++++++++++++++++++++ plugins/comtqci18ndemo/comtqci18ndemo.h | 72 ++++++++++++++++++++++ plugins/comtqci18ndemo/comtqci18ndemo.pro | 30 ++++++++++ plugins/comtqci18ndemo/plugin.cpp | 49 +++++++++++++++ plugins/comtqci18ndemo/qmldir | 2 + 5 files changed, 252 insertions(+) create mode 100644 plugins/comtqci18ndemo/comtqci18ndemo.cpp create mode 100644 plugins/comtqci18ndemo/comtqci18ndemo.h create mode 100644 plugins/comtqci18ndemo/comtqci18ndemo.pro create mode 100644 plugins/comtqci18ndemo/plugin.cpp create mode 100644 plugins/comtqci18ndemo/qmldir (limited to 'plugins') diff --git a/plugins/comtqci18ndemo/comtqci18ndemo.cpp b/plugins/comtqci18ndemo/comtqci18ndemo.cpp new file mode 100644 index 0000000..90b7226 --- /dev/null +++ b/plugins/comtqci18ndemo/comtqci18ndemo.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Neptune IVI UI. +** +** $QT_BEGIN_LICENSE:GPL-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: GPL-3.0 +** +****************************************************************************/ + +#include "comtqci18ndemo.h" +#include +#include + +ComTQCi18nDemo::ComTQCi18nDemo(QObject *parent) + : QObject(parent), + m_languageLocale(""), + m_languageFilePath(""), + m_languageFilePrefix("") +{ +} + +void ComTQCi18nDemo::setPrefix(QString languageFilePrefix) +{ + qDebug() << "File prefix: " << languageFilePrefix; + + m_languageFilePrefix = languageFilePrefix; +} + +void ComTQCi18nDemo::setPath(QUrl languageFilePath) +{ + qDebug() << "File path: " << languageFilePath.toLocalFile(); + + m_languageFilePath = languageFilePath.toLocalFile(); +} + +void ComTQCi18nDemo::setLanguageLocale(QString languageLocale) +{ + qDebug() << "Locale: " << languageLocale; + + if (m_languageLocale != languageLocale) { + if ( loadTranslationFile(languageLocale) ) { + m_languageLocale = languageLocale; + + emit languageLocaleChanged(); + emit languageChanged(); + } + } +} + +QString ComTQCi18nDemo::languageLocale() const +{ + return m_languageLocale; +} + +QString ComTQCi18nDemo::emptyString() const +{ + return ""; +} + +bool ComTQCi18nDemo::loadTranslationFile(QString &langLocale) +{ + QString fileToLoad(m_languageFilePath + m_languageFilePrefix + "_"); + fileToLoad += langLocale + ".qm"; + + qDebug() << "File to load: " << fileToLoad; + + if ( m_translator.load(fileToLoad) ) { + qDebug() << "Translation file loaded"; + qApp->installTranslator(&m_translator); + + return true; + } + + qDebug() << "Failed to load translation file"; + + return false; +} diff --git a/plugins/comtqci18ndemo/comtqci18ndemo.h b/plugins/comtqci18ndemo/comtqci18ndemo.h new file mode 100644 index 0000000..ddb4320 --- /dev/null +++ b/plugins/comtqci18ndemo/comtqci18ndemo.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Neptune IVI UI. +** +** $QT_BEGIN_LICENSE:GPL-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: GPL-3.0 +** +****************************************************************************/ + +#ifndef COMTQCI18NDEMO_H +#define COMTQCI18NDEMO_H + +#include +#include +#include + +class ComTQCi18nDemo : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QString languageLocale READ languageLocale WRITE setLanguageLocale NOTIFY languageLocaleChanged) + Q_PROPERTY(QString emptyString READ emptyString NOTIFY languageChanged) + +public: + explicit ComTQCi18nDemo(QObject *parent = 0); + + Q_INVOKABLE void setPrefix(QString languageFilePrefix); + Q_INVOKABLE void setPath(QUrl languageFilePath); + + Q_INVOKABLE void setLanguageLocale(QString languageLocale); + QString languageLocale() const; + + // Helper method to notify string change + QString emptyString() const; + +signals: + void languageLocaleChanged(); + void languageChanged(); + +protected: + bool loadTranslationFile(QString &langLocale); + + QString m_languageLocale; + QString m_languageFilePath; + QString m_languageFilePrefix; + + QTranslator m_translator; +}; + +#endif // COMTQCI18NDEMO_H diff --git a/plugins/comtqci18ndemo/comtqci18ndemo.pro b/plugins/comtqci18ndemo/comtqci18ndemo.pro new file mode 100644 index 0000000..bc692c0 --- /dev/null +++ b/plugins/comtqci18ndemo/comtqci18ndemo.pro @@ -0,0 +1,30 @@ +TEMPLATE = lib +TARGET = comtqci18ndemoplugin +QT += qml quick +CONFIG += qt plugin c++11 + +TARGET = $$qtLibraryTarget($$TARGET) +uri = com.theqtcompany.comtqci18ndemo + +SOURCES += \ + plugin.cpp \ + comtqci18ndemo.cpp \ + +HEADERS += \ + comtqci18ndemo.h \ + +OTHER_FILES = qmldir + +!equals(_PRO_FILE_PWD_, $$OUT_PWD) { + copy_qmldir.target = $$OUT_PWD/qmldir + copy_qmldir.depends = $$_PRO_FILE_PWD_/qmldir + copy_qmldir.commands = $(COPY_FILE) \"$$replace(copy_qmldir.depends, /, $$QMAKE_DIR_SEP)\" \"$$replace(copy_qmldir.target, /, $$QMAKE_DIR_SEP)\" + QMAKE_EXTRA_TARGETS += copy_qmldir + PRE_TARGETDEPS += $$copy_qmldir.target +} + +qmldir.files = qmldir +installPath = $$[QT_INSTALL_QML]/$$replace(uri, \\., /) +qmldir.path = $$installPath +target.path = $$installPath +INSTALLS += target qmldir diff --git a/plugins/comtqci18ndemo/plugin.cpp b/plugins/comtqci18ndemo/plugin.cpp new file mode 100644 index 0000000..cd4b6c5 --- /dev/null +++ b/plugins/comtqci18ndemo/plugin.cpp @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2016 Pelagicore AG +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Neptune IVI UI. +** +** $QT_BEGIN_LICENSE:GPL-QTAS$ +** Commercial License Usage +** Licensees holding valid commercial Qt Automotive Suite licenses may use +** this file in accordance with the commercial license agreement provided +** with the Software or, alternatively, in accordance with the terms +** contained in a written agreement between you and The Qt Company. For +** licensing terms and conditions see https://www.qt.io/terms-conditions. +** For further information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +** SPDX-License-Identifier: GPL-3.0 +** +****************************************************************************/ + +#include +#include +#include "comtqci18ndemo.h" + +class ComTQCi18nDemoPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface/1.0") +public: + virtual void registerTypes(const char *uri) + { + Q_ASSERT(QLatin1String(uri) == QLatin1String("com.theqtcompany.comtqci18ndemo")); + + qmlRegisterType(uri, 1, 0, "ComTQCi18nDemo"); + } +}; + +#include "plugin.moc" diff --git a/plugins/comtqci18ndemo/qmldir b/plugins/comtqci18ndemo/qmldir new file mode 100644 index 0000000..8a23ec8 --- /dev/null +++ b/plugins/comtqci18ndemo/qmldir @@ -0,0 +1,2 @@ +module com.theqtcompany.comtqci18ndemo +plugin comtqci18ndemoplugin -- cgit v1.2.3