1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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:LGPL$
|
---|
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
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \group i18n
|
---|
44 | \title Qt Classes for Internationalization
|
---|
45 |
|
---|
46 | See \l{Internationalization with Qt} for information on how to use these classes
|
---|
47 | in your applications.
|
---|
48 | */
|
---|
49 |
|
---|
50 | /*!
|
---|
51 | \page internationalization.html
|
---|
52 | \title Internationalization with Qt
|
---|
53 | \brief Information about Qt's support for internationalization and multiple languages.
|
---|
54 | \nextpage Writing Source Code for Translation
|
---|
55 |
|
---|
56 | \keyword internationalization
|
---|
57 | \keyword i18n
|
---|
58 |
|
---|
59 | The internationalization of an application is the process of making
|
---|
60 | the application usable by people in countries other than one's own.
|
---|
61 |
|
---|
62 | \tableofcontents
|
---|
63 |
|
---|
64 | \section1 Relevant Qt Classes and APIs
|
---|
65 |
|
---|
66 | These classes support internationalizing of Qt applications.
|
---|
67 |
|
---|
68 | \annotatedlist i18n
|
---|
69 |
|
---|
70 | \section1 Languages and Writing Systems
|
---|
71 |
|
---|
72 | In some cases internationalization is simple, for example, making a US
|
---|
73 | application accessible to Australian or British users may require
|
---|
74 | little more than a few spelling corrections. But to make a US
|
---|
75 | application usable by Japanese users, or a Korean application usable
|
---|
76 | by German users, will require that the software operate not only in
|
---|
77 | different languages, but use different input techniques, character
|
---|
78 | encodings and presentation conventions.
|
---|
79 |
|
---|
80 | Qt tries to make internationalization as painless as possible for
|
---|
81 | developers. All input widgets and text drawing methods in Qt offer
|
---|
82 | built-in support for all supported languages. The built-in font engine
|
---|
83 | is capable of correctly and attractively rendering text that contains
|
---|
84 | characters from a variety of different writing systems at the same
|
---|
85 | time.
|
---|
86 |
|
---|
87 | Qt supports most languages in use today, in particular:
|
---|
88 | \list
|
---|
89 | \o All East Asian languages (Chinese, Japanese and Korean)
|
---|
90 | \o All Western languages (using Latin script)
|
---|
91 | \o Arabic
|
---|
92 | \o Cyrillic languages (Russian, Ukrainian, etc.)
|
---|
93 | \o Greek
|
---|
94 | \o Hebrew
|
---|
95 | \o Thai and Lao
|
---|
96 | \o All scripts in Unicode 5.1 that do not require special processing
|
---|
97 | \endlist
|
---|
98 |
|
---|
99 | On Windows, Unix/X11 with FontConfig (client side font support)
|
---|
100 | and Qt for Embedded Linux the following languages are also supported:
|
---|
101 | \list
|
---|
102 | \o Bengali
|
---|
103 | \o Devanagari
|
---|
104 | \o Dhivehi (Thaana)
|
---|
105 | \o Gujarati
|
---|
106 | \o Gurmukhi
|
---|
107 | \o Kannada
|
---|
108 | \o Khmer
|
---|
109 | \o Malayalam
|
---|
110 | \o Myanmar
|
---|
111 | \o Syriac
|
---|
112 | \o Tamil
|
---|
113 | \o Telugu
|
---|
114 | \o Tibetan
|
---|
115 | \o N'Ko
|
---|
116 | \endlist
|
---|
117 |
|
---|
118 | Many of these writing systems exhibit special features:
|
---|
119 |
|
---|
120 | \list
|
---|
121 |
|
---|
122 | \o \bold{Special line breaking behavior.} Some of the Asian languages are
|
---|
123 | written without spaces between words. Line breaking can occur either
|
---|
124 | after every character (with exceptions) as in Chinese, Japanese and
|
---|
125 | Korean, or after logical word boundaries as in Thai.
|
---|
126 |
|
---|
127 | \o \bold{Bidirectional writing.} Arabic and Hebrew are written from right to
|
---|
128 | left, except for numbers and embedded English text which is written
|
---|
129 | left to right. The exact behavior is defined in the
|
---|
130 | \l{http://www.unicode.org/unicode/reports/tr9/}{Unicode Technical Annex #9}.
|
---|
131 |
|
---|
132 | \o \bold{Non-spacing or diacritical marks (accents or umlauts in European
|
---|
133 | languages).} Some languages such as Vietnamese make extensive use of
|
---|
134 | these marks and some characters can have more than one mark at the
|
---|
135 | same time to clarify pronunciation.
|
---|
136 |
|
---|
137 | \o \bold{Ligatures.} In special contexts, some pairs of characters get
|
---|
138 | replaced by a combined glyph forming a ligature. Common examples are
|
---|
139 | the fl and fi ligatures used in typesetting US and European books.
|
---|
140 |
|
---|
141 | \endlist
|
---|
142 |
|
---|
143 | Qt tries to take care of all the special features listed above. You
|
---|
144 | usually don't have to worry about these features so long as you use
|
---|
145 | Qt's input widgets (e.g. QLineEdit, QTextEdit, and derived classes)
|
---|
146 | and Qt's display widgets (e.g. QLabel).
|
---|
147 |
|
---|
148 | Support for these writing systems is transparent to the
|
---|
149 | programmer and completely encapsulated in \l{rich text
|
---|
150 | processing}{Qt's text engine}. This means that you don't need to
|
---|
151 | have any knowledge about the writing system used in a particular
|
---|
152 | language, except for the following small points:
|
---|
153 |
|
---|
154 | \list
|
---|
155 |
|
---|
156 | \o QPainter::drawText(int x, int y, const QString &str) will always
|
---|
157 | draw the string with its left edge at the position specified with
|
---|
158 | the x, y parameters. This will usually give you left aligned strings.
|
---|
159 | Arabic and Hebrew application strings are usually right
|
---|
160 | aligned, so for these languages use the version of drawText() that
|
---|
161 | takes a QRect since this will align in accordance with the language.
|
---|
162 |
|
---|
163 | \o When you write your own text input controls, use QTextLayout.
|
---|
164 | In some languages (e.g. Arabic or languages from the Indian
|
---|
165 | subcontinent), the width and shape of a glyph changes depending on the
|
---|
166 | surrounding characters, which QTextLayout takes into account.
|
---|
167 | Writing input controls usually requires a certain knowledge of the
|
---|
168 | scripts it is going to be used in. Usually the easiest way is to
|
---|
169 | subclass QLineEdit or QTextEdit.
|
---|
170 |
|
---|
171 | \endlist
|
---|
172 |
|
---|
173 | The following sections give some information on the status of the
|
---|
174 | internationalization (i18n) support in Qt. See also the \l{Qt
|
---|
175 | Linguist manual}.
|
---|
176 |
|
---|
177 | \section1 Step by Step
|
---|
178 |
|
---|
179 | Writing cross-platform international software with Qt is a gentle,
|
---|
180 | incremental process. Your software can become internationalized in
|
---|
181 | the following stages:
|
---|
182 |
|
---|
183 | \section2 Use QString for All User-Visible Text
|
---|
184 |
|
---|
185 | Since QString uses the Unicode 5.1 encoding internally, every
|
---|
186 | language in the world can be processed transparently using
|
---|
187 | familiar text processing operations. Also, since all Qt functions
|
---|
188 | that present text to the user take a QString as a parameter,
|
---|
189 | there is no \c{char *} to QString conversion overhead.
|
---|
190 |
|
---|
191 | Strings that are in "programmer space" (such as QObject names
|
---|
192 | and file format texts) need not use QString; the traditional
|
---|
193 | \c{char *} or the QByteArray class will suffice.
|
---|
194 |
|
---|
195 | You're unlikely to notice that you are using Unicode;
|
---|
196 | QString, and QChar are just like easier versions of the crude
|
---|
197 | \c{const char *} and char from traditional C.
|
---|
198 |
|
---|
199 | \section2 Use tr() for All Literal Text
|
---|
200 |
|
---|
201 | Wherever your program uses "quoted text" for text that will
|
---|
202 | be presented to the user, ensure that it is processed by the \l
|
---|
203 | QCoreApplication::translate() function. Essentially all that is necessary
|
---|
204 | to achieve this is to use QObject::tr(). For example, assuming the
|
---|
205 | \c LoginWidget is a subclass of QWidget:
|
---|
206 |
|
---|
207 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 0
|
---|
208 |
|
---|
209 | This accounts for 99% of the user-visible strings you're likely to
|
---|
210 | write.
|
---|
211 |
|
---|
212 | If the quoted text is not in a member function of a
|
---|
213 | QObject subclass, use either the tr() function of an
|
---|
214 | appropriate class, or the QCoreApplication::translate() function
|
---|
215 | directly:
|
---|
216 |
|
---|
217 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 1
|
---|
218 |
|
---|
219 | If you need to have translatable text completely
|
---|
220 | outside a function, there are two macros to help: QT_TR_NOOP()
|
---|
221 | and QT_TRANSLATE_NOOP(). They merely mark the text for
|
---|
222 | extraction by the \c lupdate utility described below.
|
---|
223 | The macros expand to just the text (without the context).
|
---|
224 |
|
---|
225 | Example of QT_TR_NOOP():
|
---|
226 |
|
---|
227 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 2
|
---|
228 |
|
---|
229 | Example of QT_TRANSLATE_NOOP():
|
---|
230 |
|
---|
231 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 3
|
---|
232 |
|
---|
233 | If you disable the \c{const char *} to QString automatic
|
---|
234 | conversion by compiling your software with the macro \c
|
---|
235 | QT_NO_CAST_FROM_ASCII defined, you'll be very likely to catch any
|
---|
236 | strings you are missing. See QString::fromLatin1() for more
|
---|
237 | information. Disabling the conversion can make programming a bit
|
---|
238 | cumbersome.
|
---|
239 |
|
---|
240 | If your source language uses characters outside Latin1, you
|
---|
241 | might find QObject::trUtf8() more convenient than
|
---|
242 | QObject::tr(), as tr() depends on the
|
---|
243 | QTextCodec::codecForTr(), which makes it more fragile than
|
---|
244 | QObject::trUtf8().
|
---|
245 |
|
---|
246 | \section2 Use QKeySequence() for Accelerator Values
|
---|
247 |
|
---|
248 | Accelerator values such as Ctrl+Q or Alt+F need to be translated
|
---|
249 | too. If you hardcode Qt::CTRL + Qt::Key_Q for "quit" in your
|
---|
250 | application, translators won't be able to override it. The
|
---|
251 | correct idiom is
|
---|
252 |
|
---|
253 | \snippet examples/mainwindows/application/mainwindow.cpp 20
|
---|
254 |
|
---|
255 | \section2 Use QString::arg() for Dynamic Text
|
---|
256 |
|
---|
257 | The QString::arg() functions offer a simple means for substituting
|
---|
258 | arguments:
|
---|
259 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 4
|
---|
260 |
|
---|
261 | In some languages the order of arguments may need to change, and this
|
---|
262 | can easily be achieved by changing the order of the % arguments. For
|
---|
263 | example:
|
---|
264 |
|
---|
265 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 5
|
---|
266 |
|
---|
267 | produces the correct output in English and Norwegian:
|
---|
268 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 6
|
---|
269 |
|
---|
270 | \section2 Produce Translations
|
---|
271 |
|
---|
272 | Once you are using tr() throughout an application, you can start
|
---|
273 | producing translations of the user-visible text in your program.
|
---|
274 |
|
---|
275 | The \l{Qt Linguist manual} provides further information about
|
---|
276 | Qt's translation tools, \e{Qt Linguist}, \c lupdate and \c
|
---|
277 | lrelease.
|
---|
278 |
|
---|
279 | Translation of a Qt application is a three-step process:
|
---|
280 |
|
---|
281 | \list 1
|
---|
282 |
|
---|
283 | \o Run \c lupdate to extract translatable text from the C++
|
---|
284 | source code of the Qt application, resulting in a message file
|
---|
285 | for translators (a TS file). The utility recognizes the tr()
|
---|
286 | construct and the \c{QT_TR*_NOOP()} macros described above and
|
---|
287 | produces TS files (usually one per language).
|
---|
288 |
|
---|
289 | \o Provide translations for the source texts in the TS file, using
|
---|
290 | \e{Qt Linguist}. Since TS files are in XML format, you can also
|
---|
291 | edit them by hand.
|
---|
292 |
|
---|
293 | \o Run \c lrelease to obtain a light-weight message file (a QM
|
---|
294 | file) from the TS file, suitable only for end use. Think of the TS
|
---|
295 | files as "source files", and QM files as "object files". The
|
---|
296 | translator edits the TS files, but the users of your application
|
---|
297 | only need the QM files. Both kinds of files are platform and
|
---|
298 | locale independent.
|
---|
299 |
|
---|
300 | \endlist
|
---|
301 |
|
---|
302 | Typically, you will repeat these steps for every release of your
|
---|
303 | application. The \c lupdate utility does its best to reuse the
|
---|
304 | translations from previous releases.
|
---|
305 |
|
---|
306 | Before you run \c lupdate, you should prepare a project file. Here's
|
---|
307 | an example project file (\c .pro file):
|
---|
308 |
|
---|
309 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 7
|
---|
310 |
|
---|
311 | When you run \c lupdate or \c lrelease, you must give the name of the
|
---|
312 | project file as a command-line argument.
|
---|
313 |
|
---|
314 | In this example, four exotic languages are supported: Danish,
|
---|
315 | Finnish, Norwegian and Swedish. If you use \l{qmake}, you usually
|
---|
316 | don't need an extra project file for \c lupdate; your \c qmake
|
---|
317 | project file will work fine once you add the \c TRANSLATIONS
|
---|
318 | entry.
|
---|
319 |
|
---|
320 | In your application, you must \l QTranslator::load() the translation
|
---|
321 | files appropriate for the user's language, and install them using \l
|
---|
322 | QCoreApplication::installTranslator().
|
---|
323 |
|
---|
324 | \c linguist, \c lupdate and \c lrelease are installed in the \c bin
|
---|
325 | subdirectory of the base directory Qt is installed into. Click Help|Manual
|
---|
326 | in \e{Qt Linguist} to access the user's manual; it contains a tutorial
|
---|
327 | to get you started.
|
---|
328 |
|
---|
329 | \target qt-itself
|
---|
330 | Qt itself contains over 400 strings that will also need to be
|
---|
331 | translated into the languages that you are targeting. You will find
|
---|
332 | translation files for French, German and Simplified Chinese in
|
---|
333 | \c{$QTDIR/translations}, as well as a template for translating to
|
---|
334 | other languages. (This directory also contains some additional
|
---|
335 | unsupported translations which may be useful.)
|
---|
336 |
|
---|
337 | Typically, your application's \c main() function will look like
|
---|
338 | this:
|
---|
339 |
|
---|
340 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 8
|
---|
341 |
|
---|
342 | Note the use of QLibraryInfo::location() to locate the Qt translations.
|
---|
343 | Developers should request the path to the translations at run-time by
|
---|
344 | passing QLibraryInfo::TranslationsPath to this function instead of
|
---|
345 | using the \c QTDIR environment variable in their applications.
|
---|
346 |
|
---|
347 | \section2 Support for Encodings
|
---|
348 |
|
---|
349 | The QTextCodec class and the facilities in QTextStream make it easy to
|
---|
350 | support many input and output encodings for your users' data. When an
|
---|
351 | application starts, the locale of the machine will determine the 8-bit
|
---|
352 | encoding used when dealing with 8-bit data: such as for font
|
---|
353 | selection, text display, 8-bit text I/O, and character input.
|
---|
354 |
|
---|
355 | The application may occasionally require encodings other than the
|
---|
356 | default local 8-bit encoding. For example, an application in a
|
---|
357 | Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might
|
---|
358 | need to output Cyrillic in the ISO 8859-5 encoding. Code for this
|
---|
359 | would be:
|
---|
360 |
|
---|
361 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 9
|
---|
362 |
|
---|
363 | For converting Unicode to local 8-bit encodings, a shortcut is
|
---|
364 | available: the QString::toLocal8Bit() function returns such 8-bit
|
---|
365 | data. Another useful shortcut is QString::toUtf8(), which returns
|
---|
366 | text in the 8-bit UTF-8 encoding: this perfectly preserves
|
---|
367 | Unicode information while looking like plain ASCII if the text is
|
---|
368 | wholly ASCII.
|
---|
369 |
|
---|
370 | For converting the other way, there are the QString::fromUtf8() and
|
---|
371 | QString::fromLocal8Bit() convenience functions, or the general code,
|
---|
372 | demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode
|
---|
373 | conversion:
|
---|
374 |
|
---|
375 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 10
|
---|
376 |
|
---|
377 | Ideally Unicode I/O should be used as this maximizes the portability
|
---|
378 | of documents between users around the world, but in reality it is
|
---|
379 | useful to support all the appropriate encodings that your users will
|
---|
380 | need to process existing documents. In general, Unicode (UTF-16 or
|
---|
381 | UTF-8) is best for information transferred between arbitrary people,
|
---|
382 | while within a language or national group, a local standard is often
|
---|
383 | more appropriate. The most important encoding to support is the one
|
---|
384 | returned by QTextCodec::codecForLocale(), as this is the one the user
|
---|
385 | is most likely to need for communicating with other people and
|
---|
386 | applications (this is the codec used by local8Bit()).
|
---|
387 |
|
---|
388 | Qt supports most of the more frequently used encodings natively. For a
|
---|
389 | complete list of supported encodings see the \l QTextCodec
|
---|
390 | documentation.
|
---|
391 |
|
---|
392 | In some cases and for less frequently used encodings it may be
|
---|
393 | necessary to write your own QTextCodec subclass. Depending on the
|
---|
394 | urgency, it may be useful to contact Qt's technical support team or
|
---|
395 | ask on the \c qt-interest mailing list to see if someone else is
|
---|
396 | already working on supporting the encoding.
|
---|
397 |
|
---|
398 | \keyword localization
|
---|
399 |
|
---|
400 | \section2 Localize
|
---|
401 |
|
---|
402 | Localization is the process of adapting to local conventions, for
|
---|
403 | example presenting dates and times using the locally preferred
|
---|
404 | formats. Such localizations can be accomplished using appropriate tr()
|
---|
405 | strings.
|
---|
406 |
|
---|
407 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 11
|
---|
408 |
|
---|
409 | In the example, for the US we would leave the translation of
|
---|
410 | "AMPM" as it is and thereby use the 12-hour clock branch; but in
|
---|
411 | Europe we would translate it as something else and this will make
|
---|
412 | the code use the 24-hour clock branch.
|
---|
413 |
|
---|
414 | For localized numbers use the QLocale class.
|
---|
415 |
|
---|
416 | Localizing images is not recommended. Choose clear icons that are
|
---|
417 | appropriate for all localities, rather than relying on local puns or
|
---|
418 | stretched metaphors. The exception is for images of left and right
|
---|
419 | pointing arrows which may need to be reversed for Arabic and Hebrew
|
---|
420 | locales.
|
---|
421 |
|
---|
422 | \section1 Dynamic Translation
|
---|
423 |
|
---|
424 | Some applications, such as Qt Linguist, must be able to support changes
|
---|
425 | to the user's language settings while they are still running. To make
|
---|
426 | widgets aware of changes to the installed QTranslators, reimplement the
|
---|
427 | widget's \l{QWidget::changeEvent()}{changeEvent()} function to check whether
|
---|
428 | the event is a \l{QEvent::LanguageChange}{LanguageChange} event, and update
|
---|
429 | the text displayed by widgets using the \l{QObject::tr()}{tr()} function
|
---|
430 | in the usual way. For example:
|
---|
431 |
|
---|
432 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 12
|
---|
433 |
|
---|
434 | All other change events should be passed on by calling the default
|
---|
435 | implementation of the function.
|
---|
436 |
|
---|
437 | The list of installed translators might change in reaction to a
|
---|
438 | \l{QEvent::LocaleChange}{LocaleChange} event, or the application might
|
---|
439 | provide a user interface that allows the user to change the current
|
---|
440 | application language.
|
---|
441 |
|
---|
442 | The default event handler for QWidget subclasses responds to the
|
---|
443 | QEvent::LanguageChange event, and will call this function when necessary.
|
---|
444 |
|
---|
445 | \l{QEvent::LanguageChange}{LanguageChange} events are posted when a new
|
---|
446 | translation is installed using the QCoreApplication::installTranslator()
|
---|
447 | function. Additionally, other application components can also force
|
---|
448 | widgets to update themselves by posting LanguageChange events to them.
|
---|
449 |
|
---|
450 |
|
---|
451 | \section1 Translating Non-Qt Classes
|
---|
452 |
|
---|
453 | It is sometimes necessary to provide internationalization support for
|
---|
454 | strings used in classes that do not inherit QObject or use the Q_OBJECT
|
---|
455 | macro to enable translation features. Since Qt translates strings at
|
---|
456 | run-time based on the class they are associated with and \c lupdate
|
---|
457 | looks for translatable strings in the source code, non-Qt classes must
|
---|
458 | use mechanisms that also provide this information.
|
---|
459 |
|
---|
460 | One way to do this is to add translation support to a non-Qt class
|
---|
461 | using the Q_DECLARE_TR_FUNCTIONS() macro; for example:
|
---|
462 |
|
---|
463 | \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 0
|
---|
464 | \dots
|
---|
465 | \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 1
|
---|
466 |
|
---|
467 | This provides the class with \l{QObject::}{tr()} functions that can
|
---|
468 | be used to translate strings associated with the class, and makes it
|
---|
469 | possible for \c lupdate to find translatable strings in the source
|
---|
470 | code.
|
---|
471 |
|
---|
472 | Alternatively, the QCoreApplication::translate() function can be called
|
---|
473 | with a specific context, and this will be recognized by \c lupdate and
|
---|
474 | Qt Linguist.
|
---|
475 |
|
---|
476 | \section1 System Support
|
---|
477 |
|
---|
478 | Some of the operating systems and windowing systems that Qt runs on
|
---|
479 | only have limited support for Unicode. The level of support available
|
---|
480 | in the underlying system has some influence on the support that Qt can
|
---|
481 | provide on those platforms, although in general Qt applications need
|
---|
482 | not be too concerned with platform-specific limitations.
|
---|
483 |
|
---|
484 | \section2 Unix/X11
|
---|
485 |
|
---|
486 | \list
|
---|
487 | \o Locale-oriented fonts and input methods. Qt hides these and
|
---|
488 | provides Unicode input and output.
|
---|
489 | \o Filesystem conventions such as
|
---|
490 | \l{http://www.ietf.org/rfc/rfc2279.txt}{UTF-8}
|
---|
491 | are under development in some Unix variants. All Qt file
|
---|
492 | functions allow Unicode, but convert filenames to the local
|
---|
493 | 8-bit encoding, as this is the Unix convention (see
|
---|
494 | QFile::setEncodingFunction() to explore alternative
|
---|
495 | encodings).
|
---|
496 | \o File I/O defaults to the local 8-bit encoding,
|
---|
497 | with Unicode options in QTextStream.
|
---|
498 | \o Many Unix distributions contain only partial support for some locales.
|
---|
499 | For example, if you have a \c /usr/share/locale/ja_JP.EUC directory,
|
---|
500 | this does not necessarily mean you can display Japanese text; you also
|
---|
501 | need JIS encoded fonts (or Unicode fonts), and the
|
---|
502 | \c /usr/share/locale/ja_JP.EUC directory needs to be complete. For
|
---|
503 | best results, use complete locales from your system vendor.
|
---|
504 | \endlist
|
---|
505 |
|
---|
506 | \section2 Windows
|
---|
507 |
|
---|
508 | \list
|
---|
509 | \o Qt provides full Unicode support, including input methods, fonts,
|
---|
510 | clipboard, drag-and-drop and file names.
|
---|
511 | \o File I/O defaults to Latin1, with Unicode options in QTextStream.
|
---|
512 | Note that some Windows programs do not understand big-endian
|
---|
513 | Unicode text files even though that is the order prescribed by
|
---|
514 | the Unicode Standard in the absence of higher-level protocols.
|
---|
515 | \o Unlike programs written with MFC or plain winlib, Qt programs
|
---|
516 | are portable between Windows 98 and Windows NT.
|
---|
517 | \e {You do not need different binaries to support Unicode.}
|
---|
518 | \endlist
|
---|
519 |
|
---|
520 | \section2 Mac OS X
|
---|
521 |
|
---|
522 | For details on Mac-specific translation, refer to the Qt/Mac Specific Issues
|
---|
523 | document \l{Qt for Mac OS X - Specific Issues#Translating the Application Menu and Native Dialogs}{here}.
|
---|
524 | */
|
---|
525 |
|
---|
526 | /*!
|
---|
527 | \page i18n-source-translation.html
|
---|
528 | \title Writing Source Code for Translation
|
---|
529 | \ingroup i18n
|
---|
530 | \previouspage Internationalization with Qt
|
---|
531 | \contentspage Internationalization with Qt
|
---|
532 | \nextpage Translation Rules for Plurals
|
---|
533 | \brief How to write source code in a way that makes it possible for user-visible text to be translated.
|
---|
534 |
|
---|
535 | \tableofcontents
|
---|
536 |
|
---|
537 | \section1 The Basics
|
---|
538 |
|
---|
539 | Developers use the \l{QObject::}{tr()} function to obtain translated text
|
---|
540 | for their classes, typically for display purposes. This function is also
|
---|
541 | used to indicate which text strings in an application are translatable.
|
---|
542 |
|
---|
543 | Qt indexes each translatable string by the \e{translation context} it is
|
---|
544 | associated with; this is generally the name of the QObject subclass it is
|
---|
545 | used in.
|
---|
546 |
|
---|
547 | Translation contexts are defined for new QObject-based classes by the use
|
---|
548 | of the Q_OBJECT macro in each new class definition.
|
---|
549 |
|
---|
550 | When tr() is called, it looks up the translatable string using a QTranslator
|
---|
551 | object. For translation to work, one or more of these must have been
|
---|
552 | installed on the application object in the way described in the
|
---|
553 | \l{#Enabling Translation}{Enabling Translation} section below.
|
---|
554 |
|
---|
555 | \section1 Defining a Translation Context
|
---|
556 |
|
---|
557 | The translation context for QObject and each QObject subclass is the
|
---|
558 | class name itself. Developers subclassing QObject must use the
|
---|
559 | Q_OBJECT macro in their class definition to override the translation
|
---|
560 | context. This macro sets the context to the name of the subclass.
|
---|
561 |
|
---|
562 | For example, the following class definition includes the Q_OBJECT macro,
|
---|
563 | implementing a new tr() that uses the \c MainWindow context:
|
---|
564 |
|
---|
565 | \snippet mainwindows/sdi/mainwindow.h class definition with macro
|
---|
566 | \dots
|
---|
567 |
|
---|
568 | If Q_OBJECT is not used in a class definition, the context will be
|
---|
569 | inherited from the base class. For example, since all QObject-based
|
---|
570 | classes in Qt provide a context, a new QWidget subclass defined without
|
---|
571 | a Q_OBJECT macro will use the \c QWidget context if its tr() function
|
---|
572 | is invoked.
|
---|
573 |
|
---|
574 | \section1 Using tr() to Obtain a Translation
|
---|
575 |
|
---|
576 | The following example shows how a translation is obtained for the
|
---|
577 | class shown in the previous section:
|
---|
578 |
|
---|
579 | \snippet mainwindows/sdi/mainwindow.cpp implicit tr context
|
---|
580 | \dots
|
---|
581 |
|
---|
582 | Here, the translation context is \c MainWindow because it is the
|
---|
583 | \c MainWindow::tr() function that is invoked. The text returned
|
---|
584 | by the tr() function is a translation of "&File" obtained from
|
---|
585 | the \c MainWindow context.
|
---|
586 |
|
---|
587 | When Qt's translation tool, \l lupdate, is used to process a set of source
|
---|
588 | files, the text wrapped in tr() calls is stored in a section of the translation
|
---|
589 | file that corresponds to its translation context.
|
---|
590 |
|
---|
591 | In some situations, it is useful to give a translation context explicitly
|
---|
592 | by fully qualifying the call to tr(); for example:
|
---|
593 |
|
---|
594 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp explicit tr context
|
---|
595 |
|
---|
596 | This call obtains the translated text for "Page up" from the \c QScrollBar
|
---|
597 | context. Developers can also use the QCoreApplication::translate() function
|
---|
598 | to obtain a translation for a particular translation context.
|
---|
599 |
|
---|
600 | \section1 Translator Comments
|
---|
601 |
|
---|
602 | Developers can include information about each translatable string to
|
---|
603 | help translators with the translation process. These are extracted
|
---|
604 | when \l lupdate is used to process the source files. The recommended
|
---|
605 | way to add comments is to annotate the tr() calls in your code with
|
---|
606 | comments of the form:
|
---|
607 |
|
---|
608 | \tt{//: ...}
|
---|
609 |
|
---|
610 | or
|
---|
611 |
|
---|
612 | \tt{\begincomment: ... \endcomment}
|
---|
613 |
|
---|
614 | Examples:
|
---|
615 |
|
---|
616 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 40
|
---|
617 |
|
---|
618 | In these examples, the comments will be associated with the strings
|
---|
619 | passed to tr() in the context of each call.
|
---|
620 |
|
---|
621 | \section1 Adding Meta-Data to Strings
|
---|
622 |
|
---|
623 | Additional data can be attached to each translatable message. These are
|
---|
624 | extracted when \l lupdate is used to process the source files. The
|
---|
625 | recommended way to add meta-data is to annotate the tr() calls in your code
|
---|
626 | with comments of the form:
|
---|
627 |
|
---|
628 | \tt{//= <id>}
|
---|
629 |
|
---|
630 | This can be used to give the message a unique identifier to support tools
|
---|
631 | which need it.
|
---|
632 |
|
---|
633 | An alternative way to attach meta-data is to use the following syntax:
|
---|
634 |
|
---|
635 | \tt{//~ <field name> <field contents>}
|
---|
636 |
|
---|
637 | This can be used to attach meta-data to the message. The field name should
|
---|
638 | consist of a domain prefix (possibly the conventional file extension of the
|
---|
639 | file format the field is inspired by), a hyphen and the actual field name
|
---|
640 | in underscore-delimited notation. For storage in TS files, the field name
|
---|
641 | together with the prefix "extra-" will form an XML element name. The field
|
---|
642 | contents will be XML-escaped, but otherwise appear verbatim as the
|
---|
643 | element's contents. Any number of unique fields can be added to each
|
---|
644 | message.
|
---|
645 |
|
---|
646 | Example:
|
---|
647 |
|
---|
648 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp meta data
|
---|
649 |
|
---|
650 | Meta-data appearing right in front of a magic TRANSLATOR comment applies to
|
---|
651 | the whole TS file.
|
---|
652 |
|
---|
653 | \section1 Disambiguation
|
---|
654 |
|
---|
655 | If the same translatable string is used in different roles within the same
|
---|
656 | translation context, an additional identifying string may be passed in
|
---|
657 | the call to \l{QObject::}{tr()}. This optional disambiguation argument
|
---|
658 | is used to distinguish between otherwise identical strings.
|
---|
659 |
|
---|
660 | Example:
|
---|
661 |
|
---|
662 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 17
|
---|
663 | \dots
|
---|
664 |
|
---|
665 | In Qt 4.4 and earlier, this disambiguation parameter was the preferred
|
---|
666 | way to specify comments to translators.
|
---|
667 |
|
---|
668 | \section1 Character Encodings
|
---|
669 |
|
---|
670 | You can set the encoding for the source text by calling QTextCodec::setCodecForTr().
|
---|
671 | By default, the source text is assumed to be in Latin-1 encoding.
|
---|
672 |
|
---|
673 | \section1 Handling Plurals
|
---|
674 |
|
---|
675 | Some translatable strings contain placeholders for integer values and need
|
---|
676 | to be translated differently depending on the values in use.
|
---|
677 |
|
---|
678 | To help with this problem, developers pass an additional integer argument
|
---|
679 | to the \l{QObject::}{tr()} function, and typically use a special notation
|
---|
680 | for plurals in each translatable string.
|
---|
681 |
|
---|
682 | If this argument is equal or greater than zero, all occurrences of
|
---|
683 | \c %n in the resulting string are replaced with a decimal representation
|
---|
684 | of the value supplied. In addition, the translation used will adapt to the
|
---|
685 | value according to the rules for each language.
|
---|
686 |
|
---|
687 | Example:
|
---|
688 |
|
---|
689 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 18
|
---|
690 |
|
---|
691 | The table below shows what string is returned depending on the
|
---|
692 | active translation:
|
---|
693 |
|
---|
694 | \table
|
---|
695 | \header \o \o{3,1} Active Translation
|
---|
696 | \header \o \a n \o No Translation \o French \o English
|
---|
697 | \row \o 0 \o "0 message(s) saved" \o "0 message sauvegard\unicode{0xE9}" \o "0 message\bold{s} saved"
|
---|
698 | \row \o 1 \o "1 message(s) saved" \o "1 message sauvegard\unicode{0xE9}" \o "1 message saved"
|
---|
699 | \row \o 2 \o "2 message(s) saved" \o "2 message\bold{s} sauvegard\unicode{0xE9}\bold{s}" \o "2 message\bold{s} saved"
|
---|
700 | \row \o 37 \o "37 message(s) saved" \o "37 message\bold{s} sauvegard\unicode{0xE9}\bold{s}" \o "37 message\bold{s} saved"
|
---|
701 | \endtable
|
---|
702 |
|
---|
703 | This idiom is more flexible than the traditional approach; e.g.,
|
---|
704 |
|
---|
705 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 19
|
---|
706 |
|
---|
707 | because it also works with target languages that have several
|
---|
708 | plural forms (e.g., Irish has a special "dual" form that should
|
---|
709 | be used when \c n is 2), and it handles the \e n == 0 case
|
---|
710 | correctly for languages such as French that require the singular.
|
---|
711 | See the \l{Qt Linguist Manual} for details.
|
---|
712 |
|
---|
713 | Instead of \c %n, you can use \c %Ln to produce a localized
|
---|
714 | representation of \a n. The conversion uses the default locale,
|
---|
715 | set using QLocale::setDefault(). (If no default locale was
|
---|
716 | specified, the "C" locale is used.)
|
---|
717 |
|
---|
718 | A summary of the rules used to translate strings containing plurals can be
|
---|
719 | found in the \l{Translation Rules for Plurals} document.
|
---|
720 |
|
---|
721 | \section1 Enabling Translation
|
---|
722 |
|
---|
723 | Typically, your application's \c main() function will look like
|
---|
724 | this:
|
---|
725 |
|
---|
726 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 8
|
---|
727 |
|
---|
728 | Note the use of QLibraryInfo::location() to locate the Qt translations.
|
---|
729 | Developers should request the path to the translations at run-time by
|
---|
730 | passing QLibraryInfo::TranslationsPath to this function instead of
|
---|
731 | using the \c QTDIR environment variable in their applications.
|
---|
732 |
|
---|
733 | \section1 Further Reading
|
---|
734 |
|
---|
735 | \l{Qt Linguist Manual}, \l{Hello tr Example}, \l{Translation Rules for Plurals}
|
---|
736 | */
|
---|
737 |
|
---|
738 | /*!
|
---|
739 | \page i18n-plural-rules.html
|
---|
740 | \title Translation Rules for Plurals
|
---|
741 | \ingroup i18n
|
---|
742 | \previouspage Writing Source Code for Translation
|
---|
743 | \contentspage Internationalization with Qt
|
---|
744 | \brief A summary of the translation rules for plurals produced by Qt's i18n tools.
|
---|
745 |
|
---|
746 | The table below shows the specific rules that are produced by Qt Linguist
|
---|
747 | and \c lrelease for a selection of languages. Cells marked \e otherwise
|
---|
748 | indicate the form used when none of the other rules are appropriate for a
|
---|
749 | specific language.
|
---|
750 |
|
---|
751 | \table 80%
|
---|
752 | \header \o Language \o Rule 1 \o Rule 2 \o Rule 3
|
---|
753 | \row \o English \o \c{n == 1}
|
---|
754 | \o \e{otherwise} \o N/A
|
---|
755 | \row \o French \o \c{n < 2}
|
---|
756 | \o \e{otherwise} \o N/A
|
---|
757 | \row \o Czech \o \c{n % 100 == 1}
|
---|
758 | \o \c{n % 100 >= 2 && n % 100 <= 4}
|
---|
759 | \o \e{otherwise}
|
---|
760 | \row \o Irish \o \c{n == 1}
|
---|
761 | \o \c{n == 2} \o \e{otherwise}
|
---|
762 | \row \o Latvian \o \c{n % 10 == 1&& n % 100 != 11}
|
---|
763 | \o \c{n != 0} \o \e{otherwise}
|
---|
764 | \row \o Lithuanian \o \c{n % 10 == 1&& n % 100 != 11}
|
---|
765 | \o \c{n % 100 != 12 && n % 10 == 2}
|
---|
766 | \o \e{otherwise}
|
---|
767 | \row \o Macedonian \o \c{n % 10 == 1}
|
---|
768 | \o \c{n % 10 == 2} \o \e{otherwise}
|
---|
769 | \row \o Polish \o \c{n == 1}
|
---|
770 | \o \c{n % 10 >= 2 && n % 10 <= 4
|
---|
771 | && (n % 100 < 10 || n % 100 > 20)}
|
---|
772 | \o \e{otherwise}
|
---|
773 | \row \o Romanian \o \c{n == 1}
|
---|
774 | \o \c{n == 0|| (n % 100 >= 1 && n % 100 <= 20)}
|
---|
775 | \o \e{otherwise}
|
---|
776 | \row \o Russian \o \c{n % 10 == 1&& n % 100 != 11}
|
---|
777 | \o \c{n % 10 >= 2 && n % 10 <= 4
|
---|
778 | && (n % 100 < 10 || n % 100 > 20)}
|
---|
779 | \o \e{otherwise}
|
---|
780 | \row \o Slovak \o \c{n == 1} \o \c{n >= 2 && n <= 4}
|
---|
781 | \o \e{otherwise}
|
---|
782 | \row \o Japanese \o \e{otherwise} \o N/A \o N/A
|
---|
783 | \endtable
|
---|
784 |
|
---|
785 | The rules themselves are not documented and are internal to Qt Linguist and \c lrelease.
|
---|
786 | */
|
---|