GrantleeTheme

grantleeki18nlocalizer.cpp
1/*
2 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
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 <KTextTemplate/SafeString>
12#include <QDebug>
13
14#include <KLocalizedString>
15using namespace GrantleeTheme;
16
17GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
18 : KTextTemplate::QtLocalizer()
19{
20}
21
22GrantleeKi18nLocalizer::~GrantleeKi18nLocalizer() = default;
23
24QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, const QVariantList &arguments) const
25{
26 KLocalizedString str = kstr;
27 for (auto iter = arguments.cbegin(), end = arguments.cend(); iter != end; ++iter) {
28 switch (iter->userType()) {
30 str = str.subs(iter->toString());
31 break;
32 case QMetaType::Int:
33 str = str.subs(iter->toInt());
34 break;
35 case QMetaType::UInt:
36 str = str.subs(iter->toUInt());
37 break;
39 str = str.subs(iter->toLongLong());
40 break;
42 str = str.subs(iter->toULongLong());
43 break;
44 case QMetaType::Char:
45 str = str.subs(iter->toChar());
46 break;
48 str = str.subs(iter->toDouble());
49 break;
50 case QMetaType::User:
51 default:
52 if (iter->canConvert<KTextTemplate::SafeString>()) {
53 str = str.subs(iter->value<KTextTemplate::SafeString>().get());
54 break;
55 } else {
56 qCWarning(GRANTLEETHEME_LOG) << "Unknown type" << iter->typeName() << "(" << iter->userType() << ")";
57 break;
58 }
59 }
60 }
61
62 // Return localized in the currenctly active locale
63 const QString translatedStr = str.toString(mApplicationDomain.isEmpty() ? str.applicationDomain().constData() : mApplicationDomain.constData());
64 // qDebug() << " translatedStr"<< translatedStr << " domain :" << str.applicationDomain() << " currentLocale() "<< currentLocale() << " Specific
65 // applicationDomain" << mApplicationDomain;
66 return translatedStr;
67}
68
69QString GrantleeKi18nLocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
70{
71 const KLocalizedString str = ki18nc(qPrintable(context), qPrintable(string));
72 return processArguments(str, arguments);
73}
74
75QString GrantleeKi18nLocalizer::localizeString(const QString &string, const QVariantList &arguments) const
76{
77 const KLocalizedString str = ki18n(qPrintable(string));
78 return processArguments(str, arguments);
79}
80
81QString GrantleeKi18nLocalizer::localizePluralContextString(const QString &string,
82 const QString &pluralForm,
83 const QString &context,
84 const QVariantList &arguments) const
85{
86 const KLocalizedString str = ki18ncp(qPrintable(context), qPrintable(string), qPrintable(pluralForm));
87 return processArguments(str, arguments);
88}
89
90QString GrantleeKi18nLocalizer::localizePluralString(const QString &string, const QString &pluralForm, const QVariantList &arguments) const
91{
92 const KLocalizedString str = ki18np(qPrintable(string), qPrintable(pluralForm));
93 return processArguments(str, arguments);
94}
95
96QString GrantleeKi18nLocalizer::localizeMonetaryValue(qreal value, const QString &currencySymbol) const
97{
98 return QLocale(currentLocale()).toCurrencyString(value, currencySymbol);
99}
100
101QString GrantleeKi18nLocalizer::currentLocale() const
102{
104 const int f = locale.indexOf(QLatin1Char('_'));
105 if (f >= 0) {
106 locale.truncate(f);
107 }
108 return locale;
109}
110
111void GrantleeKi18nLocalizer::setApplicationDomain(const QByteArray &domain)
112{
113 mApplicationDomain = domain;
114}
static QByteArray applicationDomain()
QString toString() const
KLocalizedString subs(const KLocalizedString &a, int fieldWidth=0, QChar fillChar=QLatin1Char(' ')) const
QString currentLocale() const override
KLocalizedString KI18N_EXPORT ki18np(const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18n(const char *text)
KLocalizedString KI18N_EXPORT ki18ncp(const char *context, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18nc(const char *context, const char *text)
const char * constData() const const
bool isEmpty() const const
QString toCurrencyString(double value, const QString &symbol, int precision) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
void truncate(qsizetype position)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.