| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> |
| 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 | |
| 30 | void KLocalizedTranslator::setTranslationDomain(const QString &translationDomain) |
| 31 | { |
| 32 | d->translationDomain = translationDomain; |
| 33 | } |
| 34 | |
| 35 | void KLocalizedTranslator::addContextToMonitor(const QString &context) |
| 36 | { |
| 37 | d->monitoredContexts.insert(value: context); |
| 38 | } |
| 39 | |
| 40 | void KLocalizedTranslator::removeContextToMonitor(const QString &context) |
| 41 | { |
| 42 | d->monitoredContexts.remove(value: 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(value: QString::fromUtf8(utf8: context))) { |
| 48 | return QTranslator::translate(context, sourceText, disambiguation, n); |
| 49 | } |
| 50 | if (qstrlen(str: disambiguation) == 0) { |
| 51 | return ki18nd(domain: d->translationDomain.toUtf8().constData(), text: sourceText).toString(); |
| 52 | } else { |
| 53 | return ki18ndc(domain: d->translationDomain.toUtf8().constData(), context: disambiguation, text: sourceText).toString(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | #include "moc_klocalizedtranslator.cpp" |
| 58 | |