GrantleeTheme

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.h"
9 #include "grantleetheme_debug.h"
10 
11 #include <QDebug>
12 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
13 #include <grantlee/safestring.h>
14 #else
15 #include <KTextTemplate/safestring.h>
16 #endif
17 
18 #include <KLocalizedString>
19 using namespace GrantleeTheme;
20 
21 GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
22 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
23  : Grantlee::QtLocalizer()
24 #else
25  : KTextTemplate::QtLocalizer()
26 #endif
27 {
28 }
29 
30 GrantleeKi18nLocalizer::~GrantleeKi18nLocalizer() = default;
31 
32 QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, const QVariantList &arguments) const
33 {
34  KLocalizedString str = kstr;
35  for (auto iter = arguments.cbegin(), end = arguments.cend(); iter != end; ++iter) {
36  switch (iter->type()) {
37  case QVariant::String:
38  str = str.subs(iter->toString());
39  break;
40  case QVariant::Int:
41  str = str.subs(iter->toInt());
42  break;
43  case QVariant::UInt:
44  str = str.subs(iter->toUInt());
45  break;
46  case QVariant::LongLong:
47  str = str.subs(iter->toLongLong());
48  break;
50  str = str.subs(iter->toULongLong());
51  break;
52  case QVariant::Char:
53  str = str.subs(iter->toChar());
54  break;
55  case QVariant::Double:
56  str = str.subs(iter->toDouble());
57  break;
58  case QVariant::UserType:
59 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
60  if (iter->canConvert<Grantlee::SafeString>()) {
61  str = str.subs(iter->value<Grantlee::SafeString>().get());
62  break;
63  }
64 #else
65  if (iter->canConvert<KTextTemplate::SafeString>()) {
66  str = str.subs(iter->value<KTextTemplate::SafeString>().get());
67  break;
68  }
69 #endif
70  // fall-through
71  default:
72  qCWarning(GRANTLEETHEME_LOG) << "Unknown type" << iter->typeName() << "(" << iter->type() << ")";
73  break;
74  }
75  }
76 
77  // Return localized in the currenctly active locale
78  const QString translatedStr = str.toString(mApplicationDomain.isEmpty() ? str.applicationDomain().constData() : mApplicationDomain.constData());
79  // qDebug() << " translatedStr"<< translatedStr << " domain :" << str.applicationDomain() << " currentLocale() "<< currentLocale() << " Specific
80  // 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,
97  const QString &pluralForm,
98  const QString &context,
99  const QVariantList &arguments) const
100 {
101  const KLocalizedString str = ki18ncp(qPrintable(context), qPrintable(string), qPrintable(pluralForm));
102  return processArguments(str, arguments);
103 }
104 
105 QString GrantleeKi18nLocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
106 {
107  const KLocalizedString str = ki18np(qPrintable(string), qPrintable(pluralForm));
108  return processArguments(str, arguments);
109 }
110 
111 QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString &currencySymbol) const
112 {
113  return QLocale(currentLocale()).toCurrencyString(value, currencySymbol);
114 }
115 
116 QString GrantleeKi18nLocalizer::currentLocale() const
117 {
118 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
119  QString locale = Grantlee::QtLocalizer::currentLocale();
120 #else
121  QString locale = KTextTemplate::QtLocalizer::currentLocale();
122 #endif
123  const int f = locale.indexOf(QLatin1Char('_'));
124  if (f >= 0) {
125  locale.truncate(f);
126  }
127  return locale;
128 }
129 
130 void GrantleeKi18nLocalizer::setApplicationDomain(const QByteArray &domain)
131 {
132  mApplicationDomain = domain;
133 }
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
QString toCurrencyString(qlonglong value, const QString &symbol) const const
QString toString() const
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18n(const char *text)
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
LocaleWrapper locale()
bool isEmpty() const const
const char * constData() const const
KLocalizedString KI18N_EXPORT ki18np(const char *singular, const char *plural)
static QByteArray applicationDomain()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Sep 28 2022 04:06:59 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.