|
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 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 |
|
55 \keyword internationalization |
|
56 \keyword i18n |
|
57 |
|
58 The internationalization of an application is the process of making |
|
59 the application usable by people in countries other than one's own. |
|
60 |
|
61 \tableofcontents |
|
62 |
|
63 \section1 Relevant Qt Classes and APIs |
|
64 |
|
65 These classes support internationalizing of Qt applications. |
|
66 |
|
67 \annotatedlist i18n |
|
68 |
|
69 \section1 Languages and Writing Systems |
|
70 |
|
71 In some cases internationalization is simple, for example, making a US |
|
72 application accessible to Australian or British users may require |
|
73 little more than a few spelling corrections. But to make a US |
|
74 application usable by Japanese users, or a Korean application usable |
|
75 by German users, will require that the software operate not only in |
|
76 different languages, but use different input techniques, character |
|
77 encodings and presentation conventions. |
|
78 |
|
79 Qt tries to make internationalization as painless as possible for |
|
80 developers. All input widgets and text drawing methods in Qt offer |
|
81 built-in support for all supported languages. The built-in font engine |
|
82 is capable of correctly and attractively rendering text that contains |
|
83 characters from a variety of different writing systems at the same |
|
84 time. |
|
85 |
|
86 Qt supports most languages in use today, in particular: |
|
87 \list |
|
88 \o All East Asian languages (Chinese, Japanese and Korean) |
|
89 \o All Western languages (using Latin script) |
|
90 \o Arabic |
|
91 \o Cyrillic languages (Russian, Ukrainian, etc.) |
|
92 \o Greek |
|
93 \o Hebrew |
|
94 \o Thai and Lao |
|
95 \o All scripts in Unicode 4.0 that do not require special processing |
|
96 \endlist |
|
97 |
|
98 On Windows, Unix/X11 with FontConfig (client side font support) |
|
99 and Qt for Embedded Linux the following languages are also supported: |
|
100 \list |
|
101 \o Bengali |
|
102 \o Devanagari |
|
103 \o Dhivehi (Thaana) |
|
104 \o Gujarati |
|
105 \o Gurmukhi |
|
106 \o Kannada |
|
107 \o Khmer |
|
108 \o Malayalam |
|
109 \o Myanmar |
|
110 \o Syriac |
|
111 \o Tamil |
|
112 \o Telugu |
|
113 \o Tibetan |
|
114 \endlist |
|
115 |
|
116 Many of these writing systems exhibit special features: |
|
117 |
|
118 \list |
|
119 |
|
120 \o \bold{Special line breaking behavior.} Some of the Asian languages are |
|
121 written without spaces between words. Line breaking can occur either |
|
122 after every character (with exceptions) as in Chinese, Japanese and |
|
123 Korean, or after logical word boundaries as in Thai. |
|
124 |
|
125 \o \bold{Bidirectional writing.} Arabic and Hebrew are written from right to |
|
126 left, except for numbers and embedded English text which is written |
|
127 left to right. The exact behavior is defined in the |
|
128 \l{http://www.unicode.org/unicode/reports/tr9/}{Unicode Technical Annex #9}. |
|
129 |
|
130 \o \bold{Non-spacing or diacritical marks (accents or umlauts in European |
|
131 languages).} Some languages such as Vietnamese make extensive use of |
|
132 these marks and some characters can have more than one mark at the |
|
133 same time to clarify pronunciation. |
|
134 |
|
135 \o \bold{Ligatures.} In special contexts, some pairs of characters get |
|
136 replaced by a combined glyph forming a ligature. Common examples are |
|
137 the fl and fi ligatures used in typesetting US and European books. |
|
138 |
|
139 \endlist |
|
140 |
|
141 Qt tries to take care of all the special features listed above. You |
|
142 usually don't have to worry about these features so long as you use |
|
143 Qt's input widgets (e.g. QLineEdit, QTextEdit, and derived classes) |
|
144 and Qt's display widgets (e.g. QLabel). |
|
145 |
|
146 Support for these writing systems is transparent to the |
|
147 programmer and completely encapsulated in \l{rich text |
|
148 processing}{Qt's text engine}. This means that you don't need to |
|
149 have any knowledge about the writing system used in a particular |
|
150 language, except for the following small points: |
|
151 |
|
152 \list |
|
153 |
|
154 \o QPainter::drawText(int x, int y, const QString &str) will always |
|
155 draw the string with its left edge at the position specified with |
|
156 the x, y parameters. This will usually give you left aligned strings. |
|
157 Arabic and Hebrew application strings are usually right |
|
158 aligned, so for these languages use the version of drawText() that |
|
159 takes a QRect since this will align in accordance with the language. |
|
160 |
|
161 \o When you write your own text input controls, use QTextLayout. |
|
162 In some languages (e.g. Arabic or languages from the Indian |
|
163 subcontinent), the width and shape of a glyph changes depending on the |
|
164 surrounding characters, which QTextLayout takes into account. |
|
165 Writing input controls usually requires a certain knowledge of the |
|
166 scripts it is going to be used in. Usually the easiest way is to |
|
167 subclass QLineEdit or QTextEdit. |
|
168 |
|
169 \endlist |
|
170 |
|
171 The following sections give some information on the status of the |
|
172 internationalization (i18n) support in Qt. See also the \l{Qt |
|
173 Linguist manual}. |
|
174 |
|
175 \section1 Step by Step |
|
176 |
|
177 Writing cross-platform international software with Qt is a gentle, |
|
178 incremental process. Your software can become internationalized in |
|
179 the following stages: |
|
180 |
|
181 \section2 Use QString for All User-Visible Text |
|
182 |
|
183 Since QString uses the Unicode 4.0 encoding internally, every |
|
184 language in the world can be processed transparently using |
|
185 familiar text processing operations. Also, since all Qt functions |
|
186 that present text to the user take a QString as a parameter, |
|
187 there is no \c{char *} to QString conversion overhead. |
|
188 |
|
189 Strings that are in "programmer space" (such as QObject names |
|
190 and file format texts) need not use QString; the traditional |
|
191 \c{char *} or the QByteArray class will suffice. |
|
192 |
|
193 You're unlikely to notice that you are using Unicode; |
|
194 QString, and QChar are just like easier versions of the crude |
|
195 \c{const char *} and char from traditional C. |
|
196 |
|
197 \section2 Use tr() for All Literal Text |
|
198 |
|
199 Wherever your program uses "quoted text" for text that will |
|
200 be presented to the user, ensure that it is processed by the \l |
|
201 QCoreApplication::translate() function. Essentially all that is necessary |
|
202 to achieve this is to use QObject::tr(). For example, assuming the |
|
203 \c LoginWidget is a subclass of QWidget: |
|
204 |
|
205 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 0 |
|
206 |
|
207 This accounts for 99% of the user-visible strings you're likely to |
|
208 write. |
|
209 |
|
210 If the quoted text is not in a member function of a |
|
211 QObject subclass, use either the tr() function of an |
|
212 appropriate class, or the QCoreApplication::translate() function |
|
213 directly: |
|
214 |
|
215 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 1 |
|
216 |
|
217 If you need to have translatable text completely |
|
218 outside a function, there are two macros to help: QT_TR_NOOP() |
|
219 and QT_TRANSLATE_NOOP(). They merely mark the text for |
|
220 extraction by the \c lupdate utility described below. |
|
221 The macros expand to just the text (without the context). |
|
222 |
|
223 Example of QT_TR_NOOP(): |
|
224 |
|
225 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 2 |
|
226 |
|
227 Example of QT_TRANSLATE_NOOP(): |
|
228 |
|
229 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 3 |
|
230 |
|
231 If you disable the \c{const char *} to QString automatic |
|
232 conversion by compiling your software with the macro \c |
|
233 QT_NO_CAST_FROM_ASCII defined, you'll be very likely to catch any |
|
234 strings you are missing. See QString::fromLatin1() for more |
|
235 information. Disabling the conversion can make programming a bit |
|
236 cumbersome. |
|
237 |
|
238 If your source language uses characters outside Latin1, you |
|
239 might find QObject::trUtf8() more convenient than |
|
240 QObject::tr(), as tr() depends on the |
|
241 QTextCodec::codecForTr(), which makes it more fragile than |
|
242 QObject::trUtf8(). |
|
243 |
|
244 \section2 Use QKeySequence() for Accelerator Values |
|
245 |
|
246 Accelerator values such as Ctrl+Q or Alt+F need to be translated |
|
247 too. If you hardcode Qt::CTRL + Qt::Key_Q for "quit" in your |
|
248 application, translators won't be able to override it. The |
|
249 correct idiom is |
|
250 |
|
251 \snippet examples/mainwindows/application/mainwindow.cpp 20 |
|
252 |
|
253 \section2 Use QString::arg() for Dynamic Text |
|
254 |
|
255 The QString::arg() functions offer a simple means for substituting |
|
256 arguments: |
|
257 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 4 |
|
258 |
|
259 In some languages the order of arguments may need to change, and this |
|
260 can easily be achieved by changing the order of the % arguments. For |
|
261 example: |
|
262 |
|
263 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 5 |
|
264 |
|
265 produces the correct output in English and Norwegian: |
|
266 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 6 |
|
267 |
|
268 \section2 Produce Translations |
|
269 |
|
270 Once you are using tr() throughout an application, you can start |
|
271 producing translations of the user-visible text in your program. |
|
272 |
|
273 The \l{Qt Linguist manual} provides further information about |
|
274 Qt's translation tools, \e{Qt Linguist}, \c lupdate and \c |
|
275 lrelease. |
|
276 |
|
277 Translation of a Qt application is a three-step process: |
|
278 |
|
279 \list 1 |
|
280 |
|
281 \o Run \c lupdate to extract translatable text from the C++ |
|
282 source code of the Qt application, resulting in a message file |
|
283 for translators (a TS file). The utility recognizes the tr() |
|
284 construct and the \c{QT_TR*_NOOP()} macros described above and |
|
285 produces TS files (usually one per language). |
|
286 |
|
287 \o Provide translations for the source texts in the TS file, using |
|
288 \e{Qt Linguist}. Since TS files are in XML format, you can also |
|
289 edit them by hand. |
|
290 |
|
291 \o Run \c lrelease to obtain a light-weight message file (a QM |
|
292 file) from the TS file, suitable only for end use. Think of the TS |
|
293 files as "source files", and QM files as "object files". The |
|
294 translator edits the TS files, but the users of your application |
|
295 only need the QM files. Both kinds of files are platform and |
|
296 locale independent. |
|
297 |
|
298 \endlist |
|
299 |
|
300 Typically, you will repeat these steps for every release of your |
|
301 application. The \c lupdate utility does its best to reuse the |
|
302 translations from previous releases. |
|
303 |
|
304 Before you run \c lupdate, you should prepare a project file. Here's |
|
305 an example project file (\c .pro file): |
|
306 |
|
307 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 7 |
|
308 |
|
309 When you run \c lupdate or \c lrelease, you must give the name of the |
|
310 project file as a command-line argument. |
|
311 |
|
312 In this example, four exotic languages are supported: Danish, |
|
313 Finnish, Norwegian and Swedish. If you use \l{qmake}, you usually |
|
314 don't need an extra project file for \c lupdate; your \c qmake |
|
315 project file will work fine once you add the \c TRANSLATIONS |
|
316 entry. |
|
317 |
|
318 In your application, you must \l QTranslator::load() the translation |
|
319 files appropriate for the user's language, and install them using \l |
|
320 QCoreApplication::installTranslator(). |
|
321 |
|
322 \c linguist, \c lupdate and \c lrelease are installed in the \c bin |
|
323 subdirectory of the base directory Qt is installed into. Click Help|Manual |
|
324 in \e{Qt Linguist} to access the user's manual; it contains a tutorial |
|
325 to get you started. |
|
326 |
|
327 \target qt-itself |
|
328 Qt itself contains over 400 strings that will also need to be |
|
329 translated into the languages that you are targeting. You will find |
|
330 translation files for French, German and Simplified Chinese in |
|
331 \c{$QTDIR/translations}, as well as a template for translating to |
|
332 other languages. (This directory also contains some additional |
|
333 unsupported translations which may be useful.) |
|
334 |
|
335 Typically, your application's \c main() function will look like |
|
336 this: |
|
337 |
|
338 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 8 |
|
339 |
|
340 Note the use of QLibraryInfo::location() to locate the Qt translations. |
|
341 Developers should request the path to the translations at run-time by |
|
342 passing QLibraryInfo::TranslationsPath to this function instead of |
|
343 using the \c QTDIR environment variable in their applications. |
|
344 |
|
345 \section2 Support for Encodings |
|
346 |
|
347 The QTextCodec class and the facilities in QTextStream make it easy to |
|
348 support many input and output encodings for your users' data. When an |
|
349 application starts, the locale of the machine will determine the 8-bit |
|
350 encoding used when dealing with 8-bit data: such as for font |
|
351 selection, text display, 8-bit text I/O, and character input. |
|
352 |
|
353 The application may occasionally require encodings other than the |
|
354 default local 8-bit encoding. For example, an application in a |
|
355 Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might |
|
356 need to output Cyrillic in the ISO 8859-5 encoding. Code for this |
|
357 would be: |
|
358 |
|
359 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 9 |
|
360 |
|
361 For converting Unicode to local 8-bit encodings, a shortcut is |
|
362 available: the QString::toLocal8Bit() function returns such 8-bit |
|
363 data. Another useful shortcut is QString::toUtf8(), which returns |
|
364 text in the 8-bit UTF-8 encoding: this perfectly preserves |
|
365 Unicode information while looking like plain ASCII if the text is |
|
366 wholly ASCII. |
|
367 |
|
368 For converting the other way, there are the QString::fromUtf8() and |
|
369 QString::fromLocal8Bit() convenience functions, or the general code, |
|
370 demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode |
|
371 conversion: |
|
372 |
|
373 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 10 |
|
374 |
|
375 Ideally Unicode I/O should be used as this maximizes the portability |
|
376 of documents between users around the world, but in reality it is |
|
377 useful to support all the appropriate encodings that your users will |
|
378 need to process existing documents. In general, Unicode (UTF-16 or |
|
379 UTF-8) is best for information transferred between arbitrary people, |
|
380 while within a language or national group, a local standard is often |
|
381 more appropriate. The most important encoding to support is the one |
|
382 returned by QTextCodec::codecForLocale(), as this is the one the user |
|
383 is most likely to need for communicating with other people and |
|
384 applications (this is the codec used by local8Bit()). |
|
385 |
|
386 Qt supports most of the more frequently used encodings natively. For a |
|
387 complete list of supported encodings see the \l QTextCodec |
|
388 documentation. |
|
389 |
|
390 In some cases and for less frequently used encodings it may be |
|
391 necessary to write your own QTextCodec subclass. Depending on the |
|
392 urgency, it may be useful to contact Qt's technical support team or |
|
393 ask on the \c qt-interest mailing list to see if someone else is |
|
394 already working on supporting the encoding. |
|
395 |
|
396 \keyword localization |
|
397 |
|
398 \section2 Localize |
|
399 |
|
400 Localization is the process of adapting to local conventions, for |
|
401 example presenting dates and times using the locally preferred |
|
402 formats. Such localizations can be accomplished using appropriate tr() |
|
403 strings. |
|
404 |
|
405 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 11 |
|
406 |
|
407 In the example, for the US we would leave the translation of |
|
408 "AMPM" as it is and thereby use the 12-hour clock branch; but in |
|
409 Europe we would translate it as something else and this will make |
|
410 the code use the 24-hour clock branch. |
|
411 |
|
412 For localized numbers use the QLocale class. |
|
413 |
|
414 Localizing images is not recommended. Choose clear icons that are |
|
415 appropriate for all localities, rather than relying on local puns or |
|
416 stretched metaphors. The exception is for images of left and right |
|
417 pointing arrows which may need to be reversed for Arabic and Hebrew |
|
418 locales. |
|
419 |
|
420 \section1 Dynamic Translation |
|
421 |
|
422 Some applications, such as Qt Linguist, must be able to support changes |
|
423 to the user's language settings while they are still running. To make |
|
424 widgets aware of changes to the installed QTranslators, reimplement the |
|
425 widget's \l{QWidget::changeEvent()}{changeEvent()} function to check whether |
|
426 the event is a \l{QEvent::LanguageChange}{LanguageChange} event, and update |
|
427 the text displayed by widgets using the \l{QObject::tr()}{tr()} function |
|
428 in the usual way. For example: |
|
429 |
|
430 \snippet doc/src/snippets/code/doc_src_i18n.qdoc 12 |
|
431 |
|
432 All other change events should be passed on by calling the default |
|
433 implementation of the function. |
|
434 |
|
435 The list of installed translators might change in reaction to a |
|
436 \l{QEvent::LocaleChange}{LocaleChange} event, or the application might |
|
437 provide a user interface that allows the user to change the current |
|
438 application language. |
|
439 |
|
440 The default event handler for QWidget subclasses responds to the |
|
441 QEvent::LanguageChange event, and will call this function when necessary; |
|
442 other application components can also force widgets to update themselves |
|
443 by posting the \l{QEvent::LanguageChange}{LanguageChange} event to them. |
|
444 |
|
445 \section1 Translating Non-Qt Classes |
|
446 |
|
447 It is sometimes necessary to provide internationalization support for |
|
448 strings used in classes that do not inherit QObject or use the Q_OBJECT |
|
449 macro to enable translation features. Since Qt translates strings at |
|
450 run-time based on the class they are associated with and \c lupdate |
|
451 looks for translatable strings in the source code, non-Qt classes must |
|
452 use mechanisms that also provide this information. |
|
453 |
|
454 One way to do this is to add translation support to a non-Qt class |
|
455 using the Q_DECLARE_TR_FUNCTIONS() macro; for example: |
|
456 |
|
457 \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 0 |
|
458 \dots |
|
459 \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 1 |
|
460 |
|
461 This provides the class with \l{QObject::}{tr()} functions that can |
|
462 be used to translate strings associated with the class, and makes it |
|
463 possible for \c lupdate to find translatable strings in the source |
|
464 code. |
|
465 |
|
466 Alternatively, the QCoreApplication::translate() function can be called |
|
467 with a specific context, and this will be recognized by \c lupdate and |
|
468 Qt Linguist. |
|
469 |
|
470 \section1 System Support |
|
471 |
|
472 Some of the operating systems and windowing systems that Qt runs on |
|
473 only have limited support for Unicode. The level of support available |
|
474 in the underlying system has some influence on the support that Qt can |
|
475 provide on those platforms, although in general Qt applications need |
|
476 not be too concerned with platform-specific limitations. |
|
477 |
|
478 \section2 Unix/X11 |
|
479 |
|
480 \list |
|
481 \o Locale-oriented fonts and input methods. Qt hides these and |
|
482 provides Unicode input and output. |
|
483 \o Filesystem conventions such as |
|
484 \l{http://www.ietf.org/rfc/rfc2279.txt}{UTF-8} |
|
485 are under development in some Unix variants. All Qt file |
|
486 functions allow Unicode, but convert filenames to the local |
|
487 8-bit encoding, as this is the Unix convention (see |
|
488 QFile::setEncodingFunction() to explore alternative |
|
489 encodings). |
|
490 \o File I/O defaults to the local 8-bit encoding, |
|
491 with Unicode options in QTextStream. |
|
492 \o Many Unix distributions contain only partial support for some locales. |
|
493 For example, if you have a \c /usr/share/locale/ja_JP.EUC directory, |
|
494 this does not necessarily mean you can display Japanese text; you also |
|
495 need JIS encoded fonts (or Unicode fonts), and the |
|
496 \c /usr/share/locale/ja_JP.EUC directory needs to be complete. For |
|
497 best results, use complete locales from your system vendor. |
|
498 \endlist |
|
499 |
|
500 \section2 Windows |
|
501 |
|
502 \list |
|
503 \o Qt provides full Unicode support, including input methods, fonts, |
|
504 clipboard, drag-and-drop and file names. |
|
505 \o File I/O defaults to Latin1, with Unicode options in QTextStream. |
|
506 Note that some Windows programs do not understand big-endian |
|
507 Unicode text files even though that is the order prescribed by |
|
508 the Unicode Standard in the absence of higher-level protocols. |
|
509 \o Unlike programs written with MFC or plain winlib, Qt programs |
|
510 are portable between Windows 98 and Windows NT. |
|
511 \e {You do not need different binaries to support Unicode.} |
|
512 \endlist |
|
513 |
|
514 \section2 Mac OS X |
|
515 |
|
516 For details on Mac-specific translation, refer to the Qt/Mac Specific Issues |
|
517 document \l{Qt for Mac OS X - Specific Issues#Translating the Application Menu and Native Dialogs}{here}. |
|
518 */ |