Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using gettext instead of Qt translation with UI files



  • I'm working on an old application which uses gettext to translate string into different languages. Now this application has Qt5 GUI built using *.ui files.

    I managed to use gettext but with workarounds:

    load(uic)
    uic.commands +=  "--tr qt5gettext --include qt5gettext.h"
    

    where qt5gettext.h is the following:

    #include <libintl.h>
    #define qt5gettext(s,i) gettext(s)
    

    This workaround is required because uic creates the following code:

    label_3->setText(qt5gettext("Time source:", Q_NULLPTR));
    

    Is there any way to have normal gettext calls without workarounds? I.e.:

    label_3->setText(gettext("Time source:"));
    

  • Qt Champions 2019

    Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.


  • Lifetime Qt Champion

    Hi and welcome to the forums
    You mean to change how it generate code from the UI files ?
    Outside of change it and compile a custom Qt version.
    Not as far as i know.

    Ahh, Mr. @Christian-Ehrlicher had a much better idea.


  • Qt Champions 2019

    Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.



  • @La-Bamba said in Using gettext instead of Qt translation with UI files:

    old application

    I assume old application is frozen, I mean no new strings/translations will be added and if so, they'll be added to new Qt's UI

    which uses gettext to translate string into different languages

    I assume that you have a bunch of .po files for each language you have a translation, i.e. for a Spanish translation file test_es.po:

    #: locationdialog.cpp:228
    msgid "Do you want to quit?"
    msgstr "¿Quiere salir?"
    

    So I'd suggest you use the normal Qt way enclosing your strings to translate with tr() calls (Qt Designer even does it for you) keeping in mind to use all the same original strings contained in .po files (all from msgid lines). From previous example:

    label_3->setText(tr("Do you want to quit?"));
    

    Next step is to convert all your .po files into Qt XML translation files (.ts) with following command (you do this only once per .po file):

    lconvert -locations none test_es.po -o test_es.ts
    

    Example output (test_es.ts):

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE TS>
    <TS version="2.0" language="es_ES">
    <context>
        <name></name>
        <message>
            <source>Do you want to quit?</source>
            <translation>¿Quiere salir?</translation>
        </message>
    </context>
    </TS>
    

    Last step is to "compile" your Qt .ts file into binary format .qm with lrelease. And from now on, all new strings you need will be added to your Qt app source code using tr(), extracted for translation with lupdate and once translated (by using QLinguist for example) converted into binary form with lrelease.



  • @Christian-Ehrlicher Thanks! Your proposal works great!

    Edit: I can't mark your reply as solution, so I've marked my own to close the thread.

    [edit: fixed the answer to me marked SGaist]


Log in to reply