Translating a small Hello World program to Latin.
The screenshot shows the English version.
见 Qt Linguist 手册 for more information about translating Qt application.
This line includes the definition of the QTranslator class. Objects of this class provide translations for user-visible text.
创建 QTranslator object without a parent.
if (!translator.load("hellotr_la")) return 1;
Tries to load a file called
file extension is implicit) that contains Latin translations for the source texts used in the program. No error will occur if the file is not found.
Adds the translations from
to the pool of translations used by the program.
QPushButton hello(QPushButton::tr("Hello world!"));
Creates a push button that displays "Hello world!". If
was found and contains a translation for "Hello world!", the translation appears; if not, the source text appears.
All classes that inherit
function. Inside a member function of a
class, we simply write
Since we haven't made the translation file
, the source text is shown when we run the application:
The first step is to create a project file,
, that lists all the source files for the project. The project file can be a qmake project file, or even an ordinary makefile. Any file that contains
SOURCES = main.cpp TRANSLATIONS = hellotr_la.ts
specifies the message files we want to maintain. In this example, we just maintain one set of translations, namely Latin.
Note that the file extension is
translation source format is designed for use during the application's development. Programmers or release managers run the
program to generate and update TS files with the source text that is extracted from the source code. Translators read and update the TS files using
adding and editing their translations.
The TS format is human-readable XML that can be emailed directly and is easy to put under version control. If you edit this file manually, be aware that the default encoding for XML is UTF-8, not Latin1 (ISO 8859-1). One way to type in a Latin1 character such as 'ø' (Norwegian o with slash) is to use an XML entity: "ø". This will work for any Unicode 4.0 character.
Once the translations are complete the
program is used to convert the TS files into the QM Qt message file format. The QM format is a compact binary format designed to deliver very fast lookup performance. Both
read all the project's source and header files (as specified in the HEADERS and SOURCES lines of the project file) and extract the strings that appear in
is used to create and update the message files (
in this case) to keep them in sync with the source code. It is safe to run
at any time, as
does not remove any information. For example, you can put it in the makefile, so the TS files are updated whenever the source changes.
right now, like this:
lupdate -verbose hellotr.pro
to display messages that explain what it is doing.) You should now have a file
in the current directory, containing this:
<!DOCTYPE TS><TS> <context> <name>QPushButton</name> <message> <source>Hello world!</source> <translation type="unfinished"></translation> </message> </context> </TS>
You don't need to understand the file format since it is read and updated using tools (
We will use Qt Linguist to provide the translation, although you can use any XML or plain text editor to enter a translation into a TS file.
To start Qt Linguist , type
You should now see the text "QPushButton" in the top left pane. Double-click it, then click on "Hello world!" and enter "Orbis, te saluto!" in the Translation pane (the middle right of the window). Don't forget the exclamation mark!
点击 Done checkbox and choose File|Save from the menu bar. The TS file will no longer contain
but instead will have
<translation>Orbis, te saluto!</translation>
To see the application running in Latin, we have to generate a QM file from the TS file. Generating a QM file can be achieved either from within
(for a single TS file), or by using the command line program
which will produce one QM file for each of the TS files listed in the project file. Generate
's menu bar and pressing
in the file save dialog that pops up. Now run the
program again. This time the button will be labelled "Orbis, te saluto!".