| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:FDL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
|---|
| 14 | ** written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Free Documentation License
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
|---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
|---|
| 20 | ** file.
|
|---|
| 21 | **
|
|---|
| 22 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 23 | ** Nokia at qt-info@nokia.com.
|
|---|
| 24 | ** $QT_END_LICENSE$
|
|---|
| 25 | **
|
|---|
| 26 | ****************************************************************************/
|
|---|
| 27 |
|
|---|
| 28 | /*!
|
|---|
| 29 | \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
|
|---|
| 354 | text in the 8-bit UTF-8 encoding: this perfectly preserves
|
|---|
| 355 | Unicode information while looking like plain ASCII if the text is
|
|---|
| 356 | wholly ASCII.
|
|---|
| 357 |
|
|---|
| 358 | For converting the other way, there are the QString::fromUtf8() and
|
|---|
| 359 | QString::fromLocal8Bit() convenience functions, or the general code,
|
|---|
| 360 | demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode
|
|---|
| 361 | conversion:
|
|---|
| 362 |
|
|---|
| 363 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 10
|
|---|
| 364 |
|
|---|
| 365 | Ideally Unicode I/O should be used as this maximizes the portability
|
|---|
| 366 | of documents between users around the world, but in reality it is
|
|---|
| 367 | useful to support all the appropriate encodings that your users will
|
|---|
| 368 | need to process existing documents. In general, Unicode (UTF-16 or
|
|---|
| 369 | UTF-8) is best for information transferred between arbitrary people,
|
|---|
| 370 | while within a language or national group, a local standard is often
|
|---|
| 371 | more appropriate. The most important encoding to support is the one
|
|---|
| 372 | returned by QTextCodec::codecForLocale(), as this is the one the user
|
|---|
| 373 | is most likely to need for communicating with other people and
|
|---|
| 374 | applications (this is the codec used by local8Bit()).
|
|---|
| 375 |
|
|---|
| 376 | Qt supports most of the more frequently used encodings natively. For a
|
|---|
| 377 | complete list of supported encodings see the \l QTextCodec
|
|---|
| 378 | documentation.
|
|---|
| 379 |
|
|---|
| 380 | In some cases and for less frequently used encodings it may be
|
|---|
| 381 | necessary to write your own QTextCodec subclass. Depending on the
|
|---|
| 382 | urgency, it may be useful to contact Qt's technical support team or
|
|---|
| 383 | ask on the \c qt-interest mailing list to see if someone else is
|
|---|
| 384 | already working on supporting the encoding.
|
|---|
| 385 |
|
|---|
| 386 | \keyword localization
|
|---|
| 387 |
|
|---|
| 388 | \section2 Localize
|
|---|
| 389 |
|
|---|
| 390 | Localization is the process of adapting to local conventions, for
|
|---|
| 391 | example presenting dates and times using the locally preferred
|
|---|
| 392 | formats. Such localizations can be accomplished using appropriate tr()
|
|---|
| 393 | strings.
|
|---|
| 394 |
|
|---|
| 395 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 11
|
|---|
| 396 |
|
|---|
| 397 | In the example, for the US we would leave the translation of
|
|---|
| 398 | "AMPM" as it is and thereby use the 12-hour clock branch; but in
|
|---|
| 399 | Europe we would translate it as something else and this will make
|
|---|
| 400 | the code use the 24-hour clock branch.
|
|---|
| 401 |
|
|---|
| 402 | For localized numbers use the QLocale class.
|
|---|
| 403 |
|
|---|
| 404 | Localizing images is not recommended. Choose clear icons that are
|
|---|
| 405 | appropriate for all localities, rather than relying on local puns or
|
|---|
| 406 | stretched metaphors. The exception is for images of left and right
|
|---|
| 407 | pointing arrows which may need to be reversed for Arabic and Hebrew
|
|---|
| 408 | locales.
|
|---|
| 409 |
|
|---|
| 410 | \section1 Dynamic Translation
|
|---|
| 411 |
|
|---|
| 412 | Some applications, such as Qt Linguist, must be able to support changes
|
|---|
| 413 | to the user's language settings while they are still running. To make
|
|---|
| 414 | widgets aware of changes to the installed QTranslators, reimplement the
|
|---|
| 415 | widget's \l{QWidget::changeEvent()}{changeEvent()} function to check whether
|
|---|
| 416 | the event is a \l{QEvent::LanguageChange}{LanguageChange} event, and update
|
|---|
| 417 | the text displayed by widgets using the \l{QObject::tr()}{tr()} function
|
|---|
| 418 | in the usual way. For example:
|
|---|
| 419 |
|
|---|
| 420 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 12
|
|---|
| 421 |
|
|---|
| 422 | All other change events should be passed on by calling the default
|
|---|
| 423 | implementation of the function.
|
|---|
| 424 |
|
|---|
| 425 | The list of installed translators might change in reaction to a
|
|---|
| 426 | \l{QEvent::LocaleChange}{LocaleChange} event, or the application might
|
|---|
| 427 | provide a user interface that allows the user to change the current
|
|---|
| 428 | application language.
|
|---|
| 429 |
|
|---|
| 430 | The default event handler for QWidget subclasses responds to the
|
|---|
| 431 | QEvent::LanguageChange event, and will call this function when necessary.
|
|---|
| 432 |
|
|---|
| 433 | \l{QEvent::LanguageChange}{LanguageChange} events are posted when a new
|
|---|
| 434 | translation is installed using the QCoreApplication::installTranslator()
|
|---|
| 435 | function. Additionally, other application components can also force
|
|---|
| 436 | widgets to update themselves by posting LanguageChange events to them.
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 | \section1 Translating Non-Qt Classes
|
|---|
| 440 |
|
|---|
| 441 | It is sometimes necessary to provide internationalization support for
|
|---|
| 442 | strings used in classes that do not inherit QObject or use the Q_OBJECT
|
|---|
| 443 | macro to enable translation features. Since Qt translates strings at
|
|---|
| 444 | run-time based on the class they are associated with and \c lupdate
|
|---|
| 445 | looks for translatable strings in the source code, non-Qt classes must
|
|---|
| 446 | use mechanisms that also provide this information.
|
|---|
| 447 |
|
|---|
| 448 | One way to do this is to add translation support to a non-Qt class
|
|---|
| 449 | using the Q_DECLARE_TR_FUNCTIONS() macro; for example:
|
|---|
| 450 |
|
|---|
| 451 | \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 0
|
|---|
| 452 | \dots
|
|---|
| 453 | \snippet doc/src/snippets/i18n-non-qt-class/myclass.h 1
|
|---|
| 454 |
|
|---|
| 455 | This provides the class with \l{QObject::}{tr()} functions that can
|
|---|
| 456 | be used to translate strings associated with the class, and makes it
|
|---|
| 457 | possible for \c lupdate to find translatable strings in the source
|
|---|
| 458 | code.
|
|---|
| 459 |
|
|---|
| 460 | Alternatively, the QCoreApplication::translate() function can be called
|
|---|
| 461 | with a specific context, and this will be recognized by \c lupdate and
|
|---|
| 462 | Qt Linguist.
|
|---|
| 463 |
|
|---|
| 464 | \section1 System Support
|
|---|
| 465 |
|
|---|
| 466 | Some of the operating systems and windowing systems that Qt runs on
|
|---|
| 467 | only have limited support for Unicode. The level of support available
|
|---|
| 468 | in the underlying system has some influence on the support that Qt can
|
|---|
| 469 | provide on those platforms, although in general Qt applications need
|
|---|
| 470 | not be too concerned with platform-specific limitations.
|
|---|
| 471 |
|
|---|
| 472 | \section2 Unix/X11
|
|---|
| 473 |
|
|---|
| 474 | \list
|
|---|
| 475 | \o Locale-oriented fonts and input methods. Qt hides these and
|
|---|
| 476 | provides Unicode input and output.
|
|---|
| 477 | \o Filesystem conventions such as
|
|---|
| 478 | \l{http://www.ietf.org/rfc/rfc2279.txt}{UTF-8}
|
|---|
| 479 | are under development in some Unix variants. All Qt file
|
|---|
| 480 | functions allow Unicode, but convert filenames to the local
|
|---|
| 481 | 8-bit encoding, as this is the Unix convention (see
|
|---|
| 482 | QFile::setEncodingFunction() to explore alternative
|
|---|
| 483 | encodings).
|
|---|
| 484 | \o File I/O defaults to the local 8-bit encoding,
|
|---|
| 485 | with Unicode options in QTextStream.
|
|---|
| 486 | \o Many Unix distributions contain only partial support for some locales.
|
|---|
| 487 | For example, if you have a \c /usr/share/locale/ja_JP.EUC directory,
|
|---|
| 488 | this does not necessarily mean you can display Japanese text; you also
|
|---|
| 489 | need JIS encoded fonts (or Unicode fonts), and the
|
|---|
| 490 | \c /usr/share/locale/ja_JP.EUC directory needs to be complete. For
|
|---|
| 491 | best results, use complete locales from your system vendor.
|
|---|
| 492 | \endlist
|
|---|
| 493 |
|
|---|
| 494 | \section2 Windows
|
|---|
| 495 |
|
|---|
| 496 | \list
|
|---|
| 497 | \o Qt provides full Unicode support, including input methods, fonts,
|
|---|
| 498 | clipboard, drag-and-drop and file names.
|
|---|
| 499 | \o File I/O defaults to Latin1, with Unicode options in QTextStream.
|
|---|
| 500 | Note that some Windows programs do not understand big-endian
|
|---|
| 501 | Unicode text files even though that is the order prescribed by
|
|---|
| 502 | the Unicode Standard in the absence of higher-level protocols.
|
|---|
| 503 | \endlist
|
|---|
| 504 |
|
|---|
| 505 | \section2 Mac OS X
|
|---|
| 506 |
|
|---|
| 507 | For details on Mac-specific translation, refer to the Qt/Mac Specific Issues
|
|---|
| 508 | document \l{Qt for Mac OS X - Specific Issues#Translating the Application Menu and Native Dialogs}{here}.
|
|---|
| 509 | */
|
|---|
| 510 |
|
|---|
| 511 | /*!
|
|---|
| 512 | \page i18n-source-translation.html
|
|---|
| 513 | \title Writing Source Code for Translation
|
|---|
| 514 | \ingroup i18n
|
|---|
| 515 | \previouspage Internationalization with Qt
|
|---|
| 516 | \contentspage Internationalization with Qt
|
|---|
| 517 | \nextpage Translation Rules for Plurals
|
|---|
| 518 | \brief How to write source code in a way that makes it possible for user-visible text to be translated.
|
|---|
| 519 |
|
|---|
| 520 | \tableofcontents
|
|---|
| 521 |
|
|---|
| 522 | \section1 The Basics
|
|---|
| 523 |
|
|---|
| 524 | Developers use the \l{QObject::}{tr()} function to obtain translated text
|
|---|
| 525 | for their classes, typically for display purposes. This function is also
|
|---|
| 526 | used to indicate which text strings in an application are translatable.
|
|---|
| 527 |
|
|---|
| 528 | Qt indexes each translatable string by the \e{translation context} it is
|
|---|
| 529 | associated with; this is generally the name of the QObject subclass it is
|
|---|
| 530 | used in.
|
|---|
| 531 |
|
|---|
| 532 | Translation contexts are defined for new QObject-based classes by the use
|
|---|
| 533 | of the Q_OBJECT macro in each new class definition.
|
|---|
| 534 |
|
|---|
| 535 | When tr() is called, it looks up the translatable string using a QTranslator
|
|---|
| 536 | object. For translation to work, one or more of these must have been
|
|---|
| 537 | installed on the application object in the way described in the
|
|---|
| 538 | \l{#Enabling Translation}{Enabling Translation} section below.
|
|---|
| 539 |
|
|---|
| 540 | \section1 Defining a Translation Context
|
|---|
| 541 |
|
|---|
| 542 | The translation context for QObject and each QObject subclass is the
|
|---|
| 543 | class name itself. Developers subclassing QObject must use the
|
|---|
| 544 | Q_OBJECT macro in their class definition to override the translation
|
|---|
| 545 | context. This macro sets the context to the name of the subclass.
|
|---|
| 546 |
|
|---|
| 547 | For example, the following class definition includes the Q_OBJECT macro,
|
|---|
| 548 | implementing a new tr() that uses the \c MainWindow context:
|
|---|
| 549 |
|
|---|
| 550 | \snippet mainwindows/sdi/mainwindow.h class definition with macro
|
|---|
| 551 | \dots
|
|---|
| 552 |
|
|---|
| 553 | If Q_OBJECT is not used in a class definition, the context will be
|
|---|
| 554 | inherited from the base class. For example, since all QObject-based
|
|---|
| 555 | classes in Qt provide a context, a new QWidget subclass defined without
|
|---|
| 556 | a Q_OBJECT macro will use the \c QWidget context if its tr() function
|
|---|
| 557 | is invoked.
|
|---|
| 558 |
|
|---|
| 559 | \section1 Using tr() to Obtain a Translation
|
|---|
| 560 |
|
|---|
| 561 | The following example shows how a translation is obtained for the
|
|---|
| 562 | class shown in the previous section:
|
|---|
| 563 |
|
|---|
| 564 | \snippet mainwindows/sdi/mainwindow.cpp implicit tr context
|
|---|
| 565 | \dots
|
|---|
| 566 |
|
|---|
| 567 | Here, the translation context is \c MainWindow because it is the
|
|---|
| 568 | \c MainWindow::tr() function that is invoked. The text returned
|
|---|
| 569 | by the tr() function is a translation of "&File" obtained from
|
|---|
| 570 | the \c MainWindow context.
|
|---|
| 571 |
|
|---|
| 572 | When Qt's translation tool, \l lupdate, is used to process a set of source
|
|---|
| 573 | files, the text wrapped in tr() calls is stored in a section of the translation
|
|---|
| 574 | file that corresponds to its translation context.
|
|---|
| 575 |
|
|---|
| 576 | In some situations, it is useful to give a translation context explicitly
|
|---|
| 577 | by fully qualifying the call to tr(); for example:
|
|---|
| 578 |
|
|---|
| 579 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp explicit tr context
|
|---|
| 580 |
|
|---|
| 581 | This call obtains the translated text for "Page up" from the \c QScrollBar
|
|---|
| 582 | context. Developers can also use the QCoreApplication::translate() function
|
|---|
| 583 | to obtain a translation for a particular translation context.
|
|---|
| 584 |
|
|---|
| 585 | \section1 Translator Comments
|
|---|
| 586 |
|
|---|
| 587 | Developers can include information about each translatable string to
|
|---|
| 588 | help translators with the translation process. These are extracted
|
|---|
| 589 | when \l lupdate is used to process the source files. The recommended
|
|---|
| 590 | way to add comments is to annotate the tr() calls in your code with
|
|---|
| 591 | comments of the form:
|
|---|
| 592 |
|
|---|
| 593 | \tt{//: ...}
|
|---|
| 594 |
|
|---|
| 595 | or
|
|---|
| 596 |
|
|---|
| 597 | \tt{\begincomment: ... \endcomment}
|
|---|
| 598 |
|
|---|
| 599 | Examples:
|
|---|
| 600 |
|
|---|
| 601 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 40
|
|---|
| 602 |
|
|---|
| 603 | In these examples, the comments will be associated with the strings
|
|---|
| 604 | passed to tr() in the context of each call.
|
|---|
| 605 |
|
|---|
| 606 | \section1 Adding Meta-Data to Strings
|
|---|
| 607 |
|
|---|
| 608 | Additional data can be attached to each translatable message. These are
|
|---|
| 609 | extracted when \l lupdate is used to process the source files. The
|
|---|
| 610 | recommended way to add meta-data is to annotate the tr() calls in your code
|
|---|
| 611 | with comments of the form:
|
|---|
| 612 |
|
|---|
| 613 | \tt{//= <id>}
|
|---|
| 614 |
|
|---|
| 615 | This can be used to give the message a unique identifier to support tools
|
|---|
| 616 | which need it.
|
|---|
| 617 |
|
|---|
| 618 | An alternative way to attach meta-data is to use the following syntax:
|
|---|
| 619 |
|
|---|
| 620 | \tt{//~ <field name> <field contents>}
|
|---|
| 621 |
|
|---|
| 622 | This can be used to attach meta-data to the message. The field name should
|
|---|
| 623 | consist of a domain prefix (possibly the conventional file extension of the
|
|---|
| 624 | file format the field is inspired by), a hyphen and the actual field name
|
|---|
| 625 | in underscore-delimited notation. For storage in TS files, the field name
|
|---|
| 626 | together with the prefix "extra-" will form an XML element name. The field
|
|---|
| 627 | contents will be XML-escaped, but otherwise appear verbatim as the
|
|---|
| 628 | element's contents. Any number of unique fields can be added to each
|
|---|
| 629 | message.
|
|---|
| 630 |
|
|---|
| 631 | Example:
|
|---|
| 632 |
|
|---|
| 633 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp meta data
|
|---|
| 634 |
|
|---|
| 635 | Meta-data appearing right in front of a magic TRANSLATOR comment applies to
|
|---|
| 636 | the whole TS file.
|
|---|
| 637 |
|
|---|
| 638 | \section1 Disambiguation
|
|---|
| 639 |
|
|---|
| 640 | If the same translatable string is used in different roles within the same
|
|---|
| 641 | translation context, an additional identifying string may be passed in
|
|---|
| 642 | the call to \l{QObject::}{tr()}. This optional disambiguation argument
|
|---|
| 643 | is used to distinguish between otherwise identical strings.
|
|---|
| 644 |
|
|---|
| 645 | Example:
|
|---|
| 646 |
|
|---|
| 647 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 17
|
|---|
| 648 | \dots
|
|---|
| 649 |
|
|---|
| 650 | In Qt 4.4 and earlier, this disambiguation parameter was the preferred
|
|---|
| 651 | way to specify comments to translators.
|
|---|
| 652 |
|
|---|
| 653 | \section1 Character Encodings
|
|---|
| 654 |
|
|---|
| 655 | You can set the encoding for the source text by calling QTextCodec::setCodecForTr().
|
|---|
| 656 | By default, the source text is assumed to be in Latin-1 encoding.
|
|---|
| 657 |
|
|---|
| 658 | \section1 Handling Plurals
|
|---|
| 659 |
|
|---|
| 660 | Some translatable strings contain placeholders for integer values and need
|
|---|
| 661 | to be translated differently depending on the values in use.
|
|---|
| 662 |
|
|---|
| 663 | To help with this problem, developers pass an additional integer argument
|
|---|
| 664 | to the \l{QObject::}{tr()} function, and typically use a special notation
|
|---|
| 665 | for plurals in each translatable string.
|
|---|
| 666 |
|
|---|
| 667 | If this argument is equal or greater than zero, all occurrences of
|
|---|
| 668 | \c %n in the resulting string are replaced with a decimal representation
|
|---|
| 669 | of the value supplied. In addition, the translation used will adapt to the
|
|---|
| 670 | value according to the rules for each language.
|
|---|
| 671 |
|
|---|
| 672 | Example:
|
|---|
| 673 |
|
|---|
| 674 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 18
|
|---|
| 675 |
|
|---|
| 676 | The table below shows what string is returned depending on the
|
|---|
| 677 | active translation:
|
|---|
| 678 |
|
|---|
| 679 | \table
|
|---|
| 680 | \header \o \o{3,1} Active Translation
|
|---|
| 681 | \header \o \a n \o No Translation \o French \o English
|
|---|
| 682 | \row \o 0 \o "0 message(s) saved" \o "0 message sauvegard\unicode{0xE9}" \o "0 message\bold{s} saved"
|
|---|
| 683 | \row \o 1 \o "1 message(s) saved" \o "1 message sauvegard\unicode{0xE9}" \o "1 message saved"
|
|---|
| 684 | \row \o 2 \o "2 message(s) saved" \o "2 message\bold{s} sauvegard\unicode{0xE9}\bold{s}" \o "2 message\bold{s} saved"
|
|---|
| 685 | \row \o 37 \o "37 message(s) saved" \o "37 message\bold{s} sauvegard\unicode{0xE9}\bold{s}" \o "37 message\bold{s} saved"
|
|---|
| 686 | \endtable
|
|---|
| 687 |
|
|---|
| 688 | This idiom is more flexible than the traditional approach; e.g.,
|
|---|
| 689 |
|
|---|
| 690 | \snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 19
|
|---|
| 691 |
|
|---|
| 692 | because it also works with target languages that have several
|
|---|
| 693 | plural forms (e.g., Irish has a special "dual" form that should
|
|---|
| 694 | be used when \c n is 2), and it handles the \e n == 0 case
|
|---|
| 695 | correctly for languages such as French that require the singular.
|
|---|
| 696 | See the \l{Qt Linguist Manual} for details.
|
|---|
| 697 |
|
|---|
| 698 | Instead of \c %n, you can use \c %Ln to produce a localized
|
|---|
| 699 | representation of \a n. The conversion uses the default locale,
|
|---|
| 700 | set using QLocale::setDefault(). (If no default locale was
|
|---|
| 701 | specified, the "C" locale is used.)
|
|---|
| 702 |
|
|---|
| 703 | A summary of the rules used to translate strings containing plurals can be
|
|---|
| 704 | found in the \l{Translation Rules for Plurals} document.
|
|---|
| 705 |
|
|---|
| 706 | \section1 Enabling Translation
|
|---|
| 707 |
|
|---|
| 708 | Typically, your application's \c main() function will look like
|
|---|
| 709 | this:
|
|---|
| 710 |
|
|---|
| 711 | \snippet doc/src/snippets/code/doc_src_i18n.qdoc 8
|
|---|
| 712 |
|
|---|
| 713 | Note the use of QLibraryInfo::location() to locate the Qt translations.
|
|---|
| 714 | Developers should request the path to the translations at run-time by
|
|---|
| 715 | passing QLibraryInfo::TranslationsPath to this function instead of
|
|---|
| 716 | using the \c QTDIR environment variable in their applications.
|
|---|
| 717 |
|
|---|
| 718 | \section1 Further Reading
|
|---|
| 719 |
|
|---|
| 720 | \l{Qt Linguist Manual}, \l{Hello tr() Example}, \l{Translation Rules for Plurals}
|
|---|
| 721 | */
|
|---|
| 722 |
|
|---|
| 723 | /*!
|
|---|
| 724 | \page i18n-plural-rules.html
|
|---|
| 725 | \title Translation Rules for Plurals
|
|---|
| 726 | \ingroup i18n
|
|---|
| 727 | \previouspage Writing Source Code for Translation
|
|---|
| 728 | \contentspage Internationalization with Qt
|
|---|
| 729 | \brief A summary of the translation rules for plurals produced by Qt's i18n tools.
|
|---|
| 730 |
|
|---|
| 731 | The table below shows the specific rules that are produced by Qt Linguist
|
|---|
| 732 | and \c lrelease for a selection of languages. Cells marked \e otherwise
|
|---|
| 733 | indicate the form used when none of the other rules are appropriate for a
|
|---|
| 734 | specific language.
|
|---|
| 735 |
|
|---|
| 736 | \table 80%
|
|---|
| 737 | \header \o Language \o Rule 1 \o Rule 2 \o Rule 3
|
|---|
| 738 | \row \o English \o \c{n == 1}
|
|---|
| 739 | \o \e{otherwise} \o N/A
|
|---|
| 740 | \row \o French \o \c{n < 2}
|
|---|
| 741 | \o \e{otherwise} \o N/A
|
|---|
| 742 | \row \o Czech \o \c{n % 100 == 1}
|
|---|
| 743 | \o \c{n % 100 >= 2 && n % 100 <= 4}
|
|---|
| 744 | \o \e{otherwise}
|
|---|
| 745 | \row \o Irish \o \c{n == 1}
|
|---|
| 746 | \o \c{n == 2} \o \e{otherwise}
|
|---|
| 747 | \row \o Latvian \o \c{n % 10 == 1&& n % 100 != 11}
|
|---|
| 748 | \o \c{n != 0} \o \e{otherwise}
|
|---|
| 749 | \row \o Lithuanian \o \c{n % 10 == 1&& n % 100 != 11}
|
|---|
| 750 | \o \c{n % 100 != 12 && n % 10 == 2}
|
|---|
| 751 | \o \e{otherwise}
|
|---|
| 752 | \row \o Macedonian \o \c{n % 10 == 1}
|
|---|
| 753 | \o \c{n % 10 == 2} \o \e{otherwise}
|
|---|
| 754 | \row \o Polish \o \c{n == 1}
|
|---|
| 755 | \o \c{n % 10 >= 2 && n % 10 <= 4
|
|---|
| 756 | && (n % 100 < 10 || n % 100 > 20)}
|
|---|
| 757 | \o \e{otherwise}
|
|---|
| 758 | \row \o Romanian \o \c{n == 1}
|
|---|
| 759 | \o \c{n == 0|| (n % 100 >= 1 && n % 100 <= 20)}
|
|---|
| 760 | \o \e{otherwise}
|
|---|
| 761 | \row \o Russian \o \c{n % 10 == 1&& n % 100 != 11}
|
|---|
| 762 | \o \c{n % 10 >= 2 && n % 10 <= 4
|
|---|
| 763 | && (n % 100 < 10 || n % 100 > 20)}
|
|---|
| 764 | \o \e{otherwise}
|
|---|
| 765 | \row \o Slovak \o \c{n == 1} \o \c{n >= 2 && n <= 4}
|
|---|
| 766 | \o \e{otherwise}
|
|---|
| 767 | \row \o Japanese \o \e{otherwise} \o N/A \o N/A
|
|---|
| 768 | \endtable
|
|---|
| 769 |
|
|---|
| 770 | The rules themselves are not documented and are internal to Qt Linguist and \c lrelease.
|
|---|
| 771 | */
|
|---|