1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at qt-info@nokia.com.
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \example linguist/trollprint
|
---|
30 | \title Troll Print Example
|
---|
31 |
|
---|
32 | Troll Print is an example application that lets the user choose
|
---|
33 | printer settings. It comes in two versions: English and
|
---|
34 | Portuguese.
|
---|
35 |
|
---|
36 | \image linguist-trollprint_10_en.png
|
---|
37 |
|
---|
38 | We've included a translation file, \c trollprint_pt.ts, which contains some
|
---|
39 | Portuguese translations for this example.
|
---|
40 |
|
---|
41 | We will consider two releases of the same application: Troll Print
|
---|
42 | 1.0 and 1.1. We will learn to reuse the translations created for one
|
---|
43 | release in a subsequent release. (In this tutorial, you need to edit
|
---|
44 | some source files. It's probably best to copy all the files to a new
|
---|
45 | temporary directory and work from there.)
|
---|
46 |
|
---|
47 | See the \l{Qt Linguist manual} for more information about
|
---|
48 | translating Qt application.
|
---|
49 |
|
---|
50 | \section1 Line by Line Walkthrough
|
---|
51 |
|
---|
52 | The \c PrintPanel class is defined in \c printpanel.h.
|
---|
53 |
|
---|
54 | \snippet examples/linguist/trollprint/printpanel.h 0
|
---|
55 |
|
---|
56 | \c PrintPanel is a QWidget. It needs the \c Q_OBJECT macro for \c
|
---|
57 | tr() to work properly.
|
---|
58 |
|
---|
59 | The implementation file is \c printpanel.cpp.
|
---|
60 |
|
---|
61 | \snippet examples/linguist/trollprint/printpanel.cpp 0
|
---|
62 |
|
---|
63 | Some of the code is commented out in Troll Print 1.0; you will
|
---|
64 | uncomment it later, for Troll Print 1.1.
|
---|
65 |
|
---|
66 | \snippet examples/linguist/trollprint/printpanel.cpp 1
|
---|
67 | \snippet examples/linguist/trollprint/printpanel.cpp 2
|
---|
68 |
|
---|
69 | Notice the two occurrences of \c tr("Enabled") and of \c
|
---|
70 | tr("Disabled") in PrintPanel. Since both "Enabled"s and "Disabled"s
|
---|
71 | appear in the same context \e {Qt Linguist} will only display one
|
---|
72 | occurrence of each and will use the same translations for the
|
---|
73 | duplicates that it doesn't display. Whilst this is a useful
|
---|
74 | timesaver, in some languages, such as Portuguese, the second
|
---|
75 | occurrence requires a separate translation. We will see how \e {Qt
|
---|
76 | Linguist} can be made to display all the occurrences for separate
|
---|
77 | translation shortly.
|
---|
78 |
|
---|
79 | The header file for \c MainWindow, \c mainwindow.h, contains no
|
---|
80 | surprises. In the implementation, \c mainwindow.cpp, we have some
|
---|
81 | user-visible source texts that must be marked for translation.
|
---|
82 |
|
---|
83 | \snippet examples/linguist/trollprint/mainwindow.cpp 0
|
---|
84 |
|
---|
85 | We must translate the window title.
|
---|
86 |
|
---|
87 | \snippet examples/linguist/trollprint/mainwindow.cpp 1
|
---|
88 | \snippet examples/linguist/trollprint/mainwindow.cpp 3
|
---|
89 |
|
---|
90 | We also need to translate the actions and menus. Note that the
|
---|
91 | two argument form of \c tr() is used for the keyboard
|
---|
92 | accelerator, "Ctrl+Q", since the second argument is the only clue
|
---|
93 | the translator has to indicate what function that accelerator
|
---|
94 | will perform.
|
---|
95 |
|
---|
96 | \snippet examples/linguist/trollprint/main.cpp 0
|
---|
97 |
|
---|
98 | The \c main() function in \c main.cpp is the same as the one in
|
---|
99 | the \l{linguist/arrowpad}{Arrow Pad} example. In particular, it
|
---|
100 | chooses a translation file based on the current locale.
|
---|
101 |
|
---|
102 | \section1 Running Troll Print 1.0 in English and in Portuguese
|
---|
103 |
|
---|
104 | We will use the translations in the \c trollprint_pt.ts file that is provided.
|
---|
105 |
|
---|
106 | Set the \c LANG environment variable to \c pt, and then run \c
|
---|
107 | trollprint. You should still see the English version. Now run \c
|
---|
108 | lrelease, e.g. \c {lrelease trollprint.pro}, and then run the
|
---|
109 | example again. Now you should see the Portuguese edition (Troll
|
---|
110 | Imprimir 1.0):
|
---|
111 |
|
---|
112 | \image linguist-trollprint_10_pt_bad.png
|
---|
113 |
|
---|
114 | Whilst the translation has appeared correctly, it is in fact wrong. In
|
---|
115 | good Portuguese, the second occurrence of "Enabled" should be
|
---|
116 | "Ativadas", not "Ativado" and the ending for the second translation of
|
---|
117 | "Disabled" must change similarly too.
|
---|
118 |
|
---|
119 | If you open \c trollprint_pt.ts using \e {Qt Linguist}, you will see that
|
---|
120 | there is just one occurrence of "Enabled" and of "Disabled" in the
|
---|
121 | translation source file, even though there are two of each in the
|
---|
122 | source code. This is because \e {Qt Linguist} tries to minimize the
|
---|
123 | translator's work by using the same translation for duplicate source
|
---|
124 | texts. In cases such as this where an identical translation is wrong,
|
---|
125 | the programmer must disambiguate the duplicate occurrences. This is
|
---|
126 | easily achieved by using the two argument form of \c tr().
|
---|
127 |
|
---|
128 | We can easily determine which file must be changed because the
|
---|
129 | translator's "context" is in fact the class name for the class where
|
---|
130 | the texts that must be changed appears. In this case the file is \c
|
---|
131 | printpanel.cpp, where there are four lines to change. Add the
|
---|
132 | second argument "two-sided" in the appropriate \c tr() calls to the
|
---|
133 | first pair of radio buttons:
|
---|
134 |
|
---|
135 | \snippet doc/src/snippets/code/doc_src_examples_trollprint.qdoc 0
|
---|
136 |
|
---|
137 | and add the second argument "colors" in the appropriate \c tr() calls
|
---|
138 | for the second pair of radio buttons:
|
---|
139 |
|
---|
140 | \snippet doc/src/snippets/code/doc_src_examples_trollprint.qdoc 1
|
---|
141 |
|
---|
142 | Now run \c lupdate and open \c trollprint_pt.ts with \e {Qt Linguist}. You
|
---|
143 | should now see two changes.
|
---|
144 |
|
---|
145 | First, the translation source file now contains \e three "Enabled",
|
---|
146 | "Disabled" pairs. The first pair is marked "(obs.)" signifying that they
|
---|
147 | are obsolete. This is because these texts appeared in \c tr() calls that
|
---|
148 | have been replaced by new calls with two arguments. The second pair has
|
---|
149 | "two-sided" as their comment, and the third pair has "colors" as their
|
---|
150 | comment. The comments are shown in the \gui {Source text and comments}
|
---|
151 | area in \e {Qt Linguist}.
|
---|
152 |
|
---|
153 | Second, the translation text "Ativado" and "Desativado" have been
|
---|
154 | automatically used as translations for the new "Enabled" and "Disabled"
|
---|
155 | texts, again to minimize the translator's work. Of course in this case
|
---|
156 | these are not correct for the second occurrence of each word, but they
|
---|
157 | provide a good starting point.
|
---|
158 |
|
---|
159 | Change the second "Ativado" into "Ativadas" and the second
|
---|
160 | "Desativado" into "Desativadas", then save and quit. Run \c lrelease
|
---|
161 | to obtain an up-to-date binary \c trollprint_pt.qm file, and run Troll Print
|
---|
162 | (or rather Troll Imprimir).
|
---|
163 |
|
---|
164 | \image linguist-trollprint_10_pt_good.png
|
---|
165 |
|
---|
166 | The second argument to \c tr() calls, called "comments" in \e {Qt
|
---|
167 | Linguist}, distinguish between identical source texts that occur in
|
---|
168 | the same context (class). They are also useful in other cases to give
|
---|
169 | clues to the translator, and in the case of Ctrl key accelerators are
|
---|
170 | the only means of conveying the function performed by the accelerator to
|
---|
171 | the translator.
|
---|
172 |
|
---|
173 | An additional way of helping the translator is to provide information on
|
---|
174 | how to navigate to the particular part of the application that contains
|
---|
175 | the source texts they must translate. This helps them see the context
|
---|
176 | in which the translation appears and also helps them to find and test
|
---|
177 | the translations. This can be achieved by using a \c TRANSLATOR comment
|
---|
178 | in the source code:
|
---|
179 |
|
---|
180 | \snippet doc/src/snippets/code/doc_src_examples_trollprint.qdoc 2
|
---|
181 |
|
---|
182 | Try adding these comments to some source files, particularly to
|
---|
183 | dialog classes, describing the navigation necessary to reach the
|
---|
184 | dialogs. You could also add them to the example files, e.g. \c
|
---|
185 | mainwindow.cpp and \c printpanel.cpp are appropriate files. Run \c
|
---|
186 | lupdate and then start \e {Qt Linguist} and load in \c trollprint_pt.ts.
|
---|
187 | You should see the comments in the \gui {Source text and comments} area
|
---|
188 | as you browse through the list of source texts.
|
---|
189 |
|
---|
190 | Sometimes, particularly with large programs, it can be difficult for
|
---|
191 | the translator to find their translations and check that they're
|
---|
192 | correct. Comments that provide good navigation information can save
|
---|
193 | them time:
|
---|
194 |
|
---|
195 | \snippet doc/src/snippets/code/doc_src_examples_trollprint.qdoc 3
|
---|
196 |
|
---|
197 | \section1 Troll Print 1.1
|
---|
198 |
|
---|
199 | We'll now prepare release 1.1 of Troll Print. Start your favorite text
|
---|
200 | editor and follow these steps:
|
---|
201 |
|
---|
202 | \list
|
---|
203 | \o Uncomment the two lines that create a QLabel with the text
|
---|
204 | "\<b\>TROLL PRINT\</b\>" in \c printpanel.cpp.
|
---|
205 | \o Word-tidying: Replace "2-sided" by "Two-sided" in \c printpanel.cpp.
|
---|
206 | \o Replace "1.0" with "1.1" everywhere it occurs in \c mainwindow.cpp.
|
---|
207 | \o Update the copyright year to 1999-2000 in \c mainwindow.cpp.
|
---|
208 | \endlist
|
---|
209 |
|
---|
210 | (Of course the version number and copyright year would be consts or
|
---|
211 | #defines in a real application.)
|
---|
212 |
|
---|
213 | Once finished, run \c lupdate, then open \c trollprint_pt.ts in \e {Qt
|
---|
214 | Linguist}. The following items are of special interest:
|
---|
215 |
|
---|
216 | \list
|
---|
217 | \o \c MainWindow
|
---|
218 | \list
|
---|
219 | \o Troll Print 1.0 - marked "(obs.)", obsolete
|
---|
220 | \o About Troll Print 1.0 - marked "(obs.)", obsolete
|
---|
221 | \o Troll Print 1.0. Copyright 1999 Software, Inc. -
|
---|
222 | marked obsolete
|
---|
223 | \o Troll Print 1.1 - automatically translated as
|
---|
224 | "Troll Imprimir 1.1"
|
---|
225 | \o About Troll Print 1.1 - automatically translated as
|
---|
226 | "Troll Imprimir 1.1"
|
---|
227 | \o Troll Print 1.1. Copyright 1999-2000 Software,
|
---|
228 | Inc. - automatically translated as "Troll Imprimir 1.1.
|
---|
229 | Copyright 1999-2000 Software, Inc."
|
---|
230 | \endlist
|
---|
231 | \o \c PrintPanel
|
---|
232 | \list
|
---|
233 | \o 2-sided - marked "(obs.)", obsolete
|
---|
234 | \o \<b\>TROLL PRINT\</b\> - unmarked, i.e. untranslated
|
---|
235 | \o Two-sided - unmarked, i.e. untranslated.
|
---|
236 | \endlist
|
---|
237 | \endlist
|
---|
238 |
|
---|
239 | Notice that \c lupdate works hard behind the scenes to make revisions
|
---|
240 | easier, and it's pretty smart with numbers.
|
---|
241 |
|
---|
242 | Go over the translations in \c MainWindow and mark these as "done".
|
---|
243 | Translate "\<b\>TROLL PRINT\</b\>" as "\<b\>TROLL IMPRIMIR\</b\>".
|
---|
244 | When you're translating "Two-sided", press the \gui {Guess Again}
|
---|
245 | button to translate "Two-sided", but change the "2" into "Dois".
|
---|
246 |
|
---|
247 | Save and quit, then run \c lrelease. The Portuguese version
|
---|
248 | should look like this:
|
---|
249 |
|
---|
250 | \image linguist-trollprint_11_pt.png
|
---|
251 |
|
---|
252 | Choose \gui{Ajuda|Sobre} (\gui{Help|About}) to see the about box.
|
---|
253 |
|
---|
254 | If you choose \gui {Ajuda|Sobre Qt} (\gui {Help|About Qt}), you'll get
|
---|
255 | an English dialog. Oops! Qt itself needs to be translated. See
|
---|
256 | \l{Internationalization with Qt} for details.
|
---|
257 |
|
---|
258 | Now set \c LANG=en to get the original English version:
|
---|
259 |
|
---|
260 | \image linguist-trollprint_11_en.png
|
---|
261 | */
|
---|