Kross

translation.cpp
1 /***************************************************************************
2  * translation.cpp
3  * This file is part of the KDE project
4  * copyright (C)2008 by Dag Andersen <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
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 GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 // Undefine this because we don't want our i18n*() method names to be turned into i18nd*()
21 #undef TRANSLATION_DOMAIN
22 
23 #include "translation.h"
24 
25 #include <QString>
26 #include <QVariant>
27 
28 #include <klocalizedstring.h>
29 #include <kpluginloader.h>
30 #include <kpluginfactory.h>
31 
32 extern "C"
33 {
34  Q_DECL_EXPORT QObject *krossmodule()
35  {
36  return new Kross::TranslationModule();
37  }
38 }
39 
40 using namespace Kross;
41 
42 namespace Kross
43 {
44 
45 /// \internal d-pointer class.
46 class TranslationModule::Private
47 {
48 };
49 }
50 
51 TranslationModule::TranslationModule()
52  : QObject()
53  , d(new Private())
54 {
55 }
56 
57 TranslationModule::~TranslationModule()
58 {
59  delete d;
60 }
61 
62 KLocalizedString TranslationModule::substituteArguments(const KLocalizedString &kls, const QVariantList &arguments, int max) const
63 {
64  KLocalizedString ls = kls;
65  int cnt = qMin(arguments.count(), max); // QString supports max 99
66  for (int i = 0; i < cnt; ++i) {
67  QVariant arg = arguments[i];
68  switch (arg.type()) {
69  case QVariant::Int: ls = ls.subs(arg.toInt()); break;
70  case QVariant::UInt: ls = ls.subs(arg.toUInt()); break;
71  case QVariant::LongLong: ls = ls.subs(arg.toLongLong()); break;
72  case QVariant::ULongLong: ls = ls.subs(arg.toULongLong()); break;
73  case QVariant::Double: ls = ls.subs(arg.toDouble()); break;
74  default: ls = ls.subs(arg.toString()); break;
75  }
76  }
77  return ls;
78 }
79 
80 QString TranslationModule::i18n(const QString &text, const QVariantList &arguments) const
81 {
82  KLocalizedString ls = ki18n(text.toUtf8().constData());
83  return substituteArguments(ls, arguments).toString();
84 }
85 
86 QString TranslationModule::i18nc(const QString &context, const QString &text, const QVariantList &arguments) const
87 {
88  KLocalizedString ls = ki18nc(context.toUtf8().constData(), text.toUtf8().constData());
89  return substituteArguments(ls, arguments).toString();
90 }
91 
92 QString TranslationModule::i18np(const QString &singular, const QString &plural, int number, const QVariantList &arguments) const
93 {
94  KLocalizedString ls = ki18np(singular.toUtf8().constData(), plural.toUtf8().constData()).subs(number);
95  return substituteArguments(ls, arguments, 98).toString();
96 }
97 
98 QString TranslationModule::i18ncp(const QString &context, const QString &singular, const QString &plural, int number, const QVariantList &arguments) const
99 {
100  KLocalizedString ls = ki18ncp(context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData()).subs(number);
101  return substituteArguments(ls, arguments, 98).toString();
102 }
103 
qlonglong toLongLong(bool *ok) const const
QString toString() const
Definition: action.cpp:36
QString i18ncp(const QString &context, const QString &singular, const QString &plural, int number, const QVariantList &arguments=QVariantList()) const
Creates localized string from a given plural and singular form dependent on number, with added context. Substitute arguments (may be empty)
Definition: translation.cpp:98
qulonglong toULongLong(bool *ok) const const
KLocalizedString subs(int a, int fieldWidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
uint toUInt(bool *ok) const const
int toInt(bool *ok) const const
const char * constData() const const
QString i18np(const QString &singular, const QString &plural, int number, const QVariantList &arguments=QVariantList()) const
Creates localized string from a given plural and singular form dependent on number. Substitute arguments (may be empty)
Definition: translation.cpp:92
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QString i18n(const QString &text, const QVariantList &arguments=QVariantList()) const
Creates localized string from a given text. Substitute arguments (may be empty)
Definition: translation.cpp:80
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18np(const char *singular, const char *plural)
QString i18nc(const QString &context, const QString &text, const QVariantList &arguments=QVariantList()) const
Creates localized string from a given text, with added context. Substitute arguments (may be empty) ...
Definition: translation.cpp:86
KLocalizedString KI18N_EXPORT ki18n(const char *text)
double toDouble(bool *ok) const const
The TranslationModule provides access to KDE translation and internationalization facilities...
Definition: translation.h:44
QVariant::Type type() const const
QString toString() const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 19 2021 23:01:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.