1 | \chapter Programmers
|
---|
2 |
|
---|
3 | Support for multiple languages is extremely simple in Qt
|
---|
4 | applications, and adds little overhead to the programmer's workload.
|
---|
5 |
|
---|
6 | Qt minimizes the performance cost of using translations by
|
---|
7 | translating the phrases for each window as they are created. In most
|
---|
8 | applications the main window is created just once. Dialogs are often
|
---|
9 | created once and then shown and hidden as required. Once the initial
|
---|
10 | translation has taken place there is no further runtime overhead for
|
---|
11 | the translated windows. Only those windows that are created,
|
---|
12 | destroyed and subsequently created will have a translation
|
---|
13 | performance cost.
|
---|
14 |
|
---|
15 | Creating applications that can switch language at runtime is possible
|
---|
16 | with Qt, but requires a certain amount of programmer intervention and
|
---|
17 | will of course incur some runtime performance cost.
|
---|
18 |
|
---|
19 | \section1 Making the Application Translation Aware
|
---|
20 |
|
---|
21 | Programmers should make their application look for and load the
|
---|
22 | appropriate translation file and mark user-visible text and Ctrl
|
---|
23 | keyboard accelerators as targets for translation.
|
---|
24 |
|
---|
25 | Each piece of text that requires translating requires context to help
|
---|
26 | the translator identify where in the program the text occurs. In the
|
---|
27 | case of multiple identical texts that require different translations,
|
---|
28 | the translator also requires some information to disambiguate the
|
---|
29 | source texts. Marking text for translation will automatically cause
|
---|
30 | the class name to be used as basic context information. In some cases
|
---|
31 | the programmer may be required to add additional information to help
|
---|
32 | the translator.
|
---|
33 |
|
---|
34 | \section2 Creating Translation Files
|
---|
35 |
|
---|
36 | \index .ts Files
|
---|
37 | \index Translation Source Files
|
---|
38 |
|
---|
39 | Translation files consist of all the user-visible text and Ctrl key
|
---|
40 | accelerators in an application and translations of that text.
|
---|
41 | Translation files are created as follows:
|
---|
42 |
|
---|
43 | \index lupdate
|
---|
44 | \index lrelease
|
---|
45 |
|
---|
46 | \list 1
|
---|
47 | \i Run \l lupdate initially to generate the first set of \c .ts
|
---|
48 | translation source files with all the user-visible text but no
|
---|
49 | translations.
|
---|
50 | \i The \c .ts files are given to the translator who adds translations
|
---|
51 | using \e {Qt Linguist}. \e {Qt Linguist} takes care of any changed
|
---|
52 | or deleted source text.
|
---|
53 | \i Run \l lupdate to incorporate any new text added to the
|
---|
54 | application. \l lupdate synchronizes the user-visible text from the
|
---|
55 | application with the translations; it does not destroy any data.
|
---|
56 | \i Steps 2 and 3 are repeated as often as necessary.
|
---|
57 | \i When a release of the application is needed \l lrelease is run to
|
---|
58 | read the \c .ts files and produce the \c .qm files used by the
|
---|
59 | application at runtime.
|
---|
60 | \endlist
|
---|
61 |
|
---|
62 | \index .pro Files
|
---|
63 | \index Project Files
|
---|
64 | \index qmake!Project Files
|
---|
65 |
|
---|
66 | For \l lupdate to work successfully, it must know which translation
|
---|
67 | files to produce. The files are simply listed in the application's \c
|
---|
68 | .pro Qt project file, for example:
|
---|
69 | \quotefile tt2/tt2.pro
|
---|
70 | \skipto TRANSLATIONS
|
---|
71 | \printline TRANSLATIONS
|
---|
72 | \printline
|
---|
73 |
|
---|
74 | See the \link lupdate "lupdate" \endlink and \link lrelease
|
---|
75 | "lrelease" \endlink sections.
|
---|
76 |
|
---|
77 | \section2 Loading Translations
|
---|
78 |
|
---|
79 | \quotefile tt1/main.cpp
|
---|
80 | \skipto main(
|
---|
81 | \printline main(
|
---|
82 | \printuntil QApplication
|
---|
83 |
|
---|
84 | \index main()
|
---|
85 |
|
---|
86 | This is how a simple \c main() function of a Qt application begins.
|
---|
87 |
|
---|
88 | \index QTranslator!load()
|
---|
89 | \index load()!QTranslator
|
---|
90 | \index QApplication!installTranslator()
|
---|
91 | \index installTranslator()!QApplication
|
---|
92 |
|
---|
93 | \quotefile tt1/main.cpp
|
---|
94 | \skipto main(
|
---|
95 | \printline main(
|
---|
96 | \printuntil app.installTrans
|
---|
97 |
|
---|
98 | For a translation-aware application a translator object is created, a
|
---|
99 | translation is loaded and the translator object installed into the
|
---|
100 | application.
|
---|
101 |
|
---|
102 | \quotefile tt2/main.cpp
|
---|
103 | \skipto main(
|
---|
104 | \printline main(
|
---|
105 | \printuntil app.installTrans
|
---|
106 |
|
---|
107 | In production applications a more flexible approach, for example,
|
---|
108 | loading translations according to locale, might be more appropriate. If
|
---|
109 | the \c .ts files are all named according to a convention such as
|
---|
110 | \e appname_locale, e.g. \c tt2_fr, \c tt2_de etc, then the
|
---|
111 | code above will load the current locale's translation at runtime.
|
---|
112 |
|
---|
113 | If there is no translation file for the current locale the application
|
---|
114 | will fall back to using the original source text.
|
---|
115 |
|
---|
116 | \section2 Making the Application Translate User-Visible Strings
|
---|
117 |
|
---|
118 | \index tr()
|
---|
119 | \index QObject!tr()
|
---|
120 |
|
---|
121 | User-visible strings are marked as translation targets by wrapping them
|
---|
122 | in a \c tr() call, for example:
|
---|
123 | \code
|
---|
124 | button = new QPushButton( "&Quit", this );
|
---|
125 | \endcode
|
---|
126 |
|
---|
127 | would become
|
---|
128 |
|
---|
129 | \code
|
---|
130 | button = new QPushButton( tr("&Quit"), this);
|
---|
131 | \endcode
|
---|
132 |
|
---|
133 | \index Q_OBJECT
|
---|
134 |
|
---|
135 | All \l QObject subclasses that use the \c Q_OBJECT macro implement
|
---|
136 | the \c tr() function.
|
---|
137 |
|
---|
138 | Although the \c tr() call is normally made directly since it is
|
---|
139 | usually called as a member function of a \l QObject subclass, in
|
---|
140 | other cases an explicit class name can be supplied, for example:
|
---|
141 |
|
---|
142 | \code
|
---|
143 | QPushButton::tr("&Quit")
|
---|
144 | \endcode
|
---|
145 |
|
---|
146 | or
|
---|
147 |
|
---|
148 | \code
|
---|
149 | QObject::tr("&Quit")
|
---|
150 | \endcode
|
---|
151 |
|
---|
152 | \section2 Distinguishing Identical Strings That Require Different
|
---|
153 | Translations
|
---|
154 |
|
---|
155 | \index Translation Contexts
|
---|
156 | \index Contexts!for Translation
|
---|
157 | \index lupdate
|
---|
158 |
|
---|
159 | The \l lupdate program automatically provides a \e context for every
|
---|
160 | source text. This context is the class name of the class that contains
|
---|
161 | the \c tr() call. This is sufficient in the vast majority of cases.
|
---|
162 | Sometimes however, the translator will need further information to
|
---|
163 | uniquely identify a source text; for example, a dialog that contained
|
---|
164 | two separate frames, each of which contained an "Enabled" option would
|
---|
165 | need each identified because in some languages the translation would
|
---|
166 | differ between the two. This is easily achieved using the
|
---|
167 | two argument form of the \c tr() call, e.g.
|
---|
168 |
|
---|
169 | \code
|
---|
170 | rbc = new QRadioButton( tr("Enabled", "Color frame"), this );
|
---|
171 | \endcode
|
---|
172 |
|
---|
173 | and
|
---|
174 |
|
---|
175 | \code
|
---|
176 | rbh = new QRadioButton( tr("Enabled", "Hue frame"), this );
|
---|
177 | \endcode
|
---|
178 |
|
---|
179 | \index Ctrl Key
|
---|
180 |
|
---|
181 | Ctrl key accelerators are also translatable:
|
---|
182 |
|
---|
183 | \quotefile tt3/mainwindow.cpp
|
---|
184 | \skipto quit()
|
---|
185 | \printline quit()
|
---|
186 | \printuntil Quit
|
---|
187 |
|
---|
188 | It is strongly recommended that the two argument form of \c tr() is used
|
---|
189 | for Ctrl key accelerators. The second argument is the only clue the
|
---|
190 | translator has as to the function performed by the accelerator.
|
---|
191 |
|
---|
192 | \section2 Helping The Translator With Navigation Information
|
---|
193 |
|
---|
194 | \index TRANSLATOR!in Comments
|
---|
195 | \index Translator Comments
|
---|
196 | \index Comments!for Translators
|
---|
197 |
|
---|
198 | In large complex applications it may be difficult for the translator to
|
---|
199 | see where a particular source text comes from. This problem can be
|
---|
200 | solved by adding a comment using the keyword \e TRANSLATOR which
|
---|
201 | describes the navigation steps to reach the text in question; e.g.
|
---|
202 |
|
---|
203 | \code
|
---|
204 | /* TRANSLATOR FindDialog
|
---|
205 |
|
---|
206 | Choose Edit|Find from the menu bar or press Ctrl+F to pop up the
|
---|
207 | Find dialog.
|
---|
208 | */
|
---|
209 | \endcode
|
---|
210 |
|
---|
211 | These comments are particularly useful for widget classes.
|
---|
212 |
|
---|
213 | \section2 Coping With C++ Namespaces
|
---|
214 |
|
---|
215 | \index Namespaces
|
---|
216 | \index C++!Namespaces
|
---|
217 | \index lupdate
|
---|
218 |
|
---|
219 | C++ namespaces and the \c {using namespace} statement can confuse
|
---|
220 | \l lupdate. It will interpret \c MyClass::tr() as meaning just
|
---|
221 | that, not as \c MyNamespace::MyClass::tr(), even if \c MyClass is
|
---|
222 | defined in the \c MyNamespace namespace. Runtime translation of
|
---|
223 | these strings will fail because of that.
|
---|
224 |
|
---|
225 | \index TRANSLATOR!in Comments
|
---|
226 | \index Translator Comments
|
---|
227 | \index Comments!for Translators
|
---|
228 |
|
---|
229 | You can work around this limitation by putting a \e TRANSLATOR
|
---|
230 | comment at the beginning of the source files that use \c
|
---|
231 | MyClass::tr():
|
---|
232 | \code
|
---|
233 | /* TRANSLATOR MyNamespace::MyClass */
|
---|
234 | \endcode
|
---|
235 | After the comment, all references to \c MyClass::tr() will be
|
---|
236 | understood as meaning \c MyNamespace::MyClass::tr().
|
---|
237 |
|
---|
238 | \section2 Translating Text that is Outside of a QObject subclass
|
---|
239 |
|
---|
240 | \section3 Using QApplication::translate()
|
---|
241 |
|
---|
242 | If the quoted text is not in a member function of a QObject subclass,
|
---|
243 | use either the tr() function of an appropriate class, or the
|
---|
244 | QApplication::translate() function directly:
|
---|
245 |
|
---|
246 | \code
|
---|
247 | void some_global_function( LoginWidget *logwid )
|
---|
248 | {
|
---|
249 | QLabel *label = new QLabel(
|
---|
250 | LoginWidget::tr("Password:"), logwid );
|
---|
251 | }
|
---|
252 |
|
---|
253 | void same_global_function( LoginWidget *logwid )
|
---|
254 | {
|
---|
255 | QLabel *label = new QLabel(
|
---|
256 | qApp->translate("LoginWidget", "Password:"),
|
---|
257 | logwid );
|
---|
258 | }
|
---|
259 | \endcode
|
---|
260 |
|
---|
261 | \section3 Using QT_TR_NOOP() and QT_TRANSLATE_NOOP()
|
---|
262 |
|
---|
263 | If you need to have translatable text completely outside a function,
|
---|
264 | there are two macros to help: QT_TR_NOOP() and QT_TRANSLATE_NOOP().
|
---|
265 | These macros merely mark the text for extraction by \l{lupdate}.
|
---|
266 | The macros expand to just the text (without the context).
|
---|
267 |
|
---|
268 | Example of QT_TR_NOOP():
|
---|
269 | \code
|
---|
270 | QString FriendlyConversation::greeting( int greet_type )
|
---|
271 | {
|
---|
272 | static const char* greeting_strings[] = {
|
---|
273 | QT_TR_NOOP( "Hello" ),
|
---|
274 | QT_TR_NOOP( "Goodbye" )
|
---|
275 | };
|
---|
276 | return tr( greeting_strings[greet_type] );
|
---|
277 | }
|
---|
278 | \endcode
|
---|
279 |
|
---|
280 | Example of QT_TRANSLATE_NOOP():
|
---|
281 | \code
|
---|
282 | static const char* greeting_strings[] = {
|
---|
283 | QT_TRANSLATE_NOOP( "FriendlyConversation", "Hello" ),
|
---|
284 | QT_TRANSLATE_NOOP( "FriendlyConversation", "Goodbye" )
|
---|
285 | };
|
---|
286 |
|
---|
287 | QString FriendlyConversation::greeting( int greet_type )
|
---|
288 | {
|
---|
289 | return tr( greeting_strings[greet_type] );
|
---|
290 | }
|
---|
291 |
|
---|
292 | QString global_greeting( int greet_type )
|
---|
293 | {
|
---|
294 | return qApp->translate( "FriendlyConversation",
|
---|
295 | greeting_strings[greet_type] );
|
---|
296 | }
|
---|
297 | \endcode
|
---|
298 |
|
---|
299 | \section1 Tutorials
|
---|
300 |
|
---|
301 | Three tutorials are presented. The first demonstrates the creation of
|
---|
302 | a \l QTranslator object. It also shows the simplest use of the \c
|
---|
303 | tr() function to mark user-visible source text for translation. The
|
---|
304 | second tutorial explains how to make the application load the
|
---|
305 | translation file applicable to the current locale. It also shows the
|
---|
306 | use of the two-argument form of \c tr() which provides additional
|
---|
307 | information to the translator. The third tutorial explains how
|
---|
308 | identical source texts can be distinguished even when they occur in
|
---|
309 | the same context. This tutorial also discusses how the translation
|
---|
310 | tools help minimize the translator's work when an application is
|
---|
311 | upgraded.
|
---|
312 |
|
---|
313 | \section2 Tutorial 1: Loading and Using Translations
|
---|
314 |
|
---|
315 | \img tt1_en.png
|
---|
316 | \caption Tutorial 1 Screenshot, English version
|
---|
317 |
|
---|
318 | \include tt1/tt1.pro
|
---|
319 | \caption \c tt1.pro
|
---|
320 |
|
---|
321 | \include tt1/main.cpp
|
---|
322 | \caption \c main.cpp
|
---|
323 |
|
---|
324 | This example is a reworking of the \link tutorial1-01.html
|
---|
325 | "hello-world" \endlink example from \link tutorial.html Tutorial
|
---|
326 | #1\endlink, with a Latin translation. The \e {Tutorial 1 Screenshot,
|
---|
327 | English version}, above, shows the English version.
|
---|
328 |
|
---|
329 | \quotefile tt1/main.cpp
|
---|
330 |
|
---|
331 | \section3 Line by Line Walk-through
|
---|
332 |
|
---|
333 | \quotefile tt1/main.cpp
|
---|
334 |
|
---|
335 | \skipto qtranslator
|
---|
336 | \printline qtranslator
|
---|
337 |
|
---|
338 | \index QTranslator
|
---|
339 |
|
---|
340 | This line includes the definition of the \l QTranslator class.
|
---|
341 | Objects of this class provide translations for user-visible text.
|
---|
342 |
|
---|
343 | \skipto QTranslator
|
---|
344 | \printuntil tor
|
---|
345 |
|
---|
346 | Creates a \l QTranslator object without a parent.
|
---|
347 |
|
---|
348 | \printline load
|
---|
349 |
|
---|
350 | \index tt1_la.qm
|
---|
351 |
|
---|
352 | Tries to load a file called \c tt1_la.qm (the \c .qm file extension is
|
---|
353 | implicit) that contains Latin translations for the source texts used in
|
---|
354 | the program. No error will occur if the file is not found.
|
---|
355 |
|
---|
356 | \index QApplication!installTranslator()
|
---|
357 | \index installTranslator()!QApplication
|
---|
358 |
|
---|
359 | \printline installTranslator
|
---|
360 |
|
---|
361 | Adds the translations from \c tt1_la.qm to the pool of translations used
|
---|
362 | by the program.
|
---|
363 |
|
---|
364 | \index Hello World
|
---|
365 |
|
---|
366 | \printline hello
|
---|
367 |
|
---|
368 | Creates a push button that displays "Hello world!". If \c tt1_la.qm
|
---|
369 | was found and contains a translation for "Hello world!", the
|
---|
370 | translation appears; if not, the source text appears.
|
---|
371 |
|
---|
372 | \index tr()
|
---|
373 | \index QObject!tr()
|
---|
374 |
|
---|
375 | All classes that inherit \l QObject have a \c tr() function. Inside
|
---|
376 | a member function of a \l QObject class, we simply write \c tr("Hello
|
---|
377 | world!") instead of \c QPushButton::tr("Hello world!") or \c
|
---|
378 | QObject::tr("Hello world!").
|
---|
379 |
|
---|
380 | \section3 Running the Application in English
|
---|
381 |
|
---|
382 | \index English Language
|
---|
383 |
|
---|
384 | Since we haven't made the translation file \c tt1_la.qm, the source text
|
---|
385 | is shown when we run the application:
|
---|
386 |
|
---|
387 | \img tt1_en.png
|
---|
388 | \caption Tutorial 1 Screenshot, English version
|
---|
389 |
|
---|
390 | \section3 Creating a Latin Message File
|
---|
391 |
|
---|
392 | \index tt1.pro
|
---|
393 | \index Latin
|
---|
394 |
|
---|
395 | The first step is to create a project file, \c tt1.pro, that lists
|
---|
396 | all the source files for the project. The project file can be a qmake
|
---|
397 | project file, or even an ordinary makefile. Any file that contains
|
---|
398 |
|
---|
399 | \index SOURCES!in Project Files
|
---|
400 | \index TRANSLATIONS!in Project Files
|
---|
401 |
|
---|
402 | \quotefile tt1/tt1.pro
|
---|
403 | \skipto SOURCES
|
---|
404 | \printline SOURCES
|
---|
405 | \skipto TRANSLATIONS
|
---|
406 | \printline TRANSLATIONS
|
---|
407 |
|
---|
408 | will work. \e TRANSLATIONS specifies the message files we want to
|
---|
409 | maintain. In this example, we just maintain one set of translations,
|
---|
410 | namely Latin.
|
---|
411 |
|
---|
412 | \index .ts Files
|
---|
413 | \index Translation Source Files
|
---|
414 | \index .qm Files
|
---|
415 | \index Qt Message Files
|
---|
416 |
|
---|
417 | Note that the file extension is \c .ts, not \c .qm. The \c .ts
|
---|
418 | translation source format is designed for use during the
|
---|
419 | application's development. Programmers or release managers run the \l
|
---|
420 | lupdate program to generate and update \c .ts files with the source
|
---|
421 | text that is extracted from the source code. Translators read and
|
---|
422 | update the \c .ts files using \e {Qt Linguist} adding and editing
|
---|
423 | their translations.
|
---|
424 |
|
---|
425 | \index XML
|
---|
426 |
|
---|
427 | The \c .ts format is human-readable XML that can be emailed directly
|
---|
428 | and is easy to put under version control. If you edit this file
|
---|
429 | manually, be aware that the default encoding for XML is UTF-8, not
|
---|
430 | Latin-1 (ISO 8859-1). One way to type in a Latin-1 character such as
|
---|
431 | '\OSLASH' (Norwegian o with slash) is to use an XML entity:
|
---|
432 | "\ø". This will work for any Unicode character.
|
---|
433 |
|
---|
434 | Once the translations are complete the \l lrelease program is used to
|
---|
435 | convert the \c .ts files into the \c .qm Qt message file format. The
|
---|
436 | \c .qm format is a compact binary format designed to deliver very
|
---|
437 | fast lookup performance. Both \l lupdate and \l lrelease read all the
|
---|
438 | project's source and header files (as specified in the HEADERS and
|
---|
439 | SOURCES lines of the project file) and extract the strings that
|
---|
440 | appear in \c tr() function calls.
|
---|
441 |
|
---|
442 | \index lupdate
|
---|
443 |
|
---|
444 | \l lupdate is used to create and update the message files (\c tt1_la.ts
|
---|
445 | in this case) to keep them in sync with the source code. It is safe to
|
---|
446 | run \l lupdate at any time, as \l lupdate does not remove any
|
---|
447 | information. For example, you can put it in the makefile, so the \c .ts
|
---|
448 | files are updated whenever the source changes.
|
---|
449 |
|
---|
450 | \index .ts Files
|
---|
451 | \index Translation Source Files
|
---|
452 | \index XML
|
---|
453 |
|
---|
454 | Try running \l lupdate right now, like this:
|
---|
455 | \code
|
---|
456 | lupdate -verbose tt1.pro
|
---|
457 | \endcode
|
---|
458 | (The \c -verbose option instructs \c lupdate to display messages that
|
---|
459 | explain what it is doing.) You should now have a file \c tt1_la.ts in
|
---|
460 | the current directory, containing this:
|
---|
461 | \code
|
---|
462 | <!DOCTYPE TS><TS>
|
---|
463 | <context>
|
---|
464 | <name>QPushButton</name>
|
---|
465 | <message>
|
---|
466 | <source>Hello world!</source>
|
---|
467 | <translation type="unfinished"></translation>
|
---|
468 | </message>
|
---|
469 | </context>
|
---|
470 | </TS>
|
---|
471 | \endcode
|
---|
472 | You don't need to understand the file format since it is read and
|
---|
473 | updated using tools (\l lupdate, \e {Qt Linguist}, \l lrelease).
|
---|
474 |
|
---|
475 | \section3 Translating to Latin with Qt Linguist
|
---|
476 |
|
---|
477 | \index Qt Linguist
|
---|
478 | \index Linguist
|
---|
479 |
|
---|
480 | We will use \e {Qt Linguist} to provide the translation, although
|
---|
481 | you can use any XML or plain text editor to enter a translation into a
|
---|
482 | \c .ts file.
|
---|
483 |
|
---|
484 | To start \e {Qt Linguist}, type
|
---|
485 | \code
|
---|
486 | linguist tt1_la.ts
|
---|
487 | \endcode
|
---|
488 |
|
---|
489 | You should now see the text "QPushButton" in the top left pane.
|
---|
490 | Double-click it, then click on "Hello world!" and enter "Orbis, te
|
---|
491 | saluto!" in the \e Translation pane (the middle right of the
|
---|
492 | window). Don't forget the exclamation mark!
|
---|
493 |
|
---|
494 | Click the \e Done checkbox and choose \e File|Save from the
|
---|
495 | menu bar. The \c .ts file will no longer contain
|
---|
496 | \code
|
---|
497 | <translation type='unfinished'></translation>
|
---|
498 | \endcode
|
---|
499 | but instead will have
|
---|
500 | \code
|
---|
501 | <translation>Orbis, te saluto!</translation>
|
---|
502 | \endcode
|
---|
503 |
|
---|
504 | \section3 Running the Application in Latin
|
---|
505 |
|
---|
506 | \index Latin
|
---|
507 | \index lrelease
|
---|
508 |
|
---|
509 | To see the application running in Latin, we have to generate a \c .qm
|
---|
510 | file from the \c .ts file. Generating a \c .qm file can be achieved
|
---|
511 | either from within \e {Qt Linguist} (for a single \c .ts file), or
|
---|
512 | by using the command line program \l lrelease which will produce one \c
|
---|
513 | .qm file for each of the \c .ts files listed in the project file.
|
---|
514 | Generate \c tt1_la.qm from \c tt1_la.ts by choosing
|
---|
515 | \e File|Release from \e {Qt Linguist}'s menu bar and pressing
|
---|
516 | \e Save in the file save dialog that pops up. Now run the \e tt1 example
|
---|
517 | program again. This time the button will be labelled "Orbis, te
|
---|
518 | saluto!".
|
---|
519 |
|
---|
520 | \img tt1_la.png
|
---|
521 | \caption Tutorial 1 Screenshot, Latin version
|
---|
522 |
|
---|
523 | \section2 Tutorial 2: Using Two or More Languages
|
---|
524 |
|
---|
525 | \img tt2_en.png
|
---|
526 | \caption Tutorial 2 Screenshot, English version
|
---|
527 |
|
---|
528 | \index .pro Files
|
---|
529 | \index Project Files
|
---|
530 | \index qmake!Project Files
|
---|
531 |
|
---|
532 | \include tt2/tt2.pro
|
---|
533 | \caption tt2.pro
|
---|
534 |
|
---|
535 | \index Translation Contexts
|
---|
536 | \index Contexts!for Translation
|
---|
537 |
|
---|
538 | This example is a slightly more involved and introduces a key
|
---|
539 | \e {Qt Linguist} concept: "contexts".
|
---|
540 |
|
---|
541 | \list
|
---|
542 | \i \c arrowpad.h contains the definition of \c ArrowPad, a custom widget;
|
---|
543 | \i \c arrowpad.cpp contains the implementation of \c ArrowPad;
|
---|
544 | \i \c mainwindow.h contains the definition of \c MainWindow, a subclass of
|
---|
545 | \l QMainWindow
|
---|
546 | \i \c mainwindow.cpp contains the implementation of \c MainWindow;
|
---|
547 | \i \c main.cpp contains main().
|
---|
548 | \endlist
|
---|
549 |
|
---|
550 | \index tt2.pro
|
---|
551 | \index French Language
|
---|
552 | \index Dutch Language
|
---|
553 |
|
---|
554 | We will use two translations, French and Dutch, although there is no
|
---|
555 | effective limit on the number of possible translations that can be used
|
---|
556 | with an application. The relevant lines of \c tt2.pro are
|
---|
557 |
|
---|
558 | \quotefile tt2/tt2.pro
|
---|
559 | \skipto HEADERS
|
---|
560 | \printuntil tt2_nl.ts
|
---|
561 |
|
---|
562 | \index lupdate
|
---|
563 | \index tt2_fr.ts
|
---|
564 | \index tt2_nl.ts
|
---|
565 |
|
---|
566 | Run \l lupdate; it should produce two identical message files
|
---|
567 | \c tt2_fr.ts and \c tt2_nl.ts. These files will contain all the source
|
---|
568 | texts marked for translation with \c tr() calls and their contexts.
|
---|
569 |
|
---|
570 | \section3 Line by Line Walk-through
|
---|
571 |
|
---|
572 | \index ArrowPad!in Translation Tutorial
|
---|
573 | \index English Language
|
---|
574 |
|
---|
575 | In \c arrowpad.h we define the \c ArrowPad subclass which is a
|
---|
576 | subclass of \l QWidget. In the \e {Tutorial 2 Screenshot, English
|
---|
577 | version}, above, the central widget with the four buttons is an
|
---|
578 | \c ArrowPad.
|
---|
579 |
|
---|
580 | \quotefile tt2/arrowpad.h
|
---|
581 | \skipto class ArrowPad
|
---|
582 | \printline class ArrowPad
|
---|
583 |
|
---|
584 | \index Q_OBJECT
|
---|
585 | \index tr()
|
---|
586 | \index QObject!tr()
|
---|
587 | \index Translation Contexts
|
---|
588 | \index Contexts!for Translation
|
---|
589 |
|
---|
590 | When \l lupdate is run it not only extracts the source texts but it
|
---|
591 | also groups them into contexts. A context is the name of the class in
|
---|
592 | which the source text appears. Thus, in this example, "ArrowPad" is a
|
---|
593 | context: it is the context of the texts in the \c ArrowPad class.
|
---|
594 | The \c Q_OBJECT macro defines \c tr(x) in \c ArrowPad like this
|
---|
595 |
|
---|
596 | \index QApplication!translate()
|
---|
597 | \index translate()!QApplication
|
---|
598 |
|
---|
599 | \code
|
---|
600 | qApp->translate( "ArrowPad", x )
|
---|
601 | \endcode
|
---|
602 |
|
---|
603 | Knowing which class each source text appears in enables \e {Qt
|
---|
604 | Linguist} to group texts that are logically related together, e.g.
|
---|
605 | all the text in a dialog will have the context of the dialog's class
|
---|
606 | name and will be shown together. This provides useful information for
|
---|
607 | the translator since the context in which text appears may influence how
|
---|
608 | it should be translated. For some translations keyboard
|
---|
609 | accelerators may need to be changed and having all the source texts in a
|
---|
610 | particular context (class) grouped together makes it easier for the
|
---|
611 | translator to perform any accelerator changes without introducing
|
---|
612 | conflicts.
|
---|
613 |
|
---|
614 | In \c arrowpad.cpp we implement the \c ArrowPad class.
|
---|
615 |
|
---|
616 | \quotefile tt2/arrowpad.cpp
|
---|
617 | \skipto QPushButton
|
---|
618 | \printline QPushButton
|
---|
619 |
|
---|
620 | We call \c ArrowPad::tr() for each button's label since the labels are
|
---|
621 | user-visible text.
|
---|
622 |
|
---|
623 | \img tt2_en.png
|
---|
624 | \caption Tutorial 2 Screenshot, English version
|
---|
625 |
|
---|
626 | \index Q_OBJECT
|
---|
627 | \index MainWindow!in Translation Tutorial
|
---|
628 |
|
---|
629 | \quotefile tt2/mainwindow.h
|
---|
630 | \skipto QMainWindow
|
---|
631 | \printline QMainWindow
|
---|
632 | \printuntil Q_OBJECT
|
---|
633 |
|
---|
634 | In the \e {Tutorial 2 Screenshot, English version}, above, the whole
|
---|
635 | window is a \c MainWindow. This is defined in the \c mainwindow.h
|
---|
636 | header file. Here too, we use \c Q_OBJECT, so that \c MainWindow will
|
---|
637 | become a context in \e {Qt Linguist}.
|
---|
638 |
|
---|
639 | In the implementation of \c MainWindow, \c mainwindow.cpp, we create
|
---|
640 | an instance of our \c ArrowPad class
|
---|
641 |
|
---|
642 | \quotefile tt2/mainwindow.cpp
|
---|
643 | \skipto arrow pad
|
---|
644 | \printline arrow pad
|
---|
645 |
|
---|
646 | We also call \c MainWindow::tr() twice, once for the menu item and
|
---|
647 | once for the accelerator.
|
---|
648 |
|
---|
649 | \index Ctrl Key
|
---|
650 | \index Alt Key
|
---|
651 |
|
---|
652 | \skipto quit()
|
---|
653 | \printline quit()
|
---|
654 | \printuntil Ctrl+Q
|
---|
655 |
|
---|
656 | Note the use of \c tr() to support different keys in other languages.
|
---|
657 | "Ctrl+Q" is a good choice for Quit in English, but a Dutch translator
|
---|
658 | might want to use "Ctrl+A" (for Afsluiten) and a German translator
|
---|
659 | "Strg+E" (for Beenden). When using \c tr() for Ctrl key accelerators,
|
---|
660 | the two argument form should be used with the second argument
|
---|
661 | describing the function that the accelerator performs.
|
---|
662 |
|
---|
663 | \index main()
|
---|
664 |
|
---|
665 | Our \c main() function is defined in \c main.cpp as usual.
|
---|
666 |
|
---|
667 | \quotefile tt2/main.cpp
|
---|
668 | \skipto QTranslator
|
---|
669 | \printline QTranslator
|
---|
670 | \printuntil install
|
---|
671 |
|
---|
672 | \index QTextCodec!locale()
|
---|
673 | \index locale()!QTextCodec
|
---|
674 | \index LANG!Environment Variable
|
---|
675 | \index Environment Variables!LANG
|
---|
676 |
|
---|
677 | We choose which translation to use according to the current locale.
|
---|
678 | \l QTextCodec::locale() can be influenced by setting the \c LANG
|
---|
679 | environment variable, for example. Notice that the use of a naming
|
---|
680 | convention that incorporates the locale for \c .qm message files,
|
---|
681 | (and \c .ts files), makes it easy to implement choosing the
|
---|
682 | translation file according to locale.
|
---|
683 |
|
---|
684 | If there is no \c .qm message file for the locale chosen the original
|
---|
685 | source text will be used and no error raised.
|
---|
686 |
|
---|
687 | \section3 Translating to French and Dutch
|
---|
688 |
|
---|
689 | We'll begin by translating the example application into French. Start
|
---|
690 | \e {Qt Linguist} with \c tt2_fr.ts. You should get the seven source
|
---|
691 | texts ("\&Up", "\&Left", etc.) grouped in two contexts ("ArrowPad"
|
---|
692 | and "MainWindow").
|
---|
693 |
|
---|
694 | Now, enter the following translations:
|
---|
695 |
|
---|
696 | \list
|
---|
697 | \i \c ArrowPad
|
---|
698 | \list
|
---|
699 | \i \&Up - \&Haut
|
---|
700 | \i \&Left - \&Gauche
|
---|
701 | \i \&Right - \&Droite
|
---|
702 | \i \&Down - \&Bas
|
---|
703 | \endlist
|
---|
704 | \i \c MainWindow
|
---|
705 | \list
|
---|
706 | \i E\&xit - \&Quitter
|
---|
707 | \i Ctrl+Q - Ctrl+Q
|
---|
708 | \i \&File - \&Fichier
|
---|
709 | \endlist
|
---|
710 | \endlist
|
---|
711 |
|
---|
712 | It's quickest to press \Key Alt+D (which clicks the \e {Done \& Next}
|
---|
713 | button) after typing each translation, since this marks the
|
---|
714 | translation as done and moves on to the next source text.
|
---|
715 |
|
---|
716 | Save the file and do the same for Dutch working with \c tt2_nl.ts:
|
---|
717 |
|
---|
718 | \list
|
---|
719 | \i \c ArrowPad
|
---|
720 | \list
|
---|
721 | \i \&Up - \&Boven
|
---|
722 | \i \&Left - \&Links
|
---|
723 | \i \&Right - \&Rechts
|
---|
724 | \i \&Down - \&Onder
|
---|
725 | \endlist
|
---|
726 | \i \c MainWindow
|
---|
727 | \list
|
---|
728 | \i E\&xit - \&Afsluiten
|
---|
729 | \i Ctrl+Q - Ctrl+A
|
---|
730 | \i File - \&Bestand
|
---|
731 | \endlist
|
---|
732 | \endlist
|
---|
733 |
|
---|
734 | We have to convert the \c tt1_fr.ts and \c tt1_nl.ts translation source
|
---|
735 | files into \c .qm files. We could use \e {Qt Linguist} as we've done
|
---|
736 | before; however using the command line tool \l lrelease ensures that
|
---|
737 | \e all the \c .qm files for the application are created without us
|
---|
738 | having to remember to load and \e File|Release each one
|
---|
739 | individually from \e {Qt Linguist}.
|
---|
740 |
|
---|
741 | In practice we would include calls to \l lupdate and \l lrelease in the
|
---|
742 | application's makefile to ensure that the latest translations are
|
---|
743 | used.
|
---|
744 |
|
---|
745 | \omit
|
---|
746 | an example of a makefile or .pro file that did this would be nice
|
---|
747 | \endomit
|
---|
748 |
|
---|
749 | Type
|
---|
750 |
|
---|
751 | \code
|
---|
752 | lrelease tt2.pro
|
---|
753 | \endcode
|
---|
754 |
|
---|
755 | \index LANG!Environment Variable
|
---|
756 | \index export!Unix Command
|
---|
757 | \index setenv!Unix Command
|
---|
758 |
|
---|
759 | This should create both \c tt2_fr.qm and \c tt2_nl.qm. Set the \c
|
---|
760 | LANG environment variable to \c fr. In Unix, one of the two following
|
---|
761 | commands should work
|
---|
762 |
|
---|
763 | \code
|
---|
764 | export LANG=fr
|
---|
765 | setenv LANG fr
|
---|
766 | \endcode
|
---|
767 |
|
---|
768 | \index
|
---|
769 |
|
---|
770 | \index autoexec.bat
|
---|
771 | \index set!Windows Command
|
---|
772 |
|
---|
773 | In Windows, either modify \c autoexec.bat or run
|
---|
774 |
|
---|
775 | \code
|
---|
776 | set LANG=fr
|
---|
777 | \endcode
|
---|
778 |
|
---|
779 | When you run the program, you should now see the French version:
|
---|
780 |
|
---|
781 | \img tt2_fr.png
|
---|
782 | \caption Tutorial 2 Screenshot, French version
|
---|
783 |
|
---|
784 | Try the same with Dutch, by setting \c LANG=nl. Now the Dutch
|
---|
785 | version should appear:
|
---|
786 |
|
---|
787 | \img tt2_nl.png
|
---|
788 | \caption Tutorial 2 Screenshot, Dutch version
|
---|
789 |
|
---|
790 | \section3 Exercises
|
---|
791 |
|
---|
792 | Mark one of the translations in \e {Qt Linguist} as not done, i.e.
|
---|
793 | by unchecking the "done" checkbox; run \l lupdate, then \l lrelease,
|
---|
794 | then the example. What effect did this change have?
|
---|
795 |
|
---|
796 | \index Canada
|
---|
797 | \index French Canada
|
---|
798 |
|
---|
799 | Set \c LANG=fr_CA (French Canada) and run the example program again.
|
---|
800 | Explain why the result is the same as with \c LANG=fr.
|
---|
801 |
|
---|
802 | Change one of the accelerators in the Dutch translation to eliminate the
|
---|
803 | conflict between \e \&Bestand and \e \&Boven.
|
---|
804 |
|
---|
805 |
|
---|
806 | \section2 Tutorial 3: Disambiguating Identical Strings
|
---|
807 |
|
---|
808 | \img tt3_10_en.png
|
---|
809 | \caption Tutorial 3 Screenshot, "Troll Print 1.0", English version
|
---|
810 |
|
---|
811 | \include tt3/tt3.pro
|
---|
812 | \caption \c tt3.pro
|
---|
813 |
|
---|
814 | \index Portuguese Language
|
---|
815 | \index Brazilian Language
|
---|
816 |
|
---|
817 | We've included a translation file, \c tt3_pt.ts, which contains some
|
---|
818 | Portuguese translations for this example.
|
---|
819 |
|
---|
820 | \index Troll Print
|
---|
821 |
|
---|
822 | We will consider two releases of the same application: Troll Print
|
---|
823 | 1.0 and 1.1. We will learn to reuse the translations created for one
|
---|
824 | release in a subsequent release. (In this tutorial, you need to edit
|
---|
825 | some source files. It's probably best to copy all the files to a new
|
---|
826 | temporary directory and work from there.)
|
---|
827 |
|
---|
828 | Troll Print is a toy example application that lets the user choose
|
---|
829 | printer settings. It comes in two versions: English and Portuguese.
|
---|
830 |
|
---|
831 | Version 1.0 consists of these files:
|
---|
832 |
|
---|
833 | \index tt3.pro
|
---|
834 | \index tt3_pt.ts
|
---|
835 |
|
---|
836 | \list
|
---|
837 | \i \c printpanel.h contains the definition of PrintPanel;
|
---|
838 | \i \c printpanel.cpp contains the implementation of PrintPanel;
|
---|
839 | \i \c mainwindow.h contains the definition of \c MainWindow;
|
---|
840 | \i \c mainwindow.cpp contains the implementation of \c MainWindow;
|
---|
841 | \i \c main.cpp contains main();
|
---|
842 | \i \c tt3.pro is the \e qmake project file.
|
---|
843 | \i \c tt3_pt.ts is the Portuguese message file.
|
---|
844 | \endlist
|
---|
845 |
|
---|
846 | \section3 Line by Line Walk-through
|
---|
847 |
|
---|
848 | The PrintPanel is defined in \c printpanel.h.
|
---|
849 |
|
---|
850 | \quotefile tt3/printpanel.h
|
---|
851 | \skipto QVBox
|
---|
852 | \printline QVBox
|
---|
853 | \printuntil Q_OBJECT
|
---|
854 |
|
---|
855 | \index Q_OBJECT
|
---|
856 |
|
---|
857 | \index PrintPanel!in Translation Tutorial
|
---|
858 |
|
---|
859 | PrintPanel is a \l QWidget. It needs the \c Q_OBJECT macro for \c
|
---|
860 | tr() to work properly.
|
---|
861 |
|
---|
862 | The implementation file is \c printpanel.cpp.
|
---|
863 |
|
---|
864 | \quotefile tt3/printpanel.cpp
|
---|
865 | \skipto setSpacing
|
---|
866 | \skipto /
|
---|
867 | \printline /
|
---|
868 | \printline
|
---|
869 | \printline
|
---|
870 | \printline
|
---|
871 |
|
---|
872 | \index Troll Print
|
---|
873 |
|
---|
874 | Some of the code is commented out in Troll Print 1.0; you will uncomment
|
---|
875 | it later, for Troll Print 1.1.
|
---|
876 |
|
---|
877 | \quotefile tt3/printpanel.cpp
|
---|
878 | \skipto twoSided
|
---|
879 | \printline twoSided
|
---|
880 | \printuntil toggle
|
---|
881 | \printline
|
---|
882 | \printuntil toggle
|
---|
883 |
|
---|
884 | Notice the two occurrences of \c tr("Enabled") and of \c
|
---|
885 | tr("Disabled") in PrintPanel. Since both "Enabled"s and "Disabled"s
|
---|
886 | appear in the same context \e {Qt Linguist} will only display one
|
---|
887 | occurrence of each and will use the same translations for the
|
---|
888 | duplicates that it doesn't display. Whilst this is a useful
|
---|
889 | timesaver, in some languages, such as Portuguese, the second
|
---|
890 | occurrence requires a separate translation. We will see how \e {Qt
|
---|
891 | Linguist} can be made to display all the occurrences for separate
|
---|
892 | translation shortly.
|
---|
893 |
|
---|
894 | \index MainWindow!in Translation Tutorial
|
---|
895 |
|
---|
896 | The header file for \c MainWindow, \c mainwindow.h, contains no
|
---|
897 | surprises. In the implementation, \c mainwindow.cpp, we have some
|
---|
898 | user-visible source texts that must be marked for translation.
|
---|
899 |
|
---|
900 | \quotefile tt3/mainwindow.cpp
|
---|
901 | \skipto setCaption
|
---|
902 | \printline setCaption
|
---|
903 |
|
---|
904 | We must translate the window's caption.
|
---|
905 |
|
---|
906 | \skipto quit
|
---|
907 | \printline quit
|
---|
908 | \printuntil Help
|
---|
909 |
|
---|
910 | We also need to translate the menu items. Note that the two argument
|
---|
911 | form of \c tr() is used for the keyboard accelerator, "Ctrl+Q", since
|
---|
912 | the second argument is the only clue the translator has to indicate
|
---|
913 | what function that accelerator will perform.
|
---|
914 |
|
---|
915 | \quotefile tt3/main.cpp
|
---|
916 | \skipto QTranslator
|
---|
917 | \printuntil installTranslator
|
---|
918 |
|
---|
919 | \index main()
|
---|
920 |
|
---|
921 | The \c main() function in \c main.cpp is the same as the one in \link
|
---|
922 | {Tutorial 2...} Tutorial 2 \endlink. In particular it chooses a
|
---|
923 | translation file based on the current locale.
|
---|
924 |
|
---|
925 | \section3 Running Troll Print 1.0 in English and in Portuguese
|
---|
926 |
|
---|
927 | We will use the translations in the \c tt3_pt.ts file that is provided.
|
---|
928 |
|
---|
929 | Set the \c LANG environment variable to \c pt, and then run \c tt3.
|
---|
930 | You should still see the English version, as shown in the \e
|
---|
931 | {Tutorial 3 Screenshot, "Troll Print 1.0", English version}, above.
|
---|
932 | Now run \l lrelease, e.g. \c {lrelease tt3.pro}, and then run the
|
---|
933 | example again. Now you should see the Portuguese edition (Troll
|
---|
934 | Imprimir 1.0):
|
---|
935 |
|
---|
936 | \img tt3_10_pt_bad.png
|
---|
937 | \caption Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Bad) Portuguese version
|
---|
938 |
|
---|
939 | Whilst the translation has appeared correctly, it is in fact wrong. In
|
---|
940 | good Portuguese, the second occurrence of "Enabled" should be
|
---|
941 | "Ativadas", not "Ativado" and the ending for the second translation of
|
---|
942 | "Disabled" must change similarly too.
|
---|
943 |
|
---|
944 | If you open \c tt3_pt.ts using \e {Qt Linguist}, you will see that
|
---|
945 | there is just one occurrence of "Enabled" and of "Disabled" in the
|
---|
946 | translation source file, even though there are two of each in the
|
---|
947 | source code. This is because \e {Qt Linguist} tries to minimize the
|
---|
948 | translator's work by using the same translation for duplicate source
|
---|
949 | texts. In cases such as this where an identical translation is wrong,
|
---|
950 | the programmer must disambiguate the duplicate occurrences. This is
|
---|
951 | easily achieved by using the two argument form of \c tr().
|
---|
952 |
|
---|
953 | We can easily determine which file must be changed because the
|
---|
954 | translator's "context" is in fact the class name for the class where
|
---|
955 | the texts that must be changed appears. In this case the file is \c
|
---|
956 | printpanel.cpp, where the there are four lines to change. Add the
|
---|
957 | second argument "two-sided" in the appropriate \c tr() calls to the
|
---|
958 | first pair of radio buttons:
|
---|
959 |
|
---|
960 | \code
|
---|
961 | but = new QRadioButton( tr("Enabled", "two-sided"), twoSided );
|
---|
962 | but = new QRadioButton( tr("Disabled", "two-sided"), twoSided );
|
---|
963 | \endcode
|
---|
964 |
|
---|
965 | and add the second argument "colors" in the appropriate \c tr() calls
|
---|
966 | for the second pair of radio buttons:
|
---|
967 |
|
---|
968 | \code
|
---|
969 | but = new QRadioButton( tr("Enabled", "colors"), colors );
|
---|
970 | but = new QRadioButton( tr("Disabled", "colors"), colors );
|
---|
971 | \endcode
|
---|
972 |
|
---|
973 | \index lupdate
|
---|
974 | \index tt3_pt.ts
|
---|
975 |
|
---|
976 | Now run \l lupdate and open \c tt3_pt.ts with \e {Qt Linguist}. You
|
---|
977 | should now see two changes.
|
---|
978 |
|
---|
979 | First, the translation source file now contains \e three "Enabled",
|
---|
980 | "Disabled" pairs. The first pair is marked "(obs.)" signifying that they
|
---|
981 | are obsolete. This is because these texts appeared in \c tr() calls that
|
---|
982 | have been replaced by new calls with two arguments. The second pair has
|
---|
983 | "two-sided" as their comment, and the third pair has "colors" as their
|
---|
984 | comment. The comments are shown in the \e {Source text and comments}
|
---|
985 | area in \e {Qt Linguist}.
|
---|
986 |
|
---|
987 | Second, the translation text "Ativado" and "Desativado" have been
|
---|
988 | automatically used as translations for the new "Enabled" and "Disabled"
|
---|
989 | texts, again to minimize the translator's work. Of course in this case
|
---|
990 | these are not correct for the second occurrence of each word, but they
|
---|
991 | provide a good starting point.
|
---|
992 |
|
---|
993 | Change the second "Ativado" into "Ativadas" and the second
|
---|
994 | "Desativado" into "Desativadas", then save and quit. Run \l lrelease
|
---|
995 | to obtain an up-to-date binary \c tt3_pt.qm file, and run Troll Print
|
---|
996 | (or rather Troll Imprimir).
|
---|
997 |
|
---|
998 | \img tt3_10_pt_good.png
|
---|
999 | \caption Tutorial 3 Screenshot, "Troll Imprimir 1.0", (Good) Portuguese version
|
---|
1000 |
|
---|
1001 | \index Translator Comments
|
---|
1002 | \index Comments!for Translators
|
---|
1003 |
|
---|
1004 | The second argument to \c tr() calls, called "comments" in \e {Qt
|
---|
1005 | Linguist}, distinguish between identical source texts that occur in
|
---|
1006 | the same context (class). They are also useful in other cases to give
|
---|
1007 | clues to the translator, and in the case of Ctrl key accelerators are
|
---|
1008 | the only means of conveying the function performed by the accelerator to
|
---|
1009 | the translator.
|
---|
1010 |
|
---|
1011 | \index TRANSLATOR!in Comments
|
---|
1012 | \index Translator Comments
|
---|
1013 | \index Comments!for Translators
|
---|
1014 |
|
---|
1015 | An additional way of helping the translator is to provide information on
|
---|
1016 | how to navigate to the particular part of the application that contains
|
---|
1017 | the source texts they must translate. This helps them see the context
|
---|
1018 | in which the translation appears and also helps them to find and test
|
---|
1019 | the translations. This can be achieved by using a \e TRANSLATOR comment
|
---|
1020 | in the source code:
|
---|
1021 | \code
|
---|
1022 | /* TRANSLATOR MainWindow
|
---|
1023 |
|
---|
1024 | In this application the whole application is a MainWindow.
|
---|
1025 | Choose Help|About from the menu bar to see some text
|
---|
1026 | belonging to MainWindow.
|
---|
1027 | */
|
---|
1028 | \endcode
|
---|
1029 |
|
---|
1030 | Try adding these comments to some source files, particularly to
|
---|
1031 | dialog classes, describing the navigation necessary to reach the
|
---|
1032 | dialogs. You could also add them to the example files, e.g. \c
|
---|
1033 | mainwindow.cpp and \c printpanel.cpp are appropriate files. Run \l
|
---|
1034 | lupdate and then start \e {Qt Linguist} and load in \c tt3_pt.ts.
|
---|
1035 | You should see the comments in the \e {Source text and comments} area
|
---|
1036 | as you browse through the list of source texts.
|
---|
1037 |
|
---|
1038 | Sometimes, particularly with large programs, it can be difficult for
|
---|
1039 | the translator to find their translations and check that they're
|
---|
1040 | correct. Comments that provide good navigation information can save
|
---|
1041 | them time:
|
---|
1042 |
|
---|
1043 | \code
|
---|
1044 | /* TRANSLATOR ZClientErrorDialog
|
---|
1045 |
|
---|
1046 | Choose Client|Edit to reach the Client Edit dialog, then choose
|
---|
1047 | Client Specification from the drop down list at the top and pick
|
---|
1048 | client Bartel Leendert van der Waerden. Now check the Profile
|
---|
1049 | checkbox and then click the Start Processing button. You should
|
---|
1050 | now see a pop up window with the text "Error: Name too long!".
|
---|
1051 | This window is a ZClientErrorDialog.
|
---|
1052 | */
|
---|
1053 | \endcode
|
---|
1054 |
|
---|
1055 |
|
---|
1056 | \section3 Troll Print 1.1
|
---|
1057 |
|
---|
1058 | We'll now prepare release 1.1 of Troll Print. Start your favorite text
|
---|
1059 | editor and follow these steps:
|
---|
1060 |
|
---|
1061 | \list
|
---|
1062 | \i Uncomment the two lines that create a \l QLabel with the text
|
---|
1063 | "\<b\>TROLL PRINT\</b\>" in \c printpanel.cpp.
|
---|
1064 | \i Word-tidying: Replace "2-sided" by "Two-sided" in \c printpanel.cpp.
|
---|
1065 | \i Replace "1.0" with "1.1" everywhere it occurs in \c mainwindow.cpp.
|
---|
1066 | \i Update the copyright year to 1999-2000 in \c mainwindow.cpp.
|
---|
1067 | \endlist
|
---|
1068 |
|
---|
1069 | (Of course the version number and copyright year would be consts or
|
---|
1070 | #defines in a real application.)
|
---|
1071 |
|
---|
1072 | Once finished, run \l lupdate, then open \c tt3_pt.ts in \e {Qt
|
---|
1073 | Linguist}. The following items are of special interest:
|
---|
1074 |
|
---|
1075 | \list
|
---|
1076 | \i \c MainWindow
|
---|
1077 | \list
|
---|
1078 | \i Troll Print 1.0 - marked "(obs.)", obsolete
|
---|
1079 | \i About Troll Print 1.0 - marked "(obs.)", obsolete
|
---|
1080 | \i Troll Print 1.0. Copyright 1999 Macroshaft, Inc. -
|
---|
1081 | marked "(obs.)", obsolete
|
---|
1082 | \i Troll Print 1.1 - automatically translated as
|
---|
1083 | "Troll Imprimir 1.1"
|
---|
1084 | \i About Troll Print 1.1 - automatically translated as
|
---|
1085 | "Troll Imprimir 1.1"
|
---|
1086 | \i Troll Print 1.1. Copyright 1999-2000 Macroshaft,
|
---|
1087 | Inc. - automatically translated as "Troll Imprimir 1.1.
|
---|
1088 | Copyright 1999-2000 Macroshaft, Inc."
|
---|
1089 | \endlist
|
---|
1090 | \i \c PrintPanel
|
---|
1091 | \list
|
---|
1092 | \i 2-sided - marked "(obs.)", obsolete
|
---|
1093 | \i \<b\>TROLL PRINT\</b\> - unmarked, i.e. untranslated
|
---|
1094 | \i Two-sided - unmarked, i.e. untranslated.
|
---|
1095 | \endlist
|
---|
1096 | \endlist
|
---|
1097 |
|
---|
1098 | Notice that \l lupdate works hard behind the scenes to make revisions
|
---|
1099 | easier, and it's pretty smart with numbers.
|
---|
1100 |
|
---|
1101 | Go over the translations in \c MainWindow and mark these as "done".
|
---|
1102 | Translate "\<b\>TROLL PRINT\</b\>" as "\<b\>TROLL IMPRIMIR\</b\>".
|
---|
1103 | When you're translating "Two-sided", press the \e {Guess Again}
|
---|
1104 | button to translate "Two-sided", but change the "2" into "Dois".
|
---|
1105 |
|
---|
1106 | Save and quit, then run \l lrelease. The Portuguese version
|
---|
1107 | should look like this:
|
---|
1108 |
|
---|
1109 | \img tt3_11_pt.png
|
---|
1110 | \caption Tutorial 3 Screenshot, "Troll Imprimir 1.1", Portuguese version
|
---|
1111 |
|
---|
1112 | Choose \e{Ajuda|Sobre}, (\e{Help|About}), to see the about box
|
---|
1113 |
|
---|
1114 | \img tt3_11_about_pt.png
|
---|
1115 | \caption Tutorial 3 Screenshot, About box, Portuguese version
|
---|
1116 |
|
---|
1117 | \index English Language
|
---|
1118 | \index Translating Qt
|
---|
1119 | \index Qt!Translating Qt
|
---|
1120 |
|
---|
1121 | If you choose \e {Ajuda|Sobre Qt}, (\e {Help|About Qt}), you'll get
|
---|
1122 | an English dialog. Oops! Qt itself needs to be translated. See the
|
---|
1123 | document \link i18n.html#qt-itself Internationalization with Qt
|
---|
1124 | \endlink for details.
|
---|
1125 |
|
---|
1126 | Now set \c LANG=en to get the original English version:
|
---|
1127 |
|
---|
1128 | \img tt3_11_en.png
|
---|
1129 | \caption Tutorial 3 Screenshot, "Troll Print 1.1", English version
|
---|
1130 |
|
---|
1131 | \section2 Summary
|
---|
1132 |
|
---|
1133 | These tutorials cover all that you need to know to prepare your Qt
|
---|
1134 | applications for translation.
|
---|
1135 |
|
---|
1136 | At the beginning of a project add the translation source files to be
|
---|
1137 | used to the project file and add calls to \l lupdate and \l lrelease to
|
---|
1138 | the make file.
|
---|
1139 |
|
---|
1140 | During the project all the programmer must do is wrap any user-visible
|
---|
1141 | text in \c tr() calls. They should also use the two argument form for
|
---|
1142 | Ctrl key accelerators, or when asked by the translator for the cases
|
---|
1143 | where the same text translates into two different forms in the same
|
---|
1144 | context. The programmer should also include \e TRANSLATION comments to
|
---|
1145 | help the translator navigate the application.
|
---|