Kross
translation.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * translation.cpp 00003 * This file is part of the KDE project 00004 * copyright (C)2008 by Dag Andersen <danders@get2net.dk> 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; see the file COPYING. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 ***************************************************************************/ 00019 00020 #include "translation.h" 00021 00022 #include <QString> 00023 #include <QVariant> 00024 #include <QVariantList> 00025 00026 #include <kdebug.h> 00027 #include <klocale.h> 00028 #include <klocalizedstring.h> 00029 #include <kpluginloader.h> 00030 #include <kpluginfactory.h> 00031 00032 extern "C" 00033 { 00034 KDE_EXPORT QObject* krossmodule() 00035 { 00036 return new Kross::TranslationModule(); 00037 } 00038 } 00039 00040 using namespace Kross; 00041 00042 00043 namespace Kross { 00044 00046 class TranslationModule::Private 00047 { 00048 }; 00049 } 00050 00051 TranslationModule::TranslationModule() 00052 : QObject() 00053 , d( new Private() ) 00054 { 00055 } 00056 00057 TranslationModule::~TranslationModule() 00058 { 00059 delete d; 00060 } 00061 00062 KLocalizedString TranslationModule::substituteArguments( const KLocalizedString &kls, const QVariantList &arguments, int max ) const 00063 { 00064 KLocalizedString ls = kls; 00065 int cnt = qMin( arguments.count(), max ); // QString supports max 99 00066 for ( int i = 0; i < cnt; ++i ) { 00067 QVariant arg = arguments[i]; 00068 switch ( arg.type() ) { 00069 case QVariant::Int: ls = ls.subs(arg.toInt()); break; 00070 case QVariant::UInt: ls = ls.subs(arg.toUInt()); break; 00071 case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break; 00072 case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break; 00073 case QVariant::Double: ls = ls.subs(arg.toDouble()); break; 00074 default: ls = ls.subs(arg.toString()); break; 00075 } 00076 } 00077 return ls; 00078 } 00079 00080 QString TranslationModule::i18n( const QString &text, const QVariantList &arguments ) const 00081 { 00082 KLocalizedString ls = ki18n(text.toUtf8()); 00083 return substituteArguments( ls, arguments ).toString(); 00084 } 00085 00086 QString TranslationModule::i18nc( const QString &context, const QString &text, const QVariantList &arguments ) const 00087 { 00088 KLocalizedString ls = ki18nc(context.toUtf8(), text.toUtf8()); 00089 return substituteArguments( ls, arguments ).toString(); 00090 } 00091 00092 QString TranslationModule::i18np( const QString &singular, const QString &plural, int number, const QVariantList &arguments ) const 00093 { 00094 KLocalizedString ls = ki18np(singular.toUtf8(), plural.toUtf8()).subs(number); 00095 return substituteArguments( ls, arguments, 98 ).toString(); 00096 } 00097 00098 QString TranslationModule::i18ncp( const QString &context, const QString &singular, const QString &plural, int number, const QVariantList &arguments ) const 00099 { 00100 KLocalizedString ls = ki18ncp(context.toUtf8(), singular.toUtf8(), plural.toUtf8()).subs( number ); 00101 return substituteArguments( ls, arguments, 98 ).toString(); 00102 } 00103 00104 #include "translation.moc"
KDE 4.6 API Reference