1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example linguist/hellotr
30 \title Hello tr() Example
32 \brief The Hello tr() example is a small Hello World program with a Latin translation.
34 The screenshot below shows the English version.
36 \image linguist-hellotr_en.png
38 See the \l{Qt Linguist manual} for more information about
39 translating Qt application.
41 \section1 Line by Line Walkthrough
44 \snippet examples/linguist/hellotr/main.cpp 0
46 This line includes the definition of the QTranslator class.
47 Objects of this class provide translations for user-visible text.
49 \snippet examples/linguist/hellotr/main.cpp 5
51 Creates a QTranslator object without a parent.
53 \snippet examples/linguist/hellotr/main.cpp 6
55 Tries to load a file called \c hellotr_la.qm (the \c .qm file extension is
56 implicit) that contains Latin translations for the source texts used in
57 the program. No error will occur if the file is not found.
59 \snippet examples/linguist/hellotr/main.cpp 7
61 Adds the translations from \c hellotr_la.qm to the pool of translations used
64 \snippet examples/linguist/hellotr/main.cpp 8
66 Creates a push button that displays "Hello world!". If \c hellotr_la.qm
67 was found and contains a translation for "Hello world!", the
68 translation appears; if not, the source text appears.
70 All classes that inherit QObject have a \c tr() function. Inside
71 a member function of a QObject class, we simply write \c tr("Hello
72 world!") instead of \c QPushButton::tr("Hello world!") or \c
73 QObject::tr("Hello world!").
75 \section1 Running the Application in English
77 Since we haven't made the translation file \c hellotr_la.qm, the source text
78 is shown when we run the application:
80 \image linguist-hellotr_en.png
82 \section1 Creating a Latin Message File
84 The first step is to create a project file, \c hellotr.pro, that lists
85 all the source files for the project. The project file can be a qmake
86 project file, or even an ordinary makefile. Any file that contains
88 \snippet examples/linguist/hellotr/hellotr.pro 0
89 \snippet examples/linguist/hellotr/hellotr.pro 1
91 will work. \c TRANSLATIONS specifies the message files we want to
92 maintain. In this example, we just maintain one set of translations,
95 Note that the file extension is \c .ts, not \c .qm. The \c .ts
96 translation source format is designed for use during the
97 application's development. Programmers or release managers run
98 the \c lupdate program to generate and update TS files with
99 the source text that is extracted from the source code.
100 Translators read and update the TS files using \e {Qt
101 Linguist} adding and editing their translations.
103 The TS format is human-readable XML that can be emailed directly
104 and is easy to put under version control. If you edit this file
105 manually, be aware that the default encoding for XML is UTF-8, not
106 Latin1 (ISO 8859-1). One way to type in a Latin1 character such as
107 '\oslash' (Norwegian o with slash) is to use an XML entity:
108 "\ø". This will work for any Unicode 4.0 character.
110 Once the translations are complete the \c lrelease program is used to
111 convert the TS files into the QM Qt message file format. The
112 QM format is a compact binary format designed to deliver very
113 fast lookup performance. Both \c lupdate and \c lrelease read all the
114 project's source and header files (as specified in the HEADERS and
115 SOURCES lines of the project file) and extract the strings that
116 appear in \c tr() function calls.
118 \c lupdate is used to create and update the message files (\c hellotr_la.ts
119 in this case) to keep them in sync with the source code. It is safe to
120 run \c lupdate at any time, as \c lupdate does not remove any
121 information. For example, you can put it in the makefile, so the TS
122 files are updated whenever the source changes.
124 Try running \c lupdate right now, like this:
126 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 0
128 (The \c -verbose option instructs \c lupdate to display messages that
129 explain what it is doing.) You should now have a file \c hellotr_la.ts in
130 the current directory, containing this:
132 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 1
134 You don't need to understand the file format since it is read and
135 updated using tools (\c lupdate, \e {Qt Linguist}, \c lrelease).
137 \section1 Translating to Latin with Qt Linguist
139 We will use \e {Qt Linguist} to provide the translation, although
140 you can use any XML or plain text editor to enter a translation into a
143 To start \e {Qt Linguist}, type
145 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 2
147 You should now see the text "QPushButton" in the top left pane.
148 Double-click it, then click on "Hello world!" and enter "Orbis, te
149 saluto!" in the \gui Translation pane (the middle right of the
150 window). Don't forget the exclamation mark!
152 Click the \gui Done checkbox and choose \gui File|Save from the
153 menu bar. The TS file will no longer contain
155 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 3
157 but instead will have
159 \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 4
161 \section1 Running the Application in Latin
163 To see the application running in Latin, we have to generate a QM
164 file from the TS file. Generating a QM file can be achieved
165 either from within \e {Qt Linguist} (for a single TS file), or
166 by using the command line program \c lrelease which will produce one
167 QM file for each of the TS files listed in the project file.
168 Generate \c hellotr_la.qm from \c hellotr_la.ts by choosing
169 \gui File|Release from \e {Qt Linguist}'s menu bar and pressing
170 \gui Save in the file save dialog that pops up. Now run the \c hellotr
171 program again. This time the button will be labelled "Orbis, te
174 \image linguist-hellotr_la.png