<tdalign="right"valign="center"><imgsrc="logo32.png"align="right"width="64"height="32"border="0"></td></tr></table><h1align=center>Internationalization with Qt</h1>
<p><!-- index internationalization --><aname="internationalization"></a><!-- index i18n --><aname="i18n"></a>
<p> The internationalization of an application is the process of making
the application usable by people in countries other than one's own.
<p><!-- toc -->
<ul>
<li><ahref="#1"> Step by Step
</a>
<ul>
<li><ahref="#1-1"> Use QString for all User-visible Text
</a>
<li><ahref="#1-2"> Use tr() for all Literal Text
</a>
<li><ahref="#1-3"> Use QKeySequence() for Accelerator Values
</a>
<li><ahref="#1-4"> Use QString::arg() for Dynamic Text
</a>
<li><ahref="#1-5"> Produce Translations
</a>
<li><ahref="#1-6"> Support for Encodings
</a>
<li><ahref="#1-7"> Localize
</a>
</ul>
<li><ahref="#2"> Dynamic Translation
</a>
<li><ahref="#3"> System Support
</a>
<ul>
<li><ahref="#3-1"> Unix/X11
</a>
<li><ahref="#3-2"> Windows
</a>
</ul>
<li><ahref="#4"> Note about Locales on X11
</a>
<li><ahref="#5"> Relevant Qt Classes
</a>
</ul>
<!-- endtoc -->
<p> In some cases internationalization is simple, for example, making a US
application accessible to Australian or British users may require
little more than a few spelling corrections. But to make a US
application usable by Japanese users, or a Korean application usable
by German users, will require that the software operate not only in
different languages, but use different input techniques, character
encodings and presentation conventions.
<p> Qt tries to make internationalization as painless as possible for
developers. All input widgets and text drawing methods in Qt offer
built-in support for all supported languages. The built-in font engine
is capable of correctly and attractively rendering text that contains
characters from a variety of different writing systems at the same
time.
<p> Qt supports most languages in use today, in particular:
<ul>
<li> All East Asian languages (Chinese, Japanese and Korean)
<li> All Western languages (using Latin script)
<li> Arabic
<li> Cyrillic languages (Russian)
<li> Greek
<li> Hebrew
<li> Thai and Lao
<li> All scripts in Unicode 3.2 that do not require special processing
</ul>
<p> On Windows NT/2000/XP and Unix/X11 with Xft (client side font support)
the following languages are also supported:
<ul>
<li> Bengali
<li> Devanagari
<li> Dhivehi (Thaana)
<li> Gujarati
<li> Gurmukhi
<li> Kannada
<li> Khmer
<li> Malayalam (X11 only)
<li> Myanmar (X11 only)
<li> Syriac
<li> Tamil
<li> Telugu
<li> Tibetan (X11 only)
</ul>
<p> Many of these writing systems exhibit special features:
<p><ul>
<p><li><b>Special line breaking behavior.</b> Some of the Asian languages are
written without spaces between words. Line breaking can occur either
after every character (with exceptions) as in Chinese, Japanese and
Korean, or after logical word boundaries as in Thai.
<p><li><b>Bidirectional writing.</b> Arabic and Hebrew are written from right to
left, except for numbers and embedded English text which is written
left to right. The exact behavior is defined in the <ahref="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report
#9</a>.
<p><li><b>Non spacing or diacritical marks</b> (accents or umlauts in European
languages). Some languages such as Vietnamese make extensive use of
these marks and some characters can have more than one mark at the
same time to clarify pronunciation.
<p><li><b>Ligatures.</b> In special contexts, some pairs of characters get
replaced by a combined glyph forming a ligature. Common examples are
the fl and fi ligatures used in typesetting US and European books.
<p></ul>
<p> Qt tries to take care of all the special features listed above. You
usually don't have to worry about these features so long as you use
Qt's input widgets (e.g. <ahref="qlineedit.html">QLineEdit</a>, <ahref="qtextedit.html">QTextEdit</a>, and derived classes)
and Qt's display widgets (e.g. <ahref="qlabel.html">QLabel</a>).
<p> Support for these writing systems is transparent to the programmer
and completely encapsulated in Qt's text engine. This means that you
don't need to have any knowledge about the writing system used in a
particular language, except for the following small points:
<ul>
<p><li><ahref="qpainter.html#drawText">QPainter::drawText</a>( int x, int y, const <ahref="qstring.html">QString</a>&str ) will always
draw the string with it's left edge at the position specified with
the x, y parameters. This will usually give you left aligned strings.
Arabic and Hebrew application strings are usually right
aligned, so for these languages use the version of drawText() that
takes a <ahref="qrect.html">QRect</a> since this will align in accordance with the language.
<p><li> When you write your own text input controls, use <ahref="qfontmetrics.html#charWidth">QFontMetrics::charWidth</a>() to determine the width of a character in a
string. In some languages (e.g. Arabic or languages from the Indian
subcontinent), the width and shape of a glyph changes depending on the
surrounding characters. Writing input controls usually requires a
certain knowledge of the scripts it is going to be used in. Usually
the easiest way is to subclass <ahref="qlineedit.html">QLineEdit</a> or <ahref="qtextedit.html">QTextEdit</a>.
<p></ul>
<p> The following sections give some information on the status
of the internationalization (i18n) support in Qt.
<p> See also the <ahref="linguist-manual.html">Qt Linguist</a> manual.
<p><h2> Step by Step
</h2>
<aname="1"></a><p> Writing multi-platform international software with Qt is a gentle,
incremental process. Your software can become internationalized in
the following stages:
<p><h3> Use <ahref="qstring.html">QString</a> for all User-visible Text
</h3>
<aname="1-1"></a><p> Since QString uses the Unicode encoding internally, every
language in the world can be processed transparently using
familiar text processing operations. Also, since all Qt
functions that present text to the user take a QString as a
parameter, there is no char* to QString conversion overhead.
<p> Strings that are in "programmer space" (such as <ahref="qobject.html">QObject</a> names
and file format texts) need not use QString; the traditional
char* or the <ahref="qcstring.html">QCString</a> class will suffice.
<p> You're unlikely to notice that you are using Unicode;
QString, and <ahref="qchar.html">QChar</a> are just like easier versions of the crude
const char* and char from traditional C.
<p><h3> Use tr() for all Literal Text
</h3>
<aname="1-2"></a><p> Wherever your program uses <tt>"quoted text"</tt> for text that will
be presented to the user, ensure that it is processed by the <ahref="qapplication.html#translate">QApplication::translate</a>() function. Essentially all that is necessary
to achieve this is to use <ahref="qobject.html#tr">QObject::tr</a>(). For example, assuming the
<tt>LoginWidget</tt> is a subclass of QWidget:
<p><pre>
LoginWidget::LoginWidget()
{
<ahref="qlabel.html">QLabel</a> *label = new <ahref="qlabel.html">QLabel</a>( tr("Password:"), this );
...
}
</pre>
<p> This accounts for 99% of the user-visible strings you're likely to
write.
<p> If the quoted text is not in a member function of a
<ahref="qobject.html">QObject</a> subclass, use either the tr() function of an
appropriate class, or the <ahref="qapplication.html#translate">QApplication::translate</a>() function
directly:
<p><pre>
void some_global_function( LoginWidget *logwid )
{
<ahref="qlabel.html">QLabel</a> *label = new <ahref="qlabel.html">QLabel</a>(
LoginWidget::tr("Password:"), logwid );
}
void same_global_function( LoginWidget *logwid )
{
<ahref="qlabel.html">QLabel</a> *label = new <ahref="qlabel.html">QLabel</a>(
further information about Qt's translation tools, <em>Qt Linguist</em>, <em>lupdate</em> and <em>lrelease</em>.
<p> Translation of a Qt application is a three-step process:
<p><oltype=1>
<p><li> Run <em>lupdate</em> to extract translatable text from the C++ source
code of the Qt application, resulting in a message file for
translators (a <tt>.ts</tt> file). The utility recognizes the tr() construct
and the QT_*_NOOP macros described above and produces <tt>.ts</tt> files
(usually one per language).
<p><li> Provide translations for the source texts in the <tt>.ts</tt> file, using
<em>Qt Linguist</em>. Since <tt>.ts</tt> files are in XML format, you can also
edit them by hand.
<p><li> Run <em>lrelease</em> to obtain a light-weight message file (a <tt>.qm</tt>
file) from the <tt>.ts</tt> file, suitable only for end use. Think of the <tt>.ts</tt> files as "source files", and <tt>.qm</tt> files as "object files". The
translator edits the <tt>.ts</tt> files, but the users of your application
only need the <tt>.qm</tt> files. Both kinds of files are platform and
locale independent.
<p></ol>
<p> Typically, you will repeat these steps for every release of your
application. The <em>lupdate</em> utility does its best to reuse the
translations from previous releases.
<p> Before you run <em>lupdate</em>, you should prepare a project file. Here's
an example project file (<tt>.pro</tt> file):
<p><pre>
HEADERS = funnydialog.h \
wackywidget.h
SOURCES = funnydialog.cpp \
main.cpp \
wackywidget.cpp
FORMS = fancybox.ui
TRANSLATIONS = superapp_dk.ts \
superapp_fi.ts \
superapp_no.ts \
superapp_se.ts
</pre>
<p> When you run <em>lupdate</em> or <em>lrelease</em>, you must give the name of the
project file as a command-line argument.
<p> In this example, four exotic languages are supported: Danish, Finnish,
Norwegian and Swedish. If you use <ahref="qmake-manual.html">qmake</a>, you usually don't need an extra project
file for <em>lupdate</em>; your <tt>qmake</tt> project file will work fine once
you add the <tt>TRANSLATIONS</tt> entry.
<p> In your application, you must <ahref="qtranslator.html#load">QTranslator::load</a>() the translation
files appropriate for the user's language, and install them using <ahref="qapplication.html#installTranslator">QApplication::installTranslator</a>().
<p> If you have been using the old Qt tools (<tt>findtr</tt>, <tt>msg2qm</tt> and <tt>mergetr</tt>), you can use <em>qm2ts</em> to convert your old <tt>.qm</tt> files.