KI18n

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

KDE's Doxygen guidelines are available online.