KLocalizedQmlContext Class

This class is meant to be used to simplify integration of the KI18n framework in QML. More...

Header: #include <KLocalizedQmlContext>
CMake: find_package(KF6 REQUIRED COMPONENTS I18n)
target_link_libraries(mytarget PRIVATE KF6::I18nQml)
Since: 6.8
Inherits: QObject

Properties

Public Functions

KLocalizedQmlContext(QObject *parent = nullptr)
void setTranslationDomain(const QString &domain)
QString translationDomain() const

Signals

void translationDomainChanged(const QString &translationDomain)
(since 6.8) KLocalizedQmlContext *setupLocalizedContext(QQmlEngine *engine)

Detailed Description

The way to do so, is by creating this object and setting it as a context object:

QQmlApplicationEngine engine;
auto ctx = new KLocalizedQmlContext(&engine);
engine->rootContext()->setContextObject(ctx);
QQmlEngine::setContextForObject(ctx, engine.rootContext());
ctx->setTranslationDomain(...);

In many cases this can be simplified using KLocalization::setupLocalizedContext():

QQmlApplicationEngine engine;
KLocalization::setupLocalizedContext(&engine);

Then i18n*() and xi18n*() functions should be available for use from the code loaded in the engine, for the view.

Unlike its predecessor KLocalizedContext this does automatically trigger a binding re-evaluation when the application language is changed at runtime (with Qt 6.6 or higher).

Note: Plural functions differ from the C/C++ version. On QML/JS we can get a real value easily. To solve warnings on those cases we'll cast the first argument to make sure it's taken into account for the plural.

Property Documentation

translationDomain : QString

This property only needs to be specified if the context is being run on a library. in an application there is no need to set the translation domain as the application's domain can be used.

Access functions:

QString translationDomain() const
void setTranslationDomain(const QString &domain)

Notifier signal:

void translationDomainChanged(const QString &translationDomain)

Member Function Documentation

[explicit] KLocalizedQmlContext::KLocalizedQmlContext(QObject *parent = nullptr)

void KLocalizedQmlContext::setTranslationDomain(const QString &domain)

Note: Setter function for property translationDomain.

See also translationDomain().

QString KLocalizedQmlContext::translationDomain() const

Note: Getter function for property translationDomain.

See also setTranslationDomain().

Related Non-Members

[since 6.8] KLocalizedQmlContext *setupLocalizedContext(QQmlEngine *engine)

Creates a KLocalizedQmlContext engine and sets it up in the root context of engine.

If TRANSLATION_DOMAIN is defined, that is also set on the created context.

This function was introduced in 6.8.