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

QmlPlugins: Port to KI18n

This commit is contained in:
David Rosca 2019-04-22 14:31:48 +02:00
parent 3f37ada906
commit 0d3fea1c93
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
7 changed files with 8 additions and 132 deletions

View File

@ -118,11 +118,6 @@ if (PySide2_FOUND AND Shiboken2_FOUND AND Python3_FOUND)
set(ENABLE_PYTHON_PLUGINS TRUE) set(ENABLE_PYTHON_PLUGINS TRUE)
endif() endif()
find_package(Intl)
if (Intl_FOUND)
set(HAVE_LIBINTL TRUE)
endif()
# Git revision # Git revision
if (EXISTS "${CMAKE_SOURCE_DIR}/.git") if (EXISTS "${CMAKE_SOURCE_DIR}/.git")
find_package(Git QUIET) find_package(Git QUIET)

View File

@ -9,5 +9,3 @@
/* Disable DBus support */ /* Disable DBus support */
#cmakedefine DISABLE_DBUS #cmakedefine DISABLE_DBUS
#cmakedefine01 HAVE_LIBINTL

View File

@ -268,10 +268,6 @@ set(SRCS ${SRCS}
webtab/webtab.cpp webtab/webtab.cpp
) )
if (HAVE_LIBINTL)
set(SRCS ${SRCS} plugins/qml/api/i18n/qmli18n.cpp)
endif()
if (WIN32) if (WIN32)
set(SRCS ${SRCS} other/registerqappassociation.cpp) set(SRCS ${SRCS} other/registerqappassociation.cpp)
endif() endif()

View File

@ -1,52 +0,0 @@
/* ============================================================
* 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 = pluginName;
initTranslations();
}
void QmlI18n::initTranslations()
{
m_domain = QString(QSL("falkon_%1")).arg(m_pluginName);
const QString localeDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSL("locale"), QStandardPaths::LocateDirectory);
const bool isLanguageSet = qEnvironmentVariableIsSet("LANGUAGE");
const QByteArray language = qgetenv("LANGUAGE");
qputenv("LANGUAGE", QLocale::system().name().toUtf8());
bindtextdomain(m_domain.toUtf8(), localeDir.toUtf8());
if (!isLanguageSet) {
qunsetenv("LANGUAGE");
} else {
qputenv("LANGUAGE", language);
}
}
QString QmlI18n::i18n(const QString &string)
{
return QString::fromUtf8(dgettext(m_domain.toUtf8(), string.toUtf8()));
}
QString QmlI18n::i18np(const QString &string1, const QString &string2, int count)
{
return QString::fromUtf8(dngettext(m_domain.toUtf8(), string1.toUtf8(), string2.toUtf8(), count));
}

View File

@ -1,46 +0,0 @@
/* ============================================================
* 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>
extern "C" {
#include <libintl.h>
}
/**
* @brief The class exposing GNU Gettext to QML
*/
class QmlI18n : public QObject
{
Q_OBJECT
public:
explicit QmlI18n(const QString &pluginName, QObject *parent = nullptr);
void initTranslations();
/**
* @brief wrapper for gettext function
*/
Q_INVOKABLE QString i18n(const QString &string);
/**
* @brief wrapper for ngettext function
*/
Q_INVOKABLE QString i18np(const QString &string1, const QString &string2, int count);
private:
QString m_pluginName;
QString m_domain;
};

View File

@ -17,13 +17,12 @@
* ============================================================ */ * ============================================================ */
#include "qmlpluginloader.h" #include "qmlpluginloader.h"
#include "qmlengine.h" #include "qmlengine.h"
#include <QQmlContext>
#include <QDir>
#include "../config.h" #include "../config.h"
#if HAVE_LIBINTL #include <QDir>
#include "qml/api/i18n/qmli18n.h" #include <QQmlContext>
#endif
#include <KLocalizedContext>
QmlPluginLoader::QmlPluginLoader(const QString &name, const QString &path) QmlPluginLoader::QmlPluginLoader(const QString &name, const QString &path)
{ {
@ -62,16 +61,10 @@ QmlPluginInterface *QmlPluginLoader::instance() const
void QmlPluginLoader::initEngineAndComponent() void QmlPluginLoader::initEngineAndComponent()
{ {
m_engine = new QmlEngine(); m_engine = new QmlEngine();
KLocalizedContext *context = new KLocalizedContext(this);
context->setTranslationDomain(QSL("falkon_%1").arg(m_name));
m_engine->rootContext()->setContextObject(context);
m_component = new QQmlComponent(m_engine, QDir(m_path).filePath(QStringLiteral("main.qml"))); m_component = new QQmlComponent(m_engine, QDir(m_path).filePath(QStringLiteral("main.qml")));
m_engine->setExtensionPath(m_path); m_engine->setExtensionPath(m_path);
m_engine->setExtensionName(m_name); m_engine->setExtensionName(m_name);
#if HAVE_LIBINTL
auto i18n = new QmlI18n(m_name);
m_engine->globalObject().setProperty(QSL("__falkon_i18n"), m_engine->newQObject(i18n));
m_engine->evaluate(QSL("i18n = function (s) { return __falkon_i18n.i18n(s) };"));
m_engine->evaluate(QSL("i18np = function (s1, s2) { return __falkon_i18n.i18np(s1, s2) }"));
#else
m_engine->evaluate(QSL("i18n = function (s) { return s; };"));
m_engine->evaluate(QSL("i18np = function (s1, s2) { return s1; }"));
#endif
} }

View File

@ -1,11 +1,3 @@
#! /bin/sh #! /bin/sh
XGETTEXT_FLAGS_QML="\ $XGETTEXT `find . -name '*.js' -o -name '*.qml'` -o $podir/falkon_helloqml.pot
--copyright-holder=This_file_is_part_of_KDE \
--msgid-bugs-address=http://bugs.kde.org \
--from-code=UTF-8 \
-L javascript \
-ki18n:1 -ki18np:1,2 \
"
$XGETTEXT_PROGRAM $XGETTEXT_FLAGS_QML `find . -name '*.qml' -o -name '*.js'` -o $podir/falkon_helloqml.pot