Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt translate strings from non-source files

I have a Qt project which uses XML files. Those XML files contain human-readable text and this text should be translated by using the Qt tools (lupdate, lrelease, QtLinguist).

The question is if it is possible to generate entries in .ts file via lupdate without duplicating the strings from the XML files in a source code file by using the QT_TR_NOOP() macro and friends? Or in general, how do you translate strings in non-source files for Qt projects?

like image 462
Sascha Avatar asked Feb 03 '11 22:02

Sascha


People also ask

What is TR () in Qt?

tr() is a function that marks a string for international translation. At runtime, the input string will be translated to an appropriate language, based on translations configured in the Qt Linguist tool.

What is qsTr in Qt?

Use qsTr() for all Literal User Interface Strings Strings in QML can be marked for translation using the qsTr(), qsTranslate(), qsTrId(), QT_TR_NOOP(), QT_TRANSLATE_NOOP(), and QT_TRID_NOOP() functions. The most common way of marking strings is with the qsTr() function.

What is Qt_translate_noop?

QT_TRANSLATE_NOOP(context, sourceText)Marks the UTF-8 encoded string literal sourceText for delayed translation in the given context. The context is typically a class name and also needs to be specified as a string literal. The macro tells lupdate to collect the string, and expands to sourceText itself.

What is a TS file in Qt?

Qt ts files are xml files which are used by the Qt Linguist application. They also get used to provide automatic translations for Qt Applications.


2 Answers

We had the same problem : XML files containing human readable strings.

Our solution was to make sure that human readable strings in the XML files were easy to extract (we put them in a LABEL attribute) and we developped a small tool which would parse the XML files, extract the strings, generate a context (by extracting data from the XML file), and then generating a CPP header file containing a list of QT_TR_NOOP().

This file was added to our project file (.pro) that was used by lupdate.

This solution was fine for us but we had to be very careful about two things :

  • run this tool each time the content of an XML file changed.
  • make sure the XML files are UTF-8 encoded.
like image 169
Jérôme Avatar answered Oct 02 '22 13:10

Jérôme


You can translate anything you want at runtime by using tr(), as long as the .qm file has a matching translation/context. It shouldn't make any difference whether lupdate extracted it or not.

I don't know how to make lupdate to extract strings from arbitrary XML, but that doesn't mean you can't use linguist.

  1. .ts files are also XML; it should be easy to make an XSLT that transforms your XML into a .ts file. If you want to target something standard instead of just Qt, lupdate(and linguist) can process also XLIFF files.
  2. you can have multiple .ts files (just call QTranslator::load more than once when setting it up)

If you really want to have it all in one file for the translator, have your XSLT copy the lupdate-generated file into its output.

As long as you use a context name that doesn't duplicate something used in the source code, this shouldn't be any different (from Qt's point of view) from the way many apps load a .qm for each DLL that has GUI.

like image 37
puetzk Avatar answered Oct 02 '22 14:10

puetzk