Cutelee  6.1.0
nulllocalizer.cpp
1 /*
2  This file is part of the Cutelee template system.
3 
4  Copyright (c) 2010 Stephen Kelly <steveire@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either version
9  2.1 of the Licence, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #include "nulllocalizer_p.h"
22 
23 #include <QtCore/QDateTime>
24 
25 using namespace Cutelee;
26 
27 NullLocalizer::NullLocalizer() {}
28 
29 NullLocalizer::~NullLocalizer() {}
30 
31 QString NullLocalizer::localizeDate(const QDate &date,
32  QLocale::FormatType formatType) const
33 {
34  Q_UNUSED(formatType)
35  return date.toString();
36 }
37 
38 QString NullLocalizer::localizeTime(const QTime &time,
39  QLocale::FormatType formatType) const
40 {
41  Q_UNUSED(formatType)
42  return time.toString();
43 }
44 
45 QString NullLocalizer::localizeDateTime(const QDateTime &dateTime,
46  QLocale::FormatType formatType) const
47 {
48  Q_UNUSED(formatType)
49  return dateTime.toString();
50 }
51 
52 QString NullLocalizer::localizeNumber(int number) const
53 {
54  return QString::number(number);
55 }
56 
57 QString NullLocalizer::localizeNumber(qreal number) const
58 {
59  return QString::number(number);
60 }
61 
62 QString NullLocalizer::localizeMonetaryValue(qreal value,
63  const QString &currencyCode) const
64 {
65  Q_UNUSED(currencyCode)
66  return QString::number(value);
67 }
68 
69 static void replacePercentN(QString *result, int n)
70 {
71  if (n >= 0) {
72  auto percentPos = 0;
73  auto len = 0;
74  while ((percentPos = result->indexOf(QLatin1Char('%'), percentPos + len))
75  != -1) {
76  len = 1;
77  QString fmt;
78  if (result->at(percentPos + len) == QLatin1Char('L')) {
79  ++len;
80  fmt = QStringLiteral("%L1");
81  } else {
82  fmt = QStringLiteral("%1");
83  }
84  if (result->at(percentPos + len) == QLatin1Char('n')) {
85  fmt = fmt.arg(n);
86  ++len;
87  result->replace(percentPos, len, fmt);
88  len = fmt.length();
89  }
90  }
91  }
92 }
93 
94 static QString substituteArguments(const QString &input,
95  const QVariantList &arguments)
96 {
97  auto string = input;
98  Q_FOREACH (const QVariant &arg, arguments) {
99  if (arg.userType() == qMetaTypeId<int>())
100  string = string.arg(arg.value<int>());
101  else if (arg.userType() == qMetaTypeId<double>())
102  string = string.arg(arg.value<double>());
103  else if (arg.userType() == qMetaTypeId<QDateTime>())
104  string = string.arg(arg.toDateTime().toString());
105  else
106  string = string.arg(arg.toString());
107  }
108  return string;
109 }
110 
111 QString
112 NullLocalizer::localizeContextString(const QString &string,
113  const QString &context,
114  const QVariantList &arguments) const
115 {
116  Q_UNUSED(context)
117  return substituteArguments(string, arguments);
118 }
119 
120 QString NullLocalizer::localizePluralContextString(
121  const QString &_string, const QString &_pluralForm, const QString &context,
122  const QVariantList &_arguments) const
123 {
124  Q_UNUSED(context)
125  const auto count = _arguments.first().value<int>();
126  auto arguments = _arguments;
127  auto string = _string;
128  auto pluralForm = _pluralForm;
129  if (_string.contains(QStringLiteral("%n"))) {
130  arguments.removeFirst();
131  replacePercentN(&string, count);
132  replacePercentN(&pluralForm, count);
133  }
134  return count > 0 ? substituteArguments(pluralForm, arguments)
135  : substituteArguments(string, arguments);
136 }
137 
138 QString NullLocalizer::localizeString(const QString &string,
139  const QVariantList &arguments) const
140 {
141  return substituteArguments(string, arguments);
142 }
143 
144 QString
145 NullLocalizer::localizePluralString(const QString &_string,
146  const QString &_pluralForm,
147  const QVariantList &_arguments) const
148 {
149  const auto count = _arguments.first().value<int>();
150  auto arguments = _arguments;
151  auto string = _string;
152  auto pluralForm = _pluralForm;
153  if (_string.contains(QStringLiteral("%n"))) {
154  arguments.removeFirst();
155  replacePercentN(&string, count);
156  replacePercentN(&pluralForm, count);
157  }
158  return count > 0 ? substituteArguments(pluralForm, arguments)
159  : substituteArguments(string, arguments);
160 }
161 
162 QString NullLocalizer::currentLocale() const { return QString(); }
163 
164 void NullLocalizer::pushLocale(const QString &localeName)
165 {
166  Q_UNUSED(localeName)
167 }
168 
169 void NullLocalizer::popLocale() {}
170 
171 void NullLocalizer::loadCatalog(const QString &path, const QString &catalog)
172 {
173  Q_UNUSED(path)
174  Q_UNUSED(catalog)
175 }
176 
177 void NullLocalizer::unloadCatalog(const QString &catalog) { Q_UNUSED(catalog) }
The Cutelee namespace holds all public Cutelee API.
Definition: Mainpage.dox:8