KI18n 5.109.0
Public Member Functions | List of all members
KLocalizedTranslator

A QTranslator using KLocalizedString for translations. More...

#include <KLocalizedTranslator>

Public Member Functions

 KLocalizedTranslator (QObject *parent=nullptr)
 
void addContextToMonitor (const QString &context)
 Adds a context for which this Translator should be active.
 
void removeContextToMonitor (const QString &context)
 Stop translating for the given context.
 
void setTranslationDomain (const QString &translationDomain)
 Sets the translationDomain to be used.
 
QString translate (const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const override
 

Detailed Description

A QTranslator using KLocalizedString for translations.

This class allows to translate strings in Qt's translation system with KLocalizedString. An example is the translation of a dynamically loaded user interface through QUILoader.

To use this Translator install it in the QCoreApplication and provide the translation domain to be used. The Translator can operate for multiple contexts, those needs to be specified.

Example for translating a UI loaded through QUILoader:

// create translator and install in QCoreApplication
QCoreApplication::instance()->installTranslator(translator);
translator->setTranslationDomain(QStringLiteral("MyAppsDomain"));
// create the QUILoader
QUiLoader *loader = new QUiLoader(this);
loader->setLanguageChangeEnabled(true);
// load the UI
QFile uiFile(QStringLiteral("/path/to/userInterface.ui"));
uiFile.open(QFile::ReadOnly);
QWidget *loadedWidget = loader->load(&uiFile, this);
uiFile.close();
// the object name of the loaded UI is the context in this case
translator->addContextToMonitor(loadedWidget->objectName());
// send a LanguageChange event, this will re-translate using our translator
QEvent le(QEvent::LanguageChange);
QCoreApplication::sendEvent(loadedWidget, &le);
A QTranslator using KLocalizedString for translations.
Definition klocalizedtranslator.h:56
void addContextToMonitor(const QString &context)
Adds a context for which this Translator should be active.
void setTranslationDomain(const QString &translationDomain)
Sets the translationDomain to be used.
Since
5.0

Member Function Documentation

◆ addContextToMonitor()

void KLocalizedTranslator::addContextToMonitor ( const QString &  context)

Adds a context for which this Translator should be active.

The Translator only translates texts with a context matching one of the monitored contexts. If the context is not monitored, the translate() method delegates to the base class.

Parameters
contextThe context for which the Translator should be active
See also
removeContextToMonitor

◆ removeContextToMonitor()

void KLocalizedTranslator::removeContextToMonitor ( const QString &  context)

Stop translating for the given context.

Parameters
contextThe context for which the Translator should no longer be active
See also
addContextToMonitor

◆ setTranslationDomain()

void KLocalizedTranslator::setTranslationDomain ( const QString &  translationDomain)

Sets the translationDomain to be used.

The translation domain is required. Without the translation domain any invocation of translate() will be delegated to the base class.

Parameters
translationDomainThe translation domain to be used.