KI18n

klocalizedcontext.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-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 "klocalizedcontext.h"
11
12#if KI18N_BUILD_DEPRECATED_SINCE(6, 8)
13#include <klocalizedstring.h>
14
15#include "ki18n_logging.h"
16
17class KLocalizedContextPrivate
18{
19public:
20 QString m_translationDomain;
21};
22
23KLocalizedContext::KLocalizedContext(QObject *parent)
24 : QObject(parent)
25 , d(new KLocalizedContextPrivate)
26{
27}
28
29KLocalizedContext::~KLocalizedContext()
30{
31 delete d;
32}
33
35{
36 return d->m_translationDomain;
37}
38
39void KLocalizedContext::setTranslationDomain(const QString &domain)
40{
41 if (domain != d->m_translationDomain) {
42 d->m_translationDomain = domain;
43 Q_EMIT translationDomainChanged(domain);
44 }
45}
46
47static void subsVariant(KLocalizedString &trMessage, const QVariant &value)
48{
49 switch (value.userType()) {
51 trMessage = trMessage.subs(value.toString());
52 break;
53 case QMetaType::Int:
54 trMessage = trMessage.subs(value.toInt());
55 break;
57 trMessage = trMessage.subs(value.toDouble());
58 break;
59 case QMetaType::Char:
60 trMessage = trMessage.subs(value.toChar());
61 break;
62 default:
63 if (value.canConvert<QString>()) {
64 trMessage = trMessage.subs(value.toString());
65 } else {
66 trMessage = trMessage.subs(QStringLiteral("???"));
67 qCWarning(KI18N) << "couldn't convert" << value << "to translate";
68 }
69 }
70}
71
72static void resolveMessage(KLocalizedString &trMessage,
73 const QVariant &param1,
74 const QVariant &param2,
75 const QVariant &param3,
76 const QVariant &param4,
77 const QVariant &param5,
78 const QVariant &param6,
79 const QVariant &param7,
80 const QVariant &param8,
81 const QVariant &param9,
82 const QVariant &param10 = QVariant())
83{
84 if (param1.isValid()) {
85 subsVariant(trMessage, param1);
86 }
87 if (param2.isValid()) {
88 subsVariant(trMessage, param2);
89 }
90 if (param3.isValid()) {
91 subsVariant(trMessage, param3);
92 }
93 if (param4.isValid()) {
94 subsVariant(trMessage, param4);
95 }
96 if (param5.isValid()) {
97 subsVariant(trMessage, param5);
98 }
99 if (param6.isValid()) {
100 subsVariant(trMessage, param6);
101 }
102 if (param7.isValid()) {
103 subsVariant(trMessage, param7);
104 }
105 if (param8.isValid()) {
106 subsVariant(trMessage, param8);
107 }
108 if (param9.isValid()) {
109 subsVariant(trMessage, param9);
110 }
111 if (param10.isValid()) {
112 subsVariant(trMessage, param10);
113 }
114}
115
116static void resolvePlural(KLocalizedString &trMessage, const QVariant &param)
117{
118 trMessage = trMessage.subs(param.toInt());
119}
120
121QString KLocalizedContext::i18n(const QString &message,
122 const QVariant &param1,
123 const QVariant &param2,
124 const QVariant &param3,
125 const QVariant &param4,
126 const QVariant &param5,
127 const QVariant &param6,
128 const QVariant &param7,
129 const QVariant &param8,
130 const QVariant &param9,
131 const QVariant &param10) const
132{
133 if (message.isEmpty()) {
134 qCWarning(KI18N) << "i18n() needs at least one parameter";
135 return QString();
136 }
137
138 KLocalizedString trMessage;
139 if (!d->m_translationDomain.isEmpty()) {
140 trMessage = ki18nd(d->m_translationDomain.toUtf8().constData(), message.toUtf8().constData());
141 } else {
142 trMessage = ki18n(message.toUtf8().constData());
143 }
144
145 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
146
147 return trMessage.toString();
148}
149
150QString KLocalizedContext::i18nc(const QString &context,
151 const QString &message,
152 const QVariant &param1,
153 const QVariant &param2,
154 const QVariant &param3,
155 const QVariant &param4,
156 const QVariant &param5,
157 const QVariant &param6,
158 const QVariant &param7,
159 const QVariant &param8,
160 const QVariant &param9,
161 const QVariant &param10) const
162{
163 if (context.isEmpty() || message.isEmpty()) {
164 qCWarning(KI18N) << "i18nc() needs at least two arguments";
165 return QString();
166 }
167
168 KLocalizedString trMessage;
169 if (!d->m_translationDomain.isEmpty()) {
170 trMessage = ki18ndc(d->m_translationDomain.toUtf8().constData(), context.toUtf8().constData(), message.toUtf8().constData());
171 } else {
172 trMessage = ki18nc(context.toUtf8().constData(), message.toUtf8().constData());
173 }
174
175 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
176
177 return trMessage.toString();
178}
179
180QString KLocalizedContext::i18np(const QString &singular,
181 const QString &plural,
182 const QVariant &param1,
183 const QVariant &param2,
184 const QVariant &param3,
185 const QVariant &param4,
186 const QVariant &param5,
187 const QVariant &param6,
188 const QVariant &param7,
189 const QVariant &param8,
190 const QVariant &param9,
191 const QVariant &param10) const
192{
193 if (singular.isEmpty() || plural.isEmpty()) {
194 qCWarning(KI18N) << "i18np() needs at least two arguments";
195 return QString();
196 }
197
198 KLocalizedString trMessage;
199 if (!d->m_translationDomain.isEmpty()) {
200 trMessage = ki18ndp(d->m_translationDomain.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
201 } else {
202 trMessage = ki18np(singular.toUtf8().constData(), plural.toUtf8().constData());
203 }
204
205 resolvePlural(trMessage, param1);
206 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
207
208 return trMessage.toString();
209}
210
211QString KLocalizedContext::i18ncp(const QString &context,
212 const QString &singular,
213 const QString &plural,
214 const QVariant &param1,
215 const QVariant &param2,
216 const QVariant &param3,
217 const QVariant &param4,
218 const QVariant &param5,
219 const QVariant &param6,
220 const QVariant &param7,
221 const QVariant &param8,
222 const QVariant &param9,
223 const QVariant &param10) const
224{
225 if (context.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
226 qCWarning(KI18N) << "i18ncp() needs at least three arguments";
227 return QString();
228 }
229
230 KLocalizedString trMessage;
231 if (!d->m_translationDomain.isEmpty()) {
232 trMessage =
233 ki18ndcp(d->m_translationDomain.toUtf8().constData(), context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
234 } else {
235 trMessage = ki18ncp(context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
236 }
237
238 resolvePlural(trMessage, param1);
239 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
240
241 return trMessage.toString();
242}
243
244QString KLocalizedContext::i18nd(const QString &domain,
245 const QString &message,
246 const QVariant &param1,
247 const QVariant &param2,
248 const QVariant &param3,
249 const QVariant &param4,
250 const QVariant &param5,
251 const QVariant &param6,
252 const QVariant &param7,
253 const QVariant &param8,
254 const QVariant &param9,
255 const QVariant &param10) const
256{
257 if (domain.isEmpty() || message.isEmpty()) {
258 qCWarning(KI18N) << "i18nd() needs at least two parameters";
259 return QString();
260 }
261
262 KLocalizedString trMessage = ki18nd(domain.toUtf8().constData(), message.toUtf8().constData());
263
264 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
265
266 return trMessage.toString();
267}
268
269QString KLocalizedContext::i18ndc(const QString &domain,
270 const QString &context,
271 const QString &message,
272 const QVariant &param1,
273 const QVariant &param2,
274 const QVariant &param3,
275 const QVariant &param4,
276 const QVariant &param5,
277 const QVariant &param6,
278 const QVariant &param7,
279 const QVariant &param8,
280 const QVariant &param9,
281 const QVariant &param10) const
282{
283 if (domain.isEmpty() || context.isEmpty() || message.isEmpty()) {
284 qCWarning(KI18N) << "i18ndc() needs at least three arguments";
285 return QString();
286 }
287
288 KLocalizedString trMessage = ki18ndc(domain.toUtf8().constData(), context.toUtf8().constData(), message.toUtf8().constData());
289
290 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
291
292 return trMessage.toString();
293}
294
295QString KLocalizedContext::i18ndp(const QString &domain,
296 const QString &singular,
297 const QString &plural,
298 const QVariant &param1,
299 const QVariant &param2,
300 const QVariant &param3,
301 const QVariant &param4,
302 const QVariant &param5,
303 const QVariant &param6,
304 const QVariant &param7,
305 const QVariant &param8,
306 const QVariant &param9,
307 const QVariant &param10) const
308{
309 if (domain.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
310 qCWarning(KI18N) << "i18ndp() needs at least three arguments";
311 return QString();
312 }
313
314 KLocalizedString trMessage = ki18ndp(domain.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
315
316 resolvePlural(trMessage, param1);
317 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
318
319 return trMessage.toString();
320}
321
322QString KLocalizedContext::i18ndcp(const QString &domain,
323 const QString &context,
324 const QString &singular,
325 const QString &plural,
326 const QVariant &param1,
327 const QVariant &param2,
328 const QVariant &param3,
329 const QVariant &param4,
330 const QVariant &param5,
331 const QVariant &param6,
332 const QVariant &param7,
333 const QVariant &param8,
334 const QVariant &param9,
335 const QVariant &param10) const
336{
337 if (domain.isEmpty() || context.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
338 qCWarning(KI18N) << "i18ndcp() needs at least four arguments";
339 return QString();
340 }
341
342 KLocalizedString trMessage =
343 ki18ndcp(domain.toUtf8().constData(), context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
344
345 resolvePlural(trMessage, param1);
346 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
347
348 return trMessage.toString();
349}
350
351/////////////////////////
352
353QString KLocalizedContext::xi18n(const QString &message,
354 const QVariant &param1,
355 const QVariant &param2,
356 const QVariant &param3,
357 const QVariant &param4,
358 const QVariant &param5,
359 const QVariant &param6,
360 const QVariant &param7,
361 const QVariant &param8,
362 const QVariant &param9,
363 const QVariant &param10) const
364{
365 if (message.isEmpty()) {
366 qCWarning(KI18N) << "xi18n() needs at least one parameter";
367 return QString();
368 }
369
370 KLocalizedString trMessage;
371 if (!d->m_translationDomain.isEmpty()) {
372 trMessage = kxi18nd(d->m_translationDomain.toUtf8().constData(), message.toUtf8().constData());
373 } else {
374 trMessage = kxi18n(message.toUtf8().constData());
375 }
376
377 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
378
379 return trMessage.toString();
380}
381
382QString KLocalizedContext::xi18nc(const QString &context,
383 const QString &message,
384 const QVariant &param1,
385 const QVariant &param2,
386 const QVariant &param3,
387 const QVariant &param4,
388 const QVariant &param5,
389 const QVariant &param6,
390 const QVariant &param7,
391 const QVariant &param8,
392 const QVariant &param9,
393 const QVariant &param10) const
394{
395 if (context.isEmpty() || message.isEmpty()) {
396 qCWarning(KI18N) << "xi18nc() needs at least two arguments";
397 return QString();
398 }
399
400 KLocalizedString trMessage;
401 if (!d->m_translationDomain.isEmpty()) {
402 trMessage = kxi18ndc(d->m_translationDomain.toUtf8().constData(), context.toUtf8().constData(), message.toUtf8().constData());
403 } else {
404 trMessage = kxi18nc(context.toUtf8().constData(), message.toUtf8().constData());
405 }
406
407 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
408
409 return trMessage.toString();
410}
411
412QString KLocalizedContext::xi18np(const QString &singular,
413 const QString &plural,
414 const QVariant &param1,
415 const QVariant &param2,
416 const QVariant &param3,
417 const QVariant &param4,
418 const QVariant &param5,
419 const QVariant &param6,
420 const QVariant &param7,
421 const QVariant &param8,
422 const QVariant &param9,
423 const QVariant &param10) const
424{
425 if (singular.isEmpty() || plural.isEmpty()) {
426 qCWarning(KI18N) << "xi18np() needs at least two arguments";
427 return QString();
428 }
429
430 KLocalizedString trMessage;
431 if (!d->m_translationDomain.isEmpty()) {
432 trMessage = kxi18ndp(d->m_translationDomain.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
433 } else {
434 trMessage = kxi18np(singular.toUtf8().constData(), plural.toUtf8().constData());
435 }
436
437 resolvePlural(trMessage, param1);
438 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
439
440 return trMessage.toString();
441}
442
443QString KLocalizedContext::xi18ncp(const QString &context,
444 const QString &singular,
445 const QString &plural,
446 const QVariant &param1,
447 const QVariant &param2,
448 const QVariant &param3,
449 const QVariant &param4,
450 const QVariant &param5,
451 const QVariant &param6,
452 const QVariant &param7,
453 const QVariant &param8,
454 const QVariant &param9,
455 const QVariant &param10) const
456{
457 if (context.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
458 qCWarning(KI18N) << "xi18ncp() needs at least three arguments";
459 return QString();
460 }
461
462 KLocalizedString trMessage;
463 if (!d->m_translationDomain.isEmpty()) {
464 trMessage =
465 kxi18ndcp(d->m_translationDomain.toUtf8().constData(), context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
466 } else {
467 trMessage = kxi18ncp(context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
468 }
469
470 resolvePlural(trMessage, param1);
471 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
472
473 return trMessage.toString();
474}
475
476QString KLocalizedContext::xi18nd(const QString &domain,
477 const QString &message,
478 const QVariant &param1,
479 const QVariant &param2,
480 const QVariant &param3,
481 const QVariant &param4,
482 const QVariant &param5,
483 const QVariant &param6,
484 const QVariant &param7,
485 const QVariant &param8,
486 const QVariant &param9,
487 const QVariant &param10) const
488{
489 if (domain.isEmpty() || message.isEmpty()) {
490 qCWarning(KI18N) << "xi18nd() needs at least two parameters";
491 return QString();
492 }
493
494 KLocalizedString trMessage = kxi18nd(domain.toUtf8().constData(), message.toUtf8().constData());
495
496 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
497
498 return trMessage.toString();
499}
500
501QString KLocalizedContext::xi18ndc(const QString &domain,
502 const QString &context,
503 const QString &message,
504 const QVariant &param1,
505 const QVariant &param2,
506 const QVariant &param3,
507 const QVariant &param4,
508 const QVariant &param5,
509 const QVariant &param6,
510 const QVariant &param7,
511 const QVariant &param8,
512 const QVariant &param9,
513 const QVariant &param10) const
514{
515 if (domain.isEmpty() || context.isEmpty() || message.isEmpty()) {
516 qCWarning(KI18N) << "x18ndc() needs at least three arguments";
517 return QString();
518 }
519
520 KLocalizedString trMessage = kxi18ndc(domain.toUtf8().constData(), context.toUtf8().constData(), message.toUtf8().constData());
521
522 resolveMessage(trMessage, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10);
523
524 return trMessage.toString();
525}
526
527QString KLocalizedContext::xi18ndp(const QString &domain,
528 const QString &singular,
529 const QString &plural,
530 const QVariant &param1,
531 const QVariant &param2,
532 const QVariant &param3,
533 const QVariant &param4,
534 const QVariant &param5,
535 const QVariant &param6,
536 const QVariant &param7,
537 const QVariant &param8,
538 const QVariant &param9,
539 const QVariant &param10) const
540{
541 if (domain.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
542 qCWarning(KI18N) << "xi18ndp() needs at least three arguments";
543 return QString();
544 }
545
546 KLocalizedString trMessage = kxi18ndp(domain.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
547
548 resolvePlural(trMessage, param1);
549 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
550
551 return trMessage.toString();
552}
553
554QString KLocalizedContext::xi18ndcp(const QString &domain,
555 const QString &context,
556 const QString &singular,
557 const QString &plural,
558 const QVariant &param1,
559 const QVariant &param2,
560 const QVariant &param3,
561 const QVariant &param4,
562 const QVariant &param5,
563 const QVariant &param6,
564 const QVariant &param7,
565 const QVariant &param8,
566 const QVariant &param9,
567 const QVariant &param10) const
568{
569 if (domain.isEmpty() || context.isEmpty() || singular.isEmpty() || plural.isEmpty()) {
570 qCWarning(KI18N) << "xi18ndcp() needs at least four arguments";
571 return QString();
572 }
573
574 KLocalizedString trMessage =
575 kxi18ndcp(domain.toUtf8().constData(), context.toUtf8().constData(), singular.toUtf8().constData(), plural.toUtf8().constData());
576
577 resolvePlural(trMessage, param1);
578 resolveMessage(trMessage, param2, param3, param4, param5, param6, param7, param8, param9, param10);
579
580 return trMessage.toString();
581}
582
583#include "moc_klocalizedcontext.cpp"
584#endif
QString translationDomain
This property only needs to be specified if the context is being run on a library.
Class for producing and handling localized messages.
QString toString() const
Finalize the translation.
KLocalizedString subs(int a, int fieldWidth=0, int base=10, QChar fillChar=QLatin1Char(' ')) const
Substitute an int argument into the message.
const char * constData() const const
Q_EMITQ_EMIT
bool isEmpty() const const
QByteArray toUtf8() const const
bool canConvert() const const
bool isValid() const const
QChar toChar() const const
double toDouble(bool *ok) const const
int toInt(bool *ok) const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:19:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.