File indexing completed on 2024-04-14 14:27:10

0001 /***************************************************************************
0002  * translation.cpp
0003  * This file is part of the KDE project
0004  * copyright (C)2008 by Dag Andersen <danders@get2net.dk>
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
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 GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 // Undefine this because we don't want our i18n*() method names to be turned into i18nd*()
0021 #undef TRANSLATION_DOMAIN
0022 
0023 #include "translation.h"
0024 
0025 #include <QString>
0026 #include <QVariant>
0027 
0028 #include <klocalizedstring.h>
0029 #include <kpluginloader.h>
0030 #include <kpluginfactory.h>
0031 
0032 extern "C"
0033 {
0034     Q_DECL_EXPORT QObject *krossmodule()
0035     {
0036         return new Kross::TranslationModule();
0037     }
0038 }
0039 
0040 using namespace Kross;
0041 
0042 namespace Kross
0043 {
0044 
0045 /// \internal d-pointer class.
0046 class TranslationModule::Private
0047 {
0048 };
0049 }
0050 
0051 TranslationModule::TranslationModule()
0052     : QObject()
0053     , d(new Private())
0054 {
0055 }
0056 
0057 TranslationModule::~TranslationModule()
0058 {
0059     delete d;
0060 }
0061 
0062 KLocalizedString TranslationModule::substituteArguments(const KLocalizedString &kls, const QVariantList &arguments, int max) const
0063 {
0064     KLocalizedString ls = kls;
0065     int cnt = qMin(arguments.count(), max);   // QString supports max 99
0066     for (int i = 0; i < cnt; ++i) {
0067         QVariant arg = arguments[i];
0068         switch (arg.type()) {
0069         case QVariant::Int: ls = ls.subs(arg.toInt()); break;
0070         case QVariant::UInt: ls = ls.subs(arg.toUInt()); break;
0071         case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break;
0072         case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break;
0073         case QVariant::Double: ls = ls.subs(arg.toDouble()); break;
0074         default: ls = ls.subs(arg.toString()); break;
0075         }
0076     }
0077     return ls;
0078 }
0079 
0080 QString TranslationModule::i18n(const QString &text, const QVariantList &arguments) const
0081 {
0082     KLocalizedString ls = ki18n(text.toUtf8().constData());
0083     return substituteArguments(ls, arguments).toString();
0084 }
0085 
0086 QString TranslationModule::i18nc(const QString &context, const QString &text, const QVariantList &arguments) const
0087 {
0088     KLocalizedString ls = ki18nc(context.toUtf8().constData(), text.toUtf8().constData());
0089     return substituteArguments(ls, arguments).toString();
0090 }
0091 
0092 QString TranslationModule::i18np(const QString &singular, const QString &plural, int number, const QVariantList &arguments) const
0093 {
0094     KLocalizedString ls = ki18np(singular.toUtf8().constData(), plural.toUtf8().constData()).subs(number);
0095     return substituteArguments(ls, arguments, 98).toString();
0096 }
0097 
0098 QString TranslationModule::i18ncp(const QString &context, const QString &singular, const QString &plural,  int number, const QVariantList &arguments) const
0099 {
0100     KLocalizedString ls = ki18ncp(context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData()).subs(number);
0101     return substituteArguments(ls, arguments, 98).toString();
0102 }
0103 
0104 #include "moc_translation.cpp"