Falkon Develop
Cross-platform Qt-based web browser
qmli18n.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "qmli18n.h"
19#include "qztools.h"
20#include <QStandardPaths>
21
22QmlI18n::QmlI18n(const QString &pluginName, QObject *parent)
23 : QObject(parent)
24{
25 m_pluginName = pluginName;
27}
28
30{
31 m_domain = QString(QSL("falkon_%1")).arg(m_pluginName);
32 const QString localeDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSL("locale"), QStandardPaths::LocateDirectory);
33 const bool isLanguageSet = qEnvironmentVariableIsSet("LANGUAGE");
34 const QByteArray language = qgetenv("LANGUAGE");
35 qputenv("LANGUAGE", QLocale::system().name().toUtf8());
36 bindtextdomain(m_domain.toUtf8().constData(), localeDir.toUtf8().constData());
37 if (!isLanguageSet) {
38 qunsetenv("LANGUAGE");
39 } else {
40 qputenv("LANGUAGE", language);
41 }
42}
43
44QString QmlI18n::i18n(const QString &string)
45{
46 return QString::fromUtf8(dgettext(m_domain.toUtf8().constData(), string.toUtf8().constData()));
47}
48
49QString QmlI18n::i18np(const QString &string1, const QString &string2, int count)
50{
51 return QString::fromUtf8(dngettext(m_domain.toUtf8().constData(), string1.toUtf8().constData(), string2.toUtf8().constData(), count));
52}
Q_INVOKABLE QString i18n(const QString &string)
wrapper for gettext function
Definition: qmli18n.cpp:44
Q_INVOKABLE QString i18np(const QString &string1, const QString &string2, int count)
wrapper for ngettext function
Definition: qmli18n.cpp:49
QmlI18n(const QString &pluginName, QObject *parent=nullptr)
Definition: qmli18n.cpp:22
void initTranslations()
Definition: qmli18n.cpp:29
#define QSL(x)
Definition: qzcommon.h:40