Using gettext instead of Qt translation with UI files
-
wrote on 30 May 2018, 13:52 last edited by La Bamba
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:"));
-
Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.
-
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. -
Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.
-
Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.
wrote on 30 May 2018, 16:50 last edited by@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.
-
Just derive from QTranslator, overwrite translate() ( http://doc.qt.io/qt-5/qtranslator.html#translate ) and call gettext there.
wrote on 1 Jun 2018, 13:56 last edited by SGaist 6 Jan 2018, 21:04@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]
-
wrote on 24 Jun 2022, 07:13 last edited by hyhhyh
HI, How to rewrite translator() , (.ui file); please answer,thanks
-
@hyhhyh
Hi and welcome to the forums.
Do you mean how to use gettext inside the function ?Else simply inherited from QTranslator and add a new
QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
to it where you use gettext. -
@hyhhyh
Hi and welcome to the forums.
Do you mean how to use gettext inside the function ?Else simply inherited from QTranslator and add a new
QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
to it where you use gettext.wrote on 28 Jun 2022, 09:20 last edited by@mrjj
I have solved
great, and thank you for sharing the solution to your issue.QString translate(const char *context, const char *sourceText,
const char *disambiguation = nullptr, int n = -1) const override;
{
return QString::fromUtf8(dgettext("ovfui", sourceText));
}
So you can extract all of your translation with:
lupdate
Then use lconvert to obtain a po file:
lconvert -of po -o file.po file.ts