1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \group i18n
|
---|
30 | \title Qt Classes for Internationalization
|
---|
31 |
|
---|
32 | See \l{Internationalization with Qt} for information on how to use these classes
|
---|
33 | in your applications.
|
---|
34 | */
|
---|
35 |
|
---|
36 | /*!
|
---|
37 | \page internationalization.html
|
---|
38 | \title Internationalization with Qt
|
---|
39 | \brief Information about Qt's support for internationalization and multiple languages.
|
---|
40 | \nextpage Writing Source Code for Translation
|
---|
41 |
|
---|
42 | \ingroup qt-basic-concepts
|
---|
43 |
|
---|
44 | \keyword internationalization
|
---|
45 | \keyword i18n
|
---|
46 |
|
---|
47 | The internationalization of an application is the process of making
|
---|
48 | the application usable by people in countries other than one's own.
|
---|
49 |
|
---|
50 | \tableofcontents
|
---|
51 |
|
---|
52 | \section1 Relevant Qt Classes and APIs
|
---|
53 |
|
---|
54 | These classes support internationalizing of Qt applications.
|
---|
55 |
|
---|
56 | \annotatedlist i18n
|
---|
57 |
|
---|
58 | \section1 Languages and Writing Systems
|
---|
59 |
|
---|
60 | In some cases internationalization is simple, for example, making a US
|
---|
61 | application accessible to Australian or British users may require
|
---|
62 | little more than a few spelling corrections. But to make a US
|
---|
63 | application usable by Japanese users, or a Korean application usable
|
---|
64 | by German users, will require that the software operate not only in
|
---|
65 | different languages, but use different input techniques, character
|
---|
66 | encodings and presentation conventions.
|
---|
67 |
|
---|
68 | Qt tries to make internationalization as painless as possible for
|
---|
69 | developers. All input widgets and text drawing methods in Qt offer
|
---|
70 | built-in support for all supported languages. The built-in font engine
|
---|
71 | is capable of correctly and attractively rendering text that contains
|
---|
72 | characters from a variety of different writing systems at the same
|
---|
73 | time.
|
---|
74 |
|
---|
75 | Qt supports most languages in use today, in particular:
|
---|
76 | \list
|
---|
77 | \o All East Asian languages (Chinese, Japanese and Korean)
|
---|
78 | \o All Western languages (using Latin script)
|
---|
79 | \o Arabic
|
---|
80 | \o Cyrillic languages (Russian, Ukrainian, etc.)
|
---|
81 | \o Greek
|
---|
82 | \o Hebrew
|
---|
83 | \o Thai and Lao
|
---|
84 | \o All scripts in Unicode 5.1 that do not require special processing
|
---|
85 | \endlist
|
---|
86 |
|
---|
87 | On Windows, Unix/X11 with FontConfig (client side font support)
|
---|
88 | and Qt for Embedded Linux the following languages are also supported:
|
---|
89 | \list
|
---|
90 | \o Bengali
|
---|
91 | \o Devanagari
|
---|
92 | \o Dhivehi (Thaana)
|
---|
93 | \o Gujarati
|
---|
94 | \o Gurmukhi
|
---|
95 | \o Kannada
|
---|
96 | \o Khmer
|
---|
97 | \o Malayalam
|
---|
98 | \o Myanmar
|
---|
99 | \o Syriac
|
---|
100 | \o Tamil
|
---|
101 | \o Telugu
|
---|
102 | \o Tibetan
|
---|
103 | \o N'Ko
|
---|
104 | \endlist
|
---|
105 |
|
---|
106 | Many of these writing systems exhibit special features:
|
---|
107 |
|
---|
108 | \list
|
---|
109 |
|
---|
110 | \o \bold{Special line breaking behavior.} Some of the Asian languages are
|
---|
111 | written without spaces between words. Line breaking can occur either
|
---|
112 | after every character (with exceptions) as in Chinese, Japanese and
|
---|
113 | Korean, or after logical word boundaries as in Thai.
|
---|
114 |
|
---|
115 | \o \bold{Bidirectional writing.} Arabic and Hebrew are written from right to
|
---|
116 | left, except for numbers and embedded English text which is written
|
---|
117 | left to right. The exact behavior is defined in the
|
---|
118 | \l{http://www.unicode.org/unicode/reports/tr9/}{Unicode Technical Annex #9}.
|
---|
119 |
|
---|
120 | \o \bold{Non-spacing or diacritical marks (accents or umlauts in European
|
---|
121 | languages).} Some languages such as Vietnamese make extensive use of
|
---|
122 | these marks and some characters can have more than one mark at the
|
---|
123 | same time to clarify pronunciation.
|
---|
124 |
|
---|
125 | \o \bold{Ligatures.} In special contexts, some pairs of characters get
|
---|
126 | replaced by a combined glyph forming a ligature. Common examples are
|
---|
127 | the fl and fi ligatures used in typesetting US and European books.
|
---|
128 |
|
---|
129 | \endlist
|
---|
130 |
|
---|
131 | Qt tries to take care of all the special features listed above. You
|
---|
132 | usually don't have to worry about these features so long as you use
|
---|
133 | Qt's input widgets (e.g. QLineEdit, QTextEdit, and derived classes)
|
---|
134 | and Qt's display widgets (e.g. QLabel).
|
---|
135 |
|
---|
136 | Support for these writing systems is transparent to the
|
---|
137 | programmer and completely encapsulated in \l{rich text
|
---|
138 | processing}{Qt's text engine}. This means that you don't need to
|
---|
139 | have any knowledge about the writing system used in a particular
|
---|
140 | language, except for the following small points:
|
---|
141 |
|
---|
142 | \list
|
---|
143 |
|
---|
144 | \o QPainter::drawText(int x, int y, const QString &str) will always
|
---|
145 | draw the string with its left edge at the position specified with
|
---|
146 | the x, y parameters. This will usually give you left aligned strings.
|
---|
147 | Arabic and Hebrew application strings are usually right
|
---|
148 | aligned, so for these languages use the version of drawText() that
|
---|
149 | takes a QRect since this will align in accordance with the language.
|
---|
150 |
|
---|
151 | \o When you write your own text input controls, use QTextLayout.
|
---|
152 | In some languages (e.g. Arabic or languages from the Indian
|
---|
153 | subcontinent), the width and shape of a glyph changes depending on the
|
---|
154 | surrounding characters, which QTextLayout takes into account.
|
---|
155 | Writing input controls usually requires a certain knowledge of the
|
---|
156 | scripts it is going to be used in. Usually the easiest way is to
|
---|
157 | subclass QLineEdit or QTextEdit.
|
---|
158 |
|
---|
159 | \endlist
|
---|
160 |
|
---|
161 | The following sections give some information on the status of the
|
---|
162 | internationalization (i18n) support in Qt. See also the \l{Qt
|
---|
163 | Linguist manual}.
|
---|
164 |
|
---|
165 | \section1 Step by Step
|
---|
166 |
|
---|
167 | Writing cross-platform international software with Qt is a gentle,
|
---|
168 | incremental process. Your software can become internationalized in
|
---|
169 | the following stages:
|
---|
170 |
|
---|
171 | \section2 Use QString for All User-Visible Text
|
---|
172 |
|
---|
173 | Since QString uses the Unicode 5.1 encoding internally, every
|
---|
174 | language in the world can be processed transparently using
|
---|
175 | familiar text processing operations. Also, since all Qt functions
|
---|
176 | that present text to the user take a QString as a parameter,
|
---|
177 | there is no \c{char *} to QString conversion overhead.
|
---|
178 |
|
---|
179 | Strings that are in "programmer space" (such as QObject names
|
---|
180 | and file format texts) need not use QString; the traditional
|
---|
181 | \c{char *} or the QByteArray class will suffice.
|
---|
182 |
|
---|
183 | You're unlikely to notice that you are using Unicode;
|
---|
184 | QString, and QChar are just like easier versions of the crude
|
---|
185 | \c{const char *} and char from traditional C.
|
---|
186 |
|
---|
187 | \section2 Use tr() for All Literal Text
|
---|
188 |
|
---|
189 | Wherever your program uses "quoted text" for text that will
|
---|
190 | be presented to the user, ensure that it is processed by the \l
|
---|
191 | QCoreApplication::translate() function. Essentially all that is necessary
|
---|
192 | to achieve this is to use QObject::tr(). For example, assuming the
|
---|
193 | \c LoginWidget is a subclass of QWidget:
|
---|
194 |
|
---|
195 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 0
|
---|
196 |
|
---|
197 | This accounts for 99% of the user-visible strings you're likely to
|
---|
198 | write.
|
---|
199 |
|
---|
200 | If the quoted text is not in a member function of a
|
---|
201 | QObject subclass, use either the tr() function of an
|
---|
202 | appropriate class, or the QCoreApplication::translate() function
|
---|
203 | directly:
|
---|
204 |
|
---|
205 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 1
|
---|
206 |
|
---|
207 | If you need to have translatable text completely
|
---|
208 | outside a function, there are two macros to help: QT_TR_NOOP()
|
---|
209 | and QT_TRANSLATE_NOOP(). They merely mark the text for
|
---|
210 | extraction by the \c lupdate utility described below.
|
---|
211 | The macros expand to just the text (without the context).
|
---|
212 |
|
---|
213 | Example of QT_TR_NOOP():
|
---|
214 |
|
---|
215 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 2
|
---|
216 |
|
---|
217 | Example of QT_TRANSLATE_NOOP():
|
---|
218 |
|
---|
219 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 3
|
---|
220 |
|
---|
221 | If you disable the \c{const char *} to QString automatic
|
---|
222 | conversion by compiling your software with the macro \c
|
---|
223 | QT_NO_CAST_FROM_ASCII defined, you'll be very likely to catch any
|
---|
224 | strings you are missing. See QString::fromLatin1() for more
|
---|
225 | information. Disabling the conversion can make programming a bit
|
---|
226 | cumbersome.
|
---|
227 |
|
---|
228 | If your source language uses characters outside Latin1, you
|
---|
229 | might find QObject::trUtf8() more convenient than
|
---|
230 | QObject::tr(), as tr() depends on the
|
---|
231 | QTextCodec::codecForTr(), which makes it more fragile than
|
---|
232 | QObject::trUtf8().
|
---|
233 |
|
---|
234 | \section2 Use QKeySequence() for Accelerator Values
|
---|
235 |
|
---|
236 | Accelerator values such as Ctrl+Q or Alt+F need to be translated
|
---|
237 | too. If you hardcode Qt::CTRL + Qt::Key_Q for "quit" in your
|
---|
238 | application, translators won't be able to override it. The
|
---|
239 | correct idiom is
|
---|
240 |
|
---|
241 | \snippet examples/mainwindows/application/mainwindow.cpp 20
|
---|
242 |
|
---|
243 | \section2 Use QString::arg() for Dynamic Text
|
---|
244 |
|
---|
245 | The QString::arg() functions offer a simple means for substituting
|
---|
246 | arguments:
|
---|
247 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 4
|
---|
248 |
|
---|
249 | In some languages the order of arguments may need to change, and this
|
---|
250 | can easily be achieved by changing the order of the % arguments. For
|
---|
251 | example:
|
---|
252 |
|
---|
253 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 5
|
---|
254 |
|
---|
255 | produces the correct output in English and Norwegian:
|
---|
256 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 6
|
---|
257 |
|
---|
258 | \section2 Produce Translations
|
---|
259 |
|
---|
260 | Once you are using tr() throughout an application, you can start
|
---|
261 | producing translations of the user-visible text in your program.
|
---|
262 |
|
---|
263 | The \l{Qt Linguist manual} provides further information about
|
---|
264 | Qt's translation tools, \e{Qt Linguist}, \c lupdate and \c
|
---|
265 | lrelease.
|
---|
266 |
|
---|
267 | Translation of a Qt application is a three-step process:
|
---|
268 |
|
---|
269 | \list 1
|
---|
270 |
|
---|
271 | \o Run \c lupdate to extract translatable text from the C++
|
---|
272 | source code of the Qt application, resulting in a message file
|
---|
273 | for translators (a TS file). The utility recognizes the tr()
|
---|
274 | construct and the \c{QT_TR*_NOOP()} macros described above and
|
---|
275 | produces TS files (usually one per language).
|
---|
276 |
|
---|
277 | \o Provide translations for the source texts in the TS file, using
|
---|
278 | \e{Qt Linguist}. Since TS files are in XML format, you can also
|
---|
279 | edit them by hand.
|
---|
280 |
|
---|
281 | \o Run \c lrelease to obtain a light-weight message file (a QM
|
---|
282 | file) from the TS file, suitable only for end use. Think of the TS
|
---|
283 | files as "source files", and QM files as "object files". The
|
---|
284 | translator edits the TS files, but the users of your application
|
---|
285 | only need the QM files. Both kinds of files are platform and
|
---|
286 | locale independent.
|
---|
287 |
|
---|
288 | \endlist
|
---|
289 |
|
---|
290 | Typically, you will repeat these steps for every release of your
|
---|
291 | application. The \c lupdate utility does its best to reuse the
|
---|
292 | translations from previous releases.
|
---|
293 |
|
---|
294 | Before you run \c lupdate, you should prepare a project file. Here's
|
---|
295 | an example project file (\c .pro file):
|
---|
296 |
|
---|
297 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 7
|
---|
298 |
|
---|
299 | When you run \c lupdate or \c lrelease, you must give the name of the
|
---|
300 | project file as a command-line argument.
|
---|
301 |
|
---|
302 | In this example, four exotic languages are supported: Danish,
|
---|
303 | Finnish, Norwegian and Swedish. If you use \l{qmake}, you usually
|
---|
304 | don't need an extra project file for \c lupdate; your \c qmake
|
---|
305 | project file will work fine once you add the \c TRANSLATIONS
|
---|
306 | entry.
|
---|
307 |
|
---|
308 | In your application, you must \l QTranslator::load() the translation
|
---|
309 | files appropriate for the user's language, and install them using \l
|
---|
310 | QCoreApplication::installTranslator().
|
---|
311 |
|
---|
312 | \c linguist, \c lupdate and \c lrelease are installed in the \c bin
|
---|
313 | subdirectory of the base directory Qt is installed into. Click Help|Manual
|
---|
314 | in \e{Qt Linguist} to access the user's manual; it contains a tutorial
|
---|
315 | to get you started.
|
---|
316 |
|
---|
317 | \target qt-itself
|
---|
318 | Qt itself contains over 400 strings that will also need to be
|
---|
319 | translated into the languages that you are targeting. You will find
|
---|
320 | translation files for French, German and Simplified Chinese in
|
---|
321 | \c{$QTDIR/translations}, as well as a template for translating to
|
---|
322 | other languages. (This directory also contains some additional
|
---|
323 | unsupported translations which may be useful.)
|
---|
324 |
|
---|
325 | Typically, your application's \c main() function will look like
|
---|
326 | this:
|
---|
327 |
|
---|
328 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 8
|
---|
329 |
|
---|
330 | Note the use of QLibraryInfo::location() to locate the Qt translations.
|
---|
331 | Developers should request the path to the translations at run-time by
|
---|
332 | passing QLibraryInfo::TranslationsPath to this function instead of
|
---|
333 | using the \c QTDIR environment variable in their applications.
|
---|
334 |
|
---|
335 | \section2 Support for Encodings
|
---|
336 |
|
---|
337 | The QTextCodec class and the facilities in QTextStream make it easy to
|
---|
338 | support many input and output encodings for your users' data. When an
|
---|
339 | application starts, the locale of the machine will determine the 8-bit
|
---|
340 | encoding used when dealing with 8-bit data: such as for font
|
---|
341 | selection, text display, 8-bit text I/O, and character input.
|
---|
342 |
|
---|
343 | The application may occasionally require encodings other than the
|
---|
344 | default local 8-bit encoding. For example, an application in a
|
---|
345 | Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might
|
---|
346 | need to output Cyrillic in the ISO 8859-5 encoding. Code for this
|
---|
347 | would be:
|
---|
348 |
|
---|
349 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 9
|
---|
350 |
|
---|
351 | For converting Unicode to local 8-bit encodings, a shortcut is
|
---|
352 | available: the QString::toLocal8Bit() function returns such 8-bit
|
---|
353 | data. Another useful shortcut is QString::toUtf8(), which returns
|
---|
|
---|