File indexing completed on 2024-04-21 03:54:23
0001 /* 0002 SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 #ifndef KLOCALIZEDTRANSLATOR_H 0007 #define KLOCALIZEDTRANSLATOR_H 0008 0009 #include <ki18n_export.h> 0010 0011 #include <QTranslator> 0012 0013 #include <memory> 0014 0015 class KLocalizedTranslatorPrivate; 0016 0017 /** 0018 * @class KLocalizedTranslator klocalizedtranslator.h <KLocalizedTranslator> 0019 * 0020 * @brief A QTranslator using KLocalizedString for translations. 0021 * 0022 * This class allows to translate strings in Qt's translation system with KLocalizedString. 0023 * An example is the translation of a dynamically loaded user interface through QUILoader. 0024 * 0025 * To use this Translator install it in the QCoreApplication and provide the translation domain 0026 * to be used. The Translator can operate for multiple contexts, those needs to be specified. 0027 * 0028 * Example for translating a UI loaded through QUILoader: 0029 * @code 0030 * // create translator and install in QCoreApplication 0031 * KLocalizedTranslator *translator = new KLocalizedTranslator(this); 0032 * QCoreApplication::instance()->installTranslator(translator); 0033 * translator->setTranslationDomain(QStringLiteral("MyAppsDomain")); 0034 * 0035 * // create the QUILoader 0036 * QUiLoader *loader = new QUiLoader(this); 0037 * loader->setLanguageChangeEnabled(true); 0038 * 0039 * // load the UI 0040 * QFile uiFile(QStringLiteral("/path/to/userInterface.ui")); 0041 * uiFile.open(QFile::ReadOnly); 0042 * QWidget *loadedWidget = loader->load(&uiFile, this); 0043 * uiFile.close(); 0044 * 0045 * // the object name of the loaded UI is the context in this case 0046 * translator->addContextToMonitor(loadedWidget->objectName()); 0047 * 0048 * // send a LanguageChange event, this will re-translate using our translator 0049 * QEvent le(QEvent::LanguageChange); 0050 * QCoreApplication::sendEvent(loadedWidget, &le); 0051 * @endcode 0052 * 0053 * @since 5.0 0054 **/ 0055 class KI18N_EXPORT KLocalizedTranslator : public QTranslator 0056 { 0057 Q_OBJECT 0058 public: 0059 explicit KLocalizedTranslator(QObject *parent = nullptr); 0060 virtual ~KLocalizedTranslator(); 0061 QString translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const override; 0062 0063 /** 0064 * Sets the @p translationDomain to be used. 0065 * 0066 * The translation domain is required. Without the translation domain any invocation of 0067 * translate() will be delegated to the base class. 0068 * 0069 * @param translationDomain The translation domain to be used. 0070 **/ 0071 void setTranslationDomain(const QString &translationDomain); 0072 0073 /** 0074 * Adds a @p context for which this Translator should be active. 0075 * 0076 * The Translator only translates texts with a context matching one of the monitored contexts. 0077 * If the context is not monitored, the translate() method delegates to the base class. 0078 * 0079 * @param context The context for which the Translator should be active 0080 * 0081 * @see removeContextToMonitor 0082 **/ 0083 void addContextToMonitor(const QString &context); 0084 0085 /** 0086 * Stop translating for the given @p context. 0087 * 0088 * @param context The context for which the Translator should no longer be active 0089 * 0090 * @see addContextToMonitor 0091 **/ 0092 void removeContextToMonitor(const QString &context); 0093 0094 private: 0095 std::unique_ptr<KLocalizedTranslatorPrivate> const d; 0096 }; 0097 0098 #endif // KLOCALIZEDTRANSLATOR_H