Update copyright headers
[qt:qt.git] / doc / src / examples / hellotr.qdoc
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
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.
16 **
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.
24 ** $QT_END_LICENSE$
25 **
26 ****************************************************************************/
27
28 /*!
29     \example linguist/hellotr
30     \title Hello tr() Example
31
32     \brief The Hello tr() example is a small Hello World program with a Latin translation.
33     
34     The screenshot below shows the English version.
35
36     \image linguist-hellotr_en.png
37
38     See the \l{Qt Linguist manual} for more information about
39     translating Qt application.
40
41     \section1 Line by Line Walkthrough
42
43
44     \snippet examples/linguist/hellotr/main.cpp 0
45
46     This line includes the definition of the QTranslator class. 
47     Objects of this class provide translations for user-visible text.
48
49     \snippet examples/linguist/hellotr/main.cpp 5
50
51     Creates a QTranslator object without a parent.
52
53     \snippet examples/linguist/hellotr/main.cpp 6
54
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.
58
59     \snippet examples/linguist/hellotr/main.cpp 7
60
61     Adds the translations from \c hellotr_la.qm to the pool of translations used
62     by the program.
63
64     \snippet examples/linguist/hellotr/main.cpp 8
65
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.
69
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!").
74
75     \section1 Running the Application in English
76
77     Since we haven't made the translation file \c hellotr_la.qm, the source text
78     is shown when we run the application:
79
80     \image linguist-hellotr_en.png
81
82     \section1 Creating a Latin Message File
83
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
87
88     \snippet examples/linguist/hellotr/hellotr.pro 0
89     \snippet examples/linguist/hellotr/hellotr.pro 1
90
91     will work. \c TRANSLATIONS specifies the message files we want to
92     maintain. In this example, we just maintain one set of translations,
93     namely Latin.
94
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.
102
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.
109
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.
117
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.
123
124     Try running \c lupdate right now, like this:
125
126     \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 0
127
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:
131
132     \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 1
133
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).
136
137     \section1 Translating to Latin with Qt Linguist
138
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
141     TS file.
142
143     To start \e {Qt Linguist}, type
144
145     \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 2
146
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!
151
152     Click the \gui Done checkbox and choose \gui File|Save from the
153     menu bar. The TS file will no longer contain
154
155     \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 3
156
157     but instead will have
158
159     \snippet doc/src/snippets/code/doc_src_examples_hellotr.qdoc 4
160
161     \section1 Running the Application in Latin
162
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
172     saluto!".
173
174     \image linguist-hellotr_la.png
175 */