简体   繁体   中英

translate buttons on qml dialogs

I am using QML MessageDialog with a Cancel and a Yes button. My phone is set to german, but the Cancel and Yes button are not translated.

I am following the qt documentation here and added this to my main:

QTranslator qtTranslator;
qtTranslator.load("qt_" + QLocale::system().name(),
        QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&qtTranslator);

It does not work. This code:

qDebug() << QLibraryInfo::location(QLibraryInfo::TranslationsPath);
qDebug() << qtTranslator.isEmpty();

Tells me:

  1. The translation path is: /work/build/______________________________PADDING______________________________/translations

  2. The qtTranslator object is empty.

I also load a translator for my own strings, and that works.

So I do I properly initialize translation of Qt internal stings on android?

If you want translate your application you should distrubute qt translation files with it. For android simple copy translation files to assets folder. Then set this path for qtTranslator .

You can do this with qmake. Just put this to your .pro file:

android {
    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
    TRDESTDIR = $$system_path($$PWD/android/assets/translations)
    QMAKE_POST_LINK = $$QMAKE_COPY $$shell_path($$[QT_INSTALL_TRANSLATIONS]/qt*_ru.qm) $$TRDESTDIR
}

Of course you should change ANDROID_PACKAGE_SOURCE_DIR and TRDESTDIR to your actual location if it differs. Also you should change qt*_ru.qm to your language.

Then you could load translation like:

qtTranslator.load("qt_ru","assets:/translations")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM