From 7d0141af41de51b8c71032d4b2ae54c5cd01039f Mon Sep 17 00:00:00 2001 From: Anmol Gautam Date: Fri, 29 Jun 2018 03:23:18 +0530 Subject: [PATCH] Added i18n to QML Plugins --- src/lib/CMakeLists.txt | 1 + src/lib/plugins/qml/api/i18n/qmli18n.cpp | 48 ++++++++++++++++++++++++ src/lib/plugins/qml/api/i18n/qmli18n.h | 33 ++++++++++++++++ src/lib/plugins/qml/qmlpluginloader.cpp | 2 + src/lib/plugins/qml/qmlplugins.cpp | 10 +++++ 5 files changed, 94 insertions(+) create mode 100644 src/lib/plugins/qml/api/i18n/qmli18n.cpp create mode 100644 src/lib/plugins/qml/api/i18n/qmli18n.h diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 1d790927f..08198b261 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -183,6 +183,7 @@ set(SRCS ${SRCS} plugins/qml/api/events/qmlmouseevent.cpp plugins/qml/api/events/qmlwheelevent.cpp plugins/qml/api/events/qmlkeyevent.cpp + plugins/qml/api/i18n/qmli18n.cpp popupwindow/popuplocationbar.cpp popupwindow/popupstatusbarmessage.cpp popupwindow/popupwebview.cpp diff --git a/src/lib/plugins/qml/api/i18n/qmli18n.cpp b/src/lib/plugins/qml/api/i18n/qmli18n.cpp new file mode 100644 index 000000000..5e6e6a2ab --- /dev/null +++ b/src/lib/plugins/qml/api/i18n/qmli18n.cpp @@ -0,0 +1,48 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 Anmol Gautam +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "qmli18n.h" +#include "qztools.h" +#include + +QmlI18n::QmlI18n(const QString &pluginName, QObject *parent) + : QObject(parent) +{ + m_pluginName = QzTools::filterCharsFromFilename(pluginName); + // QzTools::filterCharsFromFilename doesn't replaces spaces + m_pluginName.replace(QLatin1Char(' '), QLatin1Char('_')); + setlocale(LC_MESSAGES, ""); + initTranslations(); +} + +void QmlI18n::initTranslations() +{ + QString domain = QString("falkon_%1").arg(m_pluginName); + QString localeDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "locale", QStandardPaths::LocateDirectory); + bindtextdomain(domain.toUtf8(), localeDir.toUtf8()); + textdomain(domain.toUtf8()); +} + +QString QmlI18n::i18n(const QString &string) +{ + return QString::fromUtf8(gettext(string.toUtf8())); +} + +QString QmlI18n::i18np(const QString &string1, const QString &string2, int count) +{ + return QString::fromUtf8(ngettext(string1.toUtf8(), string2.toUtf8(), count)); +} diff --git a/src/lib/plugins/qml/api/i18n/qmli18n.h b/src/lib/plugins/qml/api/i18n/qmli18n.h new file mode 100644 index 000000000..6dd8cea38 --- /dev/null +++ b/src/lib/plugins/qml/api/i18n/qmli18n.h @@ -0,0 +1,33 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 Anmol Gautam +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#pragma once + +#include +#include + +class QmlI18n : public QObject +{ + Q_OBJECT +public: + explicit QmlI18n(const QString &pluginName, QObject *parent = nullptr); + void initTranslations(); + Q_INVOKABLE QString i18n(const QString &string); + Q_INVOKABLE QString i18np(const QString &string1, const QString &string2, int count); +private: + QString m_pluginName; +}; diff --git a/src/lib/plugins/qml/qmlpluginloader.cpp b/src/lib/plugins/qml/qmlpluginloader.cpp index 6a33bb6ec..dfaf80ae7 100644 --- a/src/lib/plugins/qml/qmlpluginloader.cpp +++ b/src/lib/plugins/qml/qmlpluginloader.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "qmlpluginloader.h" +#include QmlPluginLoader::QmlPluginLoader(const QString &path) { @@ -54,4 +55,5 @@ QmlPluginInterface *QmlPluginLoader::instance() const void QmlPluginLoader::setName(const QString &name) { m_interface->setName(name); + m_engine->rootContext()->setContextProperty("__name__", name); } diff --git a/src/lib/plugins/qml/qmlplugins.cpp b/src/lib/plugins/qml/qmlplugins.cpp index 05028ba87..b226f6c53 100644 --- a/src/lib/plugins/qml/qmlplugins.cpp +++ b/src/lib/plugins/qml/qmlplugins.cpp @@ -42,8 +42,10 @@ #include "api/events/qmlqzobjects.h" #include "api/events/qmlmouseevent.h" #include "api/events/qmlwheelevent.h" +#include "api/i18n/qmli18n.h" #include +#include // static void QmlPlugins::registerQmlTypes() @@ -165,4 +167,12 @@ void QmlPlugins::registerQmlTypes() // WheelEvents qmlRegisterUncreatableType("org.kde.falkon", 1, 0, "WheelEvent", "Unable to register type: WheelEvent"); + + // i18n + qmlRegisterSingletonType("org.kde.falkon", 1, 0, "I18n", [](QQmlEngine *engine, QJSEngine *scriptEngine) -> QObject * { + Q_UNUSED(scriptEngine) + QString pluginName = engine->rootContext()->contextProperty("__name__").toString(); + auto *object = new QmlI18n(pluginName); + return object; + }); }