GrantleeTheme

grantleeki18nlocalizer.cpp
1 /*
2  * Copyright (C) 2015 Daniel Vr├ítil <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  */
19 
20 #include "grantleeki18nlocalizer.h"
21 #include "grantleetheme_debug.h"
22 
23 #include <QLocale>
24 #include <QDebug>
25 
26 #include <grantlee/safestring.h>
27 
28 #include <KLocalizedString>
29 using namespace GrantleeTheme;
30 
31 GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
32  : Grantlee::QtLocalizer()
33 {
34 }
35 
36 GrantleeKi18nLocalizer::~GrantleeKi18nLocalizer()
37 {
38 }
39 
40 QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, const QVariantList &arguments) const
41 {
42  KLocalizedString str = kstr;
43  for (auto iter = arguments.cbegin(), end = arguments.cend(); iter != end; ++iter) {
44  switch (iter->type()) {
45  case QVariant::String:
46  str = str.subs(iter->toString());
47  break;
48  case QVariant::Int:
49  str = str.subs(iter->toInt());
50  break;
51  case QVariant::UInt:
52  str = str.subs(iter->toUInt());
53  break;
54  case QVariant::LongLong:
55  str = str.subs(iter->toLongLong());
56  break;
58  str = str.subs(iter->toULongLong());
59  break;
60  case QVariant::Char:
61  str = str.subs(iter->toChar());
62  break;
63  case QVariant::Double:
64  str = str.subs(iter->toDouble());
65  break;
66  case QVariant::UserType:
67  if (iter->canConvert<Grantlee::SafeString>()) {
68  str = str.subs(iter->value<Grantlee::SafeString>().get());
69  break;
70  }
71  // fall-through
72  default:
73  qCWarning(GRANTLEETHEME_LOG) << "Unknown type" << iter->typeName() << "(" << iter->type() << ")";
74  break;
75  }
76  }
77 
78  // Return localized in the currenctly active locale
79  const QString translatedStr = str.toString(mApplicationDomain.isEmpty() ? str.applicationDomain().constData() : mApplicationDomain.constData());
80  //qDebug() << " translatedStr"<< translatedStr << " domain :" << str.applicationDomain() << " currentLocale() "<< currentLocale() << " Specific applicationDomain" << mApplicationDomain;
81  return translatedStr;
82 }
83 
84 QString GrantleeKi18nLocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
85 {
86  const KLocalizedString str = ki18nc(qPrintable(context), qPrintable(string));
87  return processArguments(str, arguments);
88 }
89 
90 QString GrantleeKi18nLocalizer::localizeString(const QString &string, const QVariantList &arguments) const
91 {
92  const KLocalizedString str = ki18n(qPrintable(string));
93  return processArguments(str, arguments);
94 }
95 
96 QString GrantleeKi18nLocalizer::localizePluralContextString(const QString &string, const QString &pluralForm, const QString &context, const QVariantList &arguments) const
97 {
98  const KLocalizedString str = ki18ncp(qPrintable(context), qPrintable(string), qPrintable(pluralForm));
99  return processArguments(str, arguments);
100 }
101 
102 QString GrantleeKi18nLocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
103 {
104  const KLocalizedString str = ki18np(qPrintable(string), qPrintable(pluralForm));
105  return processArguments(str, arguments);
106 }
107 
108 QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString &currencySymbol) const
109 {
110  return QLocale(currentLocale()).toCurrencyString(value, currencySymbol);
111 }
112 
113 QString GrantleeKi18nLocalizer::currentLocale() const
114 {
115  QString locale = Grantlee::QtLocalizer::currentLocale();
116  const int f = locale.indexOf(QLatin1Char('_'));
117  if (f >= 0) {
118  locale.truncate(f);
119  }
120  return locale;
121 }
122 
123 void GrantleeKi18nLocalizer::setApplicationDomain(const QByteArray &domain)
124 {
125  mApplicationDomain = domain;
126 }
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
void truncate(int position)
QString toString() const
KLocalizedString subs(int a, int fieldWidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
const char * constData() const const
static QByteArray applicationDomain()
const QList< QKeySequence > & end()
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18np(const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18n(const char *text)
QString toCurrencyString(qlonglong value, const QString &symbol) const const
LocaleWrapper locale()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Aug 6 2020 23:15:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.