KI18n

klocalizedtranslator.cpp
1 /*
2  SPDX-FileCopyrightText: 2014 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #include "klocalizedtranslator.h"
7 #include "klocalizedstring.h"
8 
9 // Qt
10 #include <QMetaObject>
11 #include <QMetaProperty>
12 
13 class KLocalizedTranslatorPrivate
14 {
15 public:
16  QString translationDomain;
17  QSet<QString> monitoredContexts;
18 };
19 
20 KLocalizedTranslator::KLocalizedTranslator(QObject *parent)
21  : QTranslator(parent)
22  , d(new KLocalizedTranslatorPrivate)
23 {
24 }
25 
26 KLocalizedTranslator::~KLocalizedTranslator()
27 {
28 }
29 
31 {
32  d->translationDomain = translationDomain;
33 }
34 
36 {
37  d->monitoredContexts.insert(context);
38 }
39 
41 {
42  d->monitoredContexts.remove(context);
43 }
44 
45 QString KLocalizedTranslator::translate(const char *context, const char *sourceText, const char *disambiguation, int n) const
46 {
47  if (d->translationDomain.isEmpty() || !d->monitoredContexts.contains(QString::fromUtf8(context))) {
48  return QTranslator::translate(context, sourceText, disambiguation, n);
49  }
50  if (qstrlen(disambiguation) == 0) {
51  return ki18nd(d->translationDomain.toUtf8().constData(), sourceText).toString();
52  } else {
53  return ki18ndc(d->translationDomain.toUtf8().constData(), disambiguation, sourceText).toString();
54  }
55 }
QString fromUtf8(const char *str, int size)
virtual QString translate(const char *context, const char *sourceText, const char *disambiguation, int n) const const
KLocalizedString KI18N_EXPORT ki18ndc(const char *domain, const char *context, const char *text)
Create non-finalized translated string from domain with context.
void addContextToMonitor(const QString &context)
Adds a context for which this Translator should be active.
QString toString() const
Finalize the translation.
void setTranslationDomain(const QString &translationDomain)
Sets the translationDomain to be used.
KLocalizedString KI18N_EXPORT ki18nd(const char *domain, const char *text)
Create non-finalized translated string from domain.
void removeContextToMonitor(const QString &context)
Stop translating for the given context.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Aug 16 2022 04:07:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.