KTextTemplate

nulllocalizer.cpp
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#include "nulllocalizer_p.h"
11
12#include <QDateTime>
13
14using namespace KTextTemplate;
15
16NullLocalizer::NullLocalizer() = default;
17
18NullLocalizer::~NullLocalizer() = default;
19
20QString NullLocalizer::localizeDate(const QDate &date, QLocale::FormatType formatType) const
21{
22 Q_UNUSED(formatType)
23 return date.toString();
24}
25
26QString NullLocalizer::localizeTime(const QTime &time, QLocale::FormatType formatType) const
27{
28 Q_UNUSED(formatType)
29 return time.toString();
30}
31
32QString NullLocalizer::localizeDateTime(const QDateTime &dateTime, QLocale::FormatType formatType) const
33{
34 Q_UNUSED(formatType)
35 return dateTime.toString();
36}
37
38QString NullLocalizer::localizeNumber(int number) const
39{
40 return QString::number(number);
41}
42
43QString NullLocalizer::localizeNumber(qreal number) const
44{
45 return QString::number(number);
46}
47
48QString NullLocalizer::localizeMonetaryValue(qreal value, const QString &currencyCode) const
49{
50 Q_UNUSED(currencyCode)
51 return QString::number(value);
52}
53
54static void replacePercentN(QString *result, int n)
55{
56 if (n >= 0) {
57 auto percentPos = 0;
58 auto len = 0;
59 while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len)) != -1) {
60 len = 1;
61 QString fmt;
62 if (result->at(percentPos + len) == QLatin1Char('L')) {
63 ++len;
64 fmt = QStringLiteral("%L1");
65 } else {
66 fmt = QStringLiteral("%1");
67 }
68 if (result->at(percentPos + len) == QLatin1Char('n')) {
69 fmt = fmt.arg(n);
70 ++len;
71 result->replace(percentPos, len, fmt);
72 len = fmt.length();
73 }
74 }
75 }
76}
77
78static QString substituteArguments(const QString &input, const QVariantList &arguments)
79{
80 auto string = input;
81 for (const QVariant &arg : arguments) {
82 if (arg.userType() == qMetaTypeId<int>())
83 string = string.arg(arg.value<int>());
84 else if (arg.userType() == qMetaTypeId<double>())
85 string = string.arg(arg.value<double>());
86 else if (arg.userType() == qMetaTypeId<QDateTime>())
87 string = string.arg(arg.value<QDateTime>().toString());
88 else
89 string = string.arg(arg.value<QString>());
90 }
91 return string;
92}
93
94QString NullLocalizer::localizeContextString(const QString &string, const QString &context, const QVariantList &arguments) const
95{
96 Q_UNUSED(context)
97 return substituteArguments(string, arguments);
98}
99
101NullLocalizer::localizePluralContextString(const QString &_string, const QString &_pluralForm, const QString &context, const QVariantList &_arguments) const
102{
103 Q_UNUSED(context)
104 const auto count = _arguments.first().value<int>();
105 auto arguments = _arguments;
106 auto string = _string;
107 auto pluralForm = _pluralForm;
108 if (_string.contains(QStringLiteral("%n"))) {
109 arguments.removeFirst();
110 replacePercentN(&string, count);
111 replacePercentN(&pluralForm, count);
112 }
113 return count > 0 ? substituteArguments(pluralForm, arguments) : substituteArguments(string, arguments);
114}
115
116QString NullLocalizer::localizeString(const QString &string, const QVariantList &arguments) const
117{
118 return substituteArguments(string, arguments);
119}
120
121QString NullLocalizer::localizePluralString(const QString &_string, const QString &_pluralForm, const QVariantList &_arguments) const
122{
123 const auto count = _arguments.first().value<int>();
124 auto arguments = _arguments;
125 auto string = _string;
126 auto pluralForm = _pluralForm;
127 if (_string.contains(QStringLiteral("%n"))) {
128 arguments.removeFirst();
129 replacePercentN(&string, count);
130 replacePercentN(&pluralForm, count);
131 }
132 return count > 0 ? substituteArguments(pluralForm, arguments) : substituteArguments(string, arguments);
133}
134
135QString NullLocalizer::currentLocale() const
136{
137 return {};
138}
139
140void NullLocalizer::pushLocale(const QString &localeName)
141{
142 Q_UNUSED(localeName)
143}
144
145void NullLocalizer::popLocale()
146{
147}
148
149void NullLocalizer::loadCatalog(const QString &path, const QString &catalog)
150{
151 Q_UNUSED(path)
152 Q_UNUSED(catalog)
153}
154
155void NullLocalizer::unloadCatalog(const QString &catalog)
156{
157 Q_UNUSED(catalog)
158}
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
QString toString(QStringView format, QCalendar cal) const const
QString toString(QStringView format, QCalendar cal) const const
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString first(qsizetype n) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString toString(QStringView format) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.