Pages
API Documentation
On this page

KLocalizedQmlContext Class

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

  • Header: #include <KLocalizedQmlContext>
  • Since: Qt 6.8
  • CMake: find_package(KF6 REQUIRED COMPONENTS I18n)
    target_link_libraries(mytarget PRIVATE KF6::I18nQml)
  • In QML: KI18n and KI18nContext
  • Inherits: QObject

List of all members, including inherited members

Properties

Public Functions

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 KI18n.i18n*() and KI18n.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.

Warning: For history reasons the i18n functions are also available on the root context without KI18n.* prefix. It is very recommended to use the KI18n.* version moving forward, so QML tooling like qmlls and qmllint can inspect the API calls. KI18n.* was introduced in version 6.21.

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:

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()

[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 Qt 6.8.

We use cookies to personalize content and to analyze our traffic. Please decide if you are willing to accept cookies from our website.