KCalUtils

grantleeki18nlocalizer.cpp
1 /*
2  * SPDX-FileCopyrightText: 2015 Daniel Vr├ítil <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "grantleeki18nlocalizer_p.h"
9 #include "kcalutils_debug.h"
10 
11 #include <QLocale>
12 
13 #include <grantlee/safestring.h>
14 
15 #include <KLocalizedString>
16 
17 GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
18  : Grantlee::QtLocalizer()
19 {
20 }
21 
22 GrantleeKi18nLocalizer::~GrantleeKi18nLocalizer()
23 {
24 }
25 
26 QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, const QVariantList &arguments) const
27 {
28  KLocalizedString str = kstr;
29  for (auto iter = arguments.cbegin(), end = arguments.cend(); iter != end; ++iter) {
30  switch (iter->type()) {
31  case QVariant::String:
32  str = str.subs(iter->toString());
33  break;
34  case QVariant::Int:
35  str = str.subs(iter->toInt());
36  break;
37  case QVariant::UInt:
38  str = str.subs(iter->toUInt());
39  break;
40  case QVariant::LongLong:
41  str = str.subs(iter->toLongLong());
42  break;
44  str = str.subs(iter->toULongLong());
45  break;
46  case QVariant::Char:
47  str = str.subs(iter->toChar());
48  break;
49  case QVariant::Double:
50  str = str.subs(iter->toDouble());
51  break;
52  case QVariant::UserType:
53  if (iter->canConvert<Grantlee::SafeString>()) {
54  str = str.subs(iter->value<Grantlee::SafeString>().get());
55  break;
56  }
57  // fall-through
58  Q_FALLTHROUGH();
59  default:
60  qCWarning(KCALUTILS_LOG) << "Unknown type" << iter->typeName() << "(" << iter->type() << ")";
61  break;
62  }
63  }
64 
65  // Return localized in the currently active locale
66  return str.toString("libkcalutils5");
67 }
68 
69 QString GrantleeKi18nLocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
70 {
71  const KLocalizedString str = kxi18nc(qPrintable(context), qPrintable(string));
72  return processArguments(str, arguments);
73 }
74 
75 QString GrantleeKi18nLocalizer::localizeString(const QString &string, const QVariantList &arguments) const
76 {
77  const KLocalizedString str = kxi18n(qPrintable(string));
78  return processArguments(str, arguments);
79 }
80 
81 QString GrantleeKi18nLocalizer::localizePluralContextString(const QString &string, const QString &pluralForm, const QString &context, const QVariantList &arguments) const
82 {
83  const KLocalizedString str = kxi18ncp(qPrintable(context), qPrintable(string), qPrintable(pluralForm));
84  return processArguments(str, arguments);
85 }
86 
87 QString GrantleeKi18nLocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
88 {
89  const KLocalizedString str = kxi18np(qPrintable(string), qPrintable(pluralForm));
90  return processArguments(str, arguments);
91 }
92 
93 QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString &currencySymbol) const
94 {
95  return QLocale(currentLocale()).toCurrencyString(value, currencySymbol);
96 }
QString toString() const
KLocalizedString KI18N_EXPORT kxi18nc(const char *context, const char *text)
KLocalizedString subs(int a, int fieldWidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
KLocalizedString KI18N_EXPORT kxi18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT kxi18n(const char *text)
const QList< QKeySequence > & end()
QString toCurrencyString(qlonglong value, const QString &symbol) const const
KLocalizedString KI18N_EXPORT kxi18np(const char *singular, const char *plural)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 5 2020 23:10:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.