KDeclarative

translationcontext.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Kevin Ottens <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 // Undefine this because we don't want our i18n*() method names to be turned into i18nd*()
8 #undef TRANSLATION_DOMAIN
9 
10 #include "translationcontext.h"
11 
12 #include <QDebug>
13 
14 #include <KLocalizedString>
15 
16 TranslationContext::TranslationContext(QObject* parent)
17  : QObject(parent)
18 {
19 }
20 
21 TranslationContext::~TranslationContext()
22 {
23 }
24 
25 QString TranslationContext::domain() const
26 {
27  return m_domain;
28 }
29 
30 void TranslationContext::setDomain(const QString &domain)
31 {
32  if (m_domain == domain) {
33  return;
34  }
35 
36  m_domain = domain;
37  Q_EMIT domainChanged(domain);
38 }
39 
40 QString TranslationContext::i18n(const QString &message, const QString &param1, const QString &param2, const QString &param3, const QString &param4, const QString &param5, const QString &param6, const QString &param7, const QString &param8, const QString &param9, const QString &param10) const
41 {
42  if (message.isNull()) {
43  qWarning() << "i18n() needs at least one parameter";
44  return QString();
45  }
46 
47  KLocalizedString trMessage = ki18nd(m_domain.toUtf8().constData(), message.toUtf8().constData());
48 
49  if (!param1.isNull()) {
50  trMessage = trMessage.subs(param1);
51  }
52  if (!param2.isNull()) {
53  trMessage = trMessage.subs(param2);
54  }
55  if (!param3.isNull()) {
56  trMessage = trMessage.subs(param3);
57  }
58  if (!param4.isNull()) {
59  trMessage = trMessage.subs(param4);
60  }
61  if (!param5.isNull()) {
62  trMessage = trMessage.subs(param5);
63  }
64  if (!param6.isNull()) {
65  trMessage = trMessage.subs(param6);
66  }
67  if (!param7.isNull()) {
68  trMessage = trMessage.subs(param7);
69  }
70  if (!param8.isNull()) {
71  trMessage = trMessage.subs(param8);
72  }
73  if (!param9.isNull()) {
74  trMessage = trMessage.subs(param9);
75  }
76  if (!param10.isNull()) {
77  trMessage = trMessage.subs(param10);
78  }
79 
80  return trMessage.toString();
81 }
82 
83 QString TranslationContext::i18nc(const QString &context, const QString &message, const QString &param1, const QString &param2, const QString &param3, const QString &param4, const QString &param5, const QString &param6, const QString &param7, const QString &param8, const QString &param9, const QString &param10) const
84 {
85  if (context.isNull() || message.isNull()) {
86  qWarning() << "i18nc() needs at least two arguments";
87  return QString();
88  }
89 
90  KLocalizedString trMessage = ki18ndc(m_domain.toUtf8().constData(), context.toUtf8().constData(), message.toUtf8().constData());
91 
92  if (!param1.isNull()) {
93  trMessage = trMessage.subs(param1);
94  }
95  if (!param2.isNull()) {
96  trMessage = trMessage.subs(param2);
97  }
98  if (!param3.isNull()) {
99  trMessage = trMessage.subs(param3);
100  }
101  if (!param4.isNull()) {
102  trMessage = trMessage.subs(param4);
103  }
104  if (!param5.isNull()) {
105  trMessage = trMessage.subs(param5);
106  }
107  if (!param6.isNull()) {
108  trMessage = trMessage.subs(param6);
109  }
110  if (!param7.isNull()) {
111  trMessage = trMessage.subs(param7);
112  }
113  if (!param8.isNull()) {
114  trMessage = trMessage.subs(param8);
115  }
116  if (!param9.isNull()) {
117  trMessage = trMessage.subs(param9);
118  }
119  if (!param10.isNull()) {
120  trMessage = trMessage.subs(param10);
121  }
122 
123  return trMessage.toString();
124 }
125 
126 QString TranslationContext::i18np(const QString &singular, const QString &plural, const QString &param1, const QString &param2, const QString &param3, const QString &param4, const QString &param5, const QString &param6, const QString &param7, const QString &param8, const QString &param9, const QString &param10) const
127 {
128  if (singular.isNull() || plural.isNull()) {
129  qWarning() << "i18np() needs at least two arguments";
130  return QString();
131  }
132 
133  KLocalizedString trMessage = ki18ndp(m_domain.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
134 
135  if (!param1.isNull()) {
136  bool ok;
137  int num = param1.toInt(&ok);
138  if (ok) {
139  trMessage = trMessage.subs(num);
140  } else {
141  trMessage = trMessage.subs(param1);
142  }
143  }
144  if (!param2.isNull()) {
145  trMessage = trMessage.subs(param2);
146  }
147  if (!param3.isNull()) {
148  trMessage = trMessage.subs(param3);
149  }
150  if (!param4.isNull()) {
151  trMessage = trMessage.subs(param4);
152  }
153  if (!param5.isNull()) {
154  trMessage = trMessage.subs(param5);
155  }
156  if (!param6.isNull()) {
157  trMessage = trMessage.subs(param6);
158  }
159  if (!param7.isNull()) {
160  trMessage = trMessage.subs(param7);
161  }
162  if (!param8.isNull()) {
163  trMessage = trMessage.subs(param8);
164  }
165  if (!param9.isNull()) {
166  trMessage = trMessage.subs(param9);
167  }
168  if (!param10.isNull()) {
169  trMessage = trMessage.subs(param10);
170  }
171 
172  return trMessage.toString();
173 }
174 
175 QString TranslationContext::i18ncp(const QString &context, const QString &singular, const QString &plural, const QString &param1, const QString &param2, const QString &param3, const QString &param4, const QString &param5, const QString &param6, const QString &param7, const QString &param8, const QString &param9, const QString &param10) const
176 {
177  if (context.isNull() || singular.isNull() || plural.isNull()) {
178  qWarning() << "i18ncp() needs at least three arguments";
179  return QString();
180  }
181 
182  KLocalizedString trMessage = ki18ndcp(m_domain.toUtf8().constData(), context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
183 
184  if (!param1.isNull()) {
185  bool ok;
186  int num = param1.toInt(&ok);
187  if (ok) {
188  trMessage = trMessage.subs(num);
189  } else {
190  trMessage = trMessage.subs(param1);
191  }
192  }
193  if (!param2.isNull()) {
194  trMessage = trMessage.subs(param2);
195  }
196  if (!param3.isNull()) {
197  trMessage = trMessage.subs(param3);
198  }
199  if (!param4.isNull()) {
200  trMessage = trMessage.subs(param4);
201  }
202  if (!param5.isNull()) {
203  trMessage = trMessage.subs(param5);
204  }
205  if (!param6.isNull()) {
206  trMessage = trMessage.subs(param6);
207  }
208  if (!param7.isNull()) {
209  trMessage = trMessage.subs(param7);
210  }
211  if (!param8.isNull()) {
212  trMessage = trMessage.subs(param8);
213  }
214  if (!param9.isNull()) {
215  trMessage = trMessage.subs(param9);
216  }
217  if (!param10.isNull()) {
218  trMessage = trMessage.subs(param10);
219  }
220 
221  return trMessage.toString();
222 }
QString toString() const
KLocalizedString KI18N_EXPORT ki18ndc(const char *domain, const char *context, const char *text)
KLocalizedString KI18N_EXPORT ki18ndp(const char *domain, const char *singular, const char *plural)
KLocalizedString KI18N_EXPORT ki18ndcp(const char *domain, const char *context, const char *singular, const char *plural)
bool isNull() const const
KLocalizedString subs(int a, int fieldWidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
int toInt(bool *ok, int base) const const
const char * constData() const const
KLocalizedString KI18N_EXPORT ki18nd(const char *domain, const char *text)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.