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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
13 #include <grantlee/safestring.h>
14 #else
15 #include <KTextTemplate/SafeString>
16 #endif
17 
18 #include <KLocalizedString>
19 
20 GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
21 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
22  : Grantlee::QtLocalizer()
23 #else
25 #endif
26 {
27 }
28 
29 GrantleeKi18nLocalizer::~GrantleeKi18nLocalizer()
30 {
31 }
32 
33 QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, const QVariantList &arguments) const
34 {
35  KLocalizedString str = kstr;
36  for (auto iter = arguments.cbegin(), end = arguments.cend(); iter != end; ++iter) {
37 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
38  switch (iter->type()) {
39  case QVariant::String:
40  str = str.subs(iter->toString());
41  break;
42  case QVariant::Int:
43  str = str.subs(iter->toInt());
44  break;
45  case QVariant::UInt:
46  str = str.subs(iter->toUInt());
47  break;
48  case QVariant::LongLong:
49  str = str.subs(iter->toLongLong());
50  break;
52  str = str.subs(iter->toULongLong());
53  break;
54  case QVariant::Char:
55  str = str.subs(iter->toChar());
56  break;
57  case QVariant::Double:
58  str = str.subs(iter->toDouble());
59  break;
60  case QVariant::UserType:
61  if (iter->canConvert<Grantlee::SafeString>()) {
62  str = str.subs(iter->value<Grantlee::SafeString>().get());
63  break;
64  }
65  // fall-through
66  Q_FALLTHROUGH();
67  default:
68  qCWarning(KCALUTILS_LOG) << "Unknown type" << iter->typeName() << "(" << iter->type() << ")";
69  break;
70  }
71 #else
72  switch (iter->userType()) {
73  case QMetaType::QString:
74  str = str.subs(iter->toString());
75  break;
76  case QMetaType::Int:
77  str = str.subs(iter->toInt());
78  break;
79  case QMetaType::UInt:
80  str = str.subs(iter->toUInt());
81  break;
83  str = str.subs(iter->toLongLong());
84  break;
86  str = str.subs(iter->toULongLong());
87  break;
88  case QMetaType::Char:
89  str = str.subs(iter->toChar());
90  break;
91  case QMetaType::Double:
92  str = str.subs(iter->toDouble());
93  break;
94  case QMetaType::User:
95  if (iter->canConvert<KTextTemplate::SafeString>()) {
96  str = str.subs(iter->value<KTextTemplate::SafeString>().get());
97  break;
98  }
99  // fall-through
100  Q_FALLTHROUGH();
101  default:
102  qCWarning(KCALUTILS_LOG) << "Unknown type" << iter->typeName() << "(" << iter->userType() << ")";
103  break;
104  }
105 #endif
106  }
107 
108  // Return localized in the currently active locale
109  return str.toString("libkcalutils5");
110 }
111 
112 QString GrantleeKi18nLocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
113 {
114  const KLocalizedString str = kxi18nc(qPrintable(context), qPrintable(string));
115  return processArguments(str, arguments);
116 }
117 
118 QString GrantleeKi18nLocalizer::localizeString(const QString &string, const QVariantList &arguments) const
119 {
120  const KLocalizedString str = kxi18n(qPrintable(string));
121  return processArguments(str, arguments);
122 }
123 
124 QString GrantleeKi18nLocalizer::localizePluralContextString(const QString &string,
125  const QString &pluralForm,
126  const QString &context,
127  const QVariantList &arguments) const
128 {
129  const KLocalizedString str = kxi18ncp(qPrintable(context), qPrintable(string), qPrintable(pluralForm));
130  return processArguments(str, arguments);
131 }
132 
133 QString GrantleeKi18nLocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
134 {
135  const KLocalizedString str = kxi18np(qPrintable(string), qPrintable(pluralForm));
136  return processArguments(str, arguments);
137 }
138 
139 QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString &currencySymbol) const
140 {
141  return QLocale(currentLocale()).toCurrencyString(value, currencySymbol);
142 }
KLocalizedString KI18N_EXPORT kxi18n(const char *text)
QString toCurrencyString(qlonglong value, const QString &symbol) const const
QString toString() const
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
KLocalizedString KI18N_EXPORT kxi18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT kxi18np(const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT kxi18nc(const char *context, const char *text)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:09:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.