Translation of QDialogButtonBox in statically build app

  • Hi,
    I created an app that is statically build into one exe file. I use the translation .qm files to switch between languages. I load them from the qrc resource file:

    QApplication a(argc, argv);
    QTranslator translator;
    QString locale;
    locale = QLocale::system().name();
    translator.load(QString(":/lang/myapp_") + locale);

    My problem is that the "OK" and "Cancel" buttons of QDialogButtonBox are not translated.
    How to instruct the QT5.5 to include the system .qm files in the exe? I would like to avoid copying the system .qm files into my project.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    AFAIK, you can't avoid that copy

  • @SGaist Thanks.
    If I cannot avoid a copy, can I at least automate the copy to my project via the .pro file by running qmake? Eg. are the translation files across platforms always at the same location?
    In addition, do I also need to load the translations manually from the resource file or is there another way to instruct the qt to load them from the resource file?

  • Another question related to translations. If I run lrelease, it generates the .qm file in the same directory as the .ts files. Is there a way to specify the output directory?

  • Lifetime Qt Champion

    IIRC, you can call lrelease -qm path/qmfile.qm

  • Finally I'm able to automatically copy the qt translation files and create resource file from the pro file:

    TRANSLATIONS = myapp_cs.ts \
    QT_TRANSLATIONS = qtbase_cs.qm
    # create lang folder
    QM_DIR = $$shadowed($${PWD})/lang
    # copy system translation files to lang folder
        FILE = $$shell_path($$[QT_INSTALL_TRANSLATIONS]/$${FILE})
        system($${QMAKE_COPY} $${FILE} $$shell_path($${QM_DIR}/lang))
    #generate qm files directly from qmake
        QM_FILE = $$shell_path($${QM_DIR}/lang/$$replace(FILE,.ts,.qm))
        system(lrelease $${FILE} -qm $${QM_FILE})
    #create resource file with translations
    win32:system(cd $$shell_path($${QM_DIR}) & rcc --project -o lang.qrc)
    unix:system(cd $$shell_path($${QM_DIR}) ; rcc --project -o lang.qrc)
    RESOURCES += $${QM_DIR}/lang.qrc
    # extra target to generate ts translation files
    lupdate.commands     = lupdate $$_PRO_FILE_
    lupdate.depends      = FORCE
    QMAKE_EXTRA_TARGETS += lupdate
    # extra compiler to generate qm translation files
    lrelease.input         = TRANSLATIONS
    lrelease.output        = $${QM_DIR}/lang/${QMAKE_FILE_BASE}.qm
    lrelease.commands      = lrelease ${QMAKE_FILE_NAME} -qm ${QMAKE_FILE_OUT}
    lrelease.CONFIG       += no_link target_predeps

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.