File indexing completed on 2024-12-22 04:41:14

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2018 Anmol Gautam <tarptaeya@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "qmli18n.h"
0019 #include "qztools.h"
0020 #include <QStandardPaths>
0021 
0022 QmlI18n::QmlI18n(const QString &pluginName, QObject *parent)
0023     : QObject(parent)
0024 {
0025     m_pluginName = pluginName;
0026     initTranslations();
0027 }
0028 
0029 void QmlI18n::initTranslations()
0030 {
0031     m_domain = QString(QSL("falkon_%1")).arg(m_pluginName);
0032     const QString localeDir = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QSL("locale"), QStandardPaths::LocateDirectory);
0033     const bool isLanguageSet = qEnvironmentVariableIsSet("LANGUAGE");
0034     const QByteArray language = qgetenv("LANGUAGE");
0035     qputenv("LANGUAGE", QLocale::system().name().toUtf8());
0036     bindtextdomain(m_domain.toUtf8().constData(), localeDir.toUtf8().constData());
0037     if (!isLanguageSet) {
0038         qunsetenv("LANGUAGE");
0039     } else {
0040         qputenv("LANGUAGE", language);
0041     }
0042 }
0043 
0044 QString QmlI18n::i18n(const QString &string)
0045 {
0046     return QString::fromUtf8(dgettext(m_domain.toUtf8().constData(), string.toUtf8().constData()));
0047 }
0048 
0049 QString QmlI18n::i18np(const QString &string1, const QString &string2, int count)
0050 {
0051     return QString::fromUtf8(dngettext(m_domain.toUtf8().constData(), string1.toUtf8().constData(), string2.toUtf8().constData(), count));
0052 }