From 36b3e611bdcb604198aced71b6733a4aafe99895 Mon Sep 17 00:00:00 2001 From: Pierre Lamot <pierre@videolabs.io> Date: Mon, 2 Dec 2019 15:39:43 +0100 Subject: [PATCH] qml: add support for i18n through vlc_gettext this way, qml files can be translated using vlc usual tools rather than qt own tools Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org> --- modules/gui/qt/Makefile.am | 2 + .../gui/qt/maininterface/main_interface.cpp | 3 ++ modules/gui/qt/util/i18n.cpp | 45 +++++++++++++++++++ modules/gui/qt/util/i18n.hpp | 43 ++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 modules/gui/qt/util/i18n.cpp create mode 100644 modules/gui/qt/util/i18n.hpp diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am index ce74bb989e44..47ddf08afbe1 100644 --- a/modules/gui/qt/Makefile.am +++ b/modules/gui/qt/Makefile.am @@ -161,6 +161,7 @@ libqt_plugin_la_SOURCES = \ gui/qt/util/audio_device_model.cpp \ gui/qt/util/audio_device_model.hpp \ gui/qt/util/imagehelper.cpp gui/qt/util/imagehelper.hpp \ + gui/qt/util/i18n.cpp gui/qt/util/i18n.hpp \ gui/qt/util/navigation_history.cpp gui/qt/util/navigation_history.hpp \ gui/qt/util/qml_main_context.cpp \ gui/qt/util/qml_main_context.hpp \ @@ -286,6 +287,7 @@ nodist_libqt_plugin_la_SOURCES = \ gui/qt/playlist/playlist_item.moc.cpp \ gui/qt/playlist/playlist_model.moc.cpp \ gui/qt/util/audio_device_model.moc.cpp \ + gui/qt/util/i18n.moc.cpp \ gui/qt/util/navigation_history.moc.cpp \ gui/qt/util/qml_main_context.moc.cpp \ gui/qt/util/qmleventfilter.moc.cpp \ diff --git a/modules/gui/qt/maininterface/main_interface.cpp b/modules/gui/qt/maininterface/main_interface.cpp index 9eea5706fcc5..f843bab77b0e 100644 --- a/modules/gui/qt/maininterface/main_interface.cpp +++ b/modules/gui/qt/maininterface/main_interface.cpp @@ -68,6 +68,7 @@ #include "util/qml_main_context.hpp" #include "util/qmleventfilter.hpp" +#include "util/i18n.hpp" #include "menus/menus.hpp" // Menu creation @@ -386,6 +387,7 @@ void MainInterface::createMainWidget( QSettings * ) mediacenterView = new QQuickWidget(this); mediacenterView->setClearColor(Qt::transparent); + I18n* i18n = new I18n(this); NavigationHistory* navigation_history = new NavigationHistory(mediacenterView); QmlMainContext* mainCtx = new QmlMainContext(p_intf, this, mediacenterView); @@ -395,6 +397,7 @@ void MainInterface::createMainWidget( QSettings * ) rootCtx->setContextProperty( "history", navigation_history ); rootCtx->setContextProperty( "player", p_intf->p_sys->p_mainPlayerController ); + rootCtx->setContextProperty( "i18n", i18n ); rootCtx->setContextProperty( "mainctx", mainCtx); rootCtx->setContextProperty( "rootQMLView", mediacenterView); rootCtx->setContextProperty( "rootWindow", this); diff --git a/modules/gui/qt/util/i18n.cpp b/modules/gui/qt/util/i18n.cpp new file mode 100644 index 000000000000..ab3ff12fb4b7 --- /dev/null +++ b/modules/gui/qt/util/i18n.cpp @@ -0,0 +1,45 @@ +/***************************************************************************** + * Access to vlc_gettext from QML + **************************************************************************** + * Copyright (C) 2019 the VideoLAN team + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "i18n.hpp" +#include <vlc_common.h> +#include <QDebug> + +#ifdef qtr +#undef qtr +#endif + +I18n::I18n(QObject *parent) + : QObject(parent) +{ +} + +QString I18n::qtr(const QString msgid) const +{ + //we need msgIdUtf8 to stay valid for the whole scope, + //as vlc_gettext may return the incoming pointer + QByteArray msgIdUtf8 = msgid.toUtf8(); + const char * msgstr_c = vlc_gettext(msgIdUtf8.constData()); + return QString::fromUtf8( msgstr_c ); +} diff --git a/modules/gui/qt/util/i18n.hpp b/modules/gui/qt/util/i18n.hpp new file mode 100644 index 000000000000..51c38b4a5678 --- /dev/null +++ b/modules/gui/qt/util/i18n.hpp @@ -0,0 +1,43 @@ +/***************************************************************************** + * Access to vlc_gettext from QML + **************************************************************************** + * Copyright (C) 2019 the VideoLAN team + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + *****************************************************************************/ + +#ifndef I18N_HPP +#define I18N_HPP + +#include <QString> +#include <QObject> + +class I18n : public QObject +{ + Q_OBJECT +public: + I18n(QObject* parent = nullptr); +public: +#ifdef qtr +#undef qtr + Q_INVOKABLE QString qtr(QString msgid) const; +#define qtr(i) QString::fromUtf8( vlc_gettext(i) ) +#else + Q_INVOKABLE QString qtr(const QString msgid) const; +#endif + +}; + +#endif // I18N_HPP -- GitLab