1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-22 02:02:10 +02:00

Added i18n to QML Plugins

This commit is contained in:
Anmol Gautam 2018-06-29 03:23:18 +05:30
parent 84ad4b016a
commit 7d0141af41
5 changed files with 94 additions and 0 deletions

View File

@ -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

View File

@ -0,0 +1,48 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qmli18n.h"
#include "qztools.h"
#include <QStandardPaths>
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));
}

View File

@ -0,0 +1,33 @@
/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
* ============================================================ */
#pragma once
#include <QObject>
#include <libintl.h>
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;
};

View File

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "qmlpluginloader.h"
#include <QQmlContext>
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);
}

View File

@ -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 <QQmlEngine>
#include <QQmlContext>
// static
void QmlPlugins::registerQmlTypes()
@ -165,4 +167,12 @@ void QmlPlugins::registerQmlTypes()
// WheelEvents
qmlRegisterUncreatableType<QmlWheelEvent>("org.kde.falkon", 1, 0, "WheelEvent", "Unable to register type: WheelEvent");
// i18n
qmlRegisterSingletonType<QmlI18n>("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;
});
}