Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 744

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/doc/i18n.doc:36 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Internationalization with Qt</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Internationalization with Qt</h1>

 
<p> <!-- index internationalization --><a name="internationalization"></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> 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> See also the <a href="linguist-manual.html">Qt Linguist</a> manual.
<p> <h2> Step by Step
</h2>
<a name="1"></a><p> Writing cross-platform international software with Qt is a gentle,
incremental process. Your software can become internationalized in
the following stages:
<p> <h3> Use <a href="qstring.html">QString</a> for all User-visible Text
</h3>
<a name="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 time overhead.
<p> Strings that are in "programmer space" (such as <a href="qobject.html">QObject</a> names
and file format texts) need not use QString; the traditional
char* or the <a href="qcstring.html">QCString</a> class will suffice.
<p> You're unlikely to notice that you are using Unicode;
QString, and <a href="qchar.html">QChar</a> are just like easier versions of the crude
const char* and char from traditional C.
<p> <h3> Use <a href="qobject.html#tr">tr()</a> for all Literal Text
</h3>
<a name="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 <a href="qapplication.html#translate">QApplication::translate</a>() function. Essentially all that is necessary
to achieve this is to use <a href="qobject.html#tr">QObject::tr</a>(). For example, assuming
<tt>LoginWidget</tt> is a subclass of QWidget:
<p> <pre>
    LoginWidget::LoginWidget()
    {
        <a href="qlabel.html">QLabel</a> *label = new <a href="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
<a href="qobject.html">QObject</a> subclass, use either the tr() function of an
appropriate class, or the <a href="qapplication.html#translate">QApplication::translate</a>() function
directly:
<p> <pre>
    void some_global_function( LoginWidget *logwid )
    {
        <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>(
                LoginWidget::tr("Password:"), logwid );
    }

    void same_global_function( LoginWidget *logwid )
    {
        <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>(
                qApp-&gt;<a href="qapplication.html#translate">translate</a>("LoginWidget", "Password:"),
                logwid );
    }
</pre>
 
<p> If you need to have translatable text completely
outside a function, there are two macros to help: QT_TR_NOOP()
and QT_TRANSLATE_NOOP(). They merely mark the text for
extraction by the <em>lupdate</em> utility described below.
The macros expand to just the text (without the context).
<p> Example of QT_TR_NOOP():
<pre>
    QString FriendlyConversation::greeting( int greet_type )
    {
        static const char* greeting_strings[] = {
            QT_TR_NOOP( "Hello" ),
            QT_TR_NOOP( "Goodbye" )
        };
        return tr( greeting_strings[greet_type] );
    }
</pre>
 
<p> Example of QT_TRANSLATE_NOOP():
<pre>
    static const char* greeting_strings[] = {
        QT_TRANSLATE_NOOP( "FriendlyConversation", "Hello" ),
        QT_TRANSLATE_NOOP( "FriendlyConversation", "Goodbye" )
    };

    QString FriendlyConversation::greeting( int greet_type )
    {
        return tr( greeting_strings[greet_type] );
    }

    <a href="qstring.html">QString</a> global_greeting( int greet_type )
    {
        return qApp-&gt;<a href="qapplication.html#translate">translate</a>( "FriendlyConversation",
                                greeting_strings[greet_type] );
    }
</pre>
 
<p> If you disable the const char* to <a href="qstring.html">QString</a> automatic conversion
by compiling your software with the macro QT_NO_CAST_ASCII
defined, you'll be very likely to catch any strings you are
missing. See <a href="qstring.html#fromLatin1">QString::fromLatin1</a>() for more information.
Disabling the conversion makes programming cumbersome.
<p> If your source language uses characters outside Latin-1, you
might find <a href="qobject.html#trUtf8">QObject::trUtf8</a>() more convenient than
<a href="qobject.html#tr">QObject::tr</a>(), as tr() depends on the
<a href="qapplication.html#defaultCodec">QApplication::defaultCodec</a>(), which makes it more fragile than
QObject::trUtf8().
<p> <h3> Use <a href="qkeysequence.html">QKeySequence</a>() for Accelerator Values
</h3>
<a name="1-3"></a><p> Accelerator values such as Ctrl+Q or Alt+F need to be
translated too. If you hardcode <tt>CTRL+Key_Q</tt> for "Quit" in
your application, translators won't be able to override
it. The correct idiom is
<p> <pre>
    <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( tr("&amp;Quit"), this, SLOT(quit()),
                      QKeySequence(tr("Ctrl+Q", "File|Quit")) );
</pre>
 
<p> <h3> Use <a href="qstring.html#arg">QString::arg</a>() for Simple Arguments
</h3>
<a name="1-4"></a><p> The printf() style of inserting arguments in strings
is often a poor choice for internationalized text, as it is
sometimes necessary to change the order of arguments when
translating. Nonetheless, the QString::arg()
functions offer a simple means for substituting arguments:
<pre>
    void FileCopier::showProgress( int done, int total,
                                   const <a href="qstring.html">QString</a>&amp; current_file )
    {
        label.setText( tr("%1 of %2 files copied.\nCopying: %3")
                        .arg(done)
                        .arg(total)
                        .arg(current_file) );
    }
</pre>
 
<p> <h3> Produce Translations
</h3>
<a name="1-5"></a><p> Once you are using tr() throughout an application, you can start
producing translations of the user-visible text in your program.
<p> <a href="linguist-manual.html">Qt Linguist</a>'s manual provides
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> <ol type=1>
<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 .ts file). The utility
recognizes the tr() construct and the QT_*_NOOP macros described
above and produces .ts files (usually one per language).
<li> Provide translations for the source texts in the .ts
file, using Qt Linguist. Since .ts files are in XML
format, you can also edit them by hand.
<li> Run <em>lrelease</em> to obtain a light-weight message
file (a .qm file) from the .ts file, suitable only for end
use. 	You can see the .ts files as "source files", and .qm
as "object files". The translator edits the .ts files, but
the users of your application only need the .qm files. Both
kinds of files are platform and locale independent.
</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 (.pro 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 qmake (or tmake), you
usually don't need an extra project file for <em>lupdate</em>; your
qmake project file will work fine once you add the TRANSLATIONS entry.
<p> In your application, you must <a href="qtranslator.html#load">QTranslator::load</a>()
the translation files appropriate for the user's language, and
install them using <a href="qapplication.html#installTranslator">QApplication::installTranslator</a>().
<p> If you have been using the old Qt tools (findtr, msg2qm and
mergetr), you can use <em>qm2ts</em> to convert your old
.qm files.
<p> <em>linguist</em>, <em>lupdate</em> and <em>lrelease</em> are installed in
<tt>$QTDIR/bin</tt>. Click Help|Manual in Qt Linguist to access the
user's manual; it contains a tutorial to get you started.
<p> While these utilities offer a convenient way to create .qm files, any
system that writes .qm files is sufficient. You could make an
application that adds translations to a <a href="qtranslator.html">QTranslator</a> with
<a href="qtranslator.html#insert">QTranslator::insert</a>() and then writes a .qm file with
<a href="qtranslator.html#save">QTranslator::save</a>(). This way the translations can come from any
source you choose.
<p> <a name="qt-itself"></a>
Qt itself contains about 400 strings that will also need to be
translated into the languages that you are targeting. You will find
translation files for French and German in <tt>$QTDIR/translations</tt>
as well as a template for translating to other languages.
<p> Typically, your application's main() function will look like this:
<pre>
    int main( int argc, char **argv )
    {
        <a href="qapplication.html">QApplication</a> app( argc, argv );

        // translation file for Qt
        <a href="qtranslator.html">QTranslator</a> qt( 0 );
        qt.<a href="qtranslator.html#load">load</a>( QString( "qt_" ) + QTextCodec::locale(), "." );
        app.<a href="qapplication.html#installTranslator">installTranslator</a>( &amp;qt );

        // translation file for application strings
        <a href="qtranslator.html">QTranslator</a> myapp( 0 );
        myapp.<a href="qtranslator.html#load">load</a>( QString( "myapp_" ) + QTextCodec::locale(), "." );
        app.<a href="qapplication.html#installTranslator">installTranslator</a>( &amp;myapp );

        ...

        return app.<a href="qapplication.html#exec">exec</a>();
    }
</pre>
 
<p> <h3> Support for Encodings
</h3>
<a name="1-6"></a><p> The <a href="qtextcodec.html">QTextCodec</a> class and the facilities in <a href="qtextstream.html">QTextStream</a> make it easy
to support many input and output encodings for your users' data. When
an application starts, the locale of the machine will determine the
8-bit encoding used when dealing with 8-bit data - such as for font
selection, text display, 8-bit text I/O and character input.
<p> The application may occasionally require encodings other
than the default local 8-bit encoding. For example, an application
in a Cyrillic KOI8-R locale (the de-facto standard locale in Russia)
might need to output Cyrillic in the ISO 8859-5 encoding. Code for
this would be:
<p> <pre>
    <a href="qstring.html">QString</a> string = ...; // some Unicode text

    <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::<a href="qtextcodec.html#codecForName">codecForName</a>( "ISO 8859-5" );
    <a href="qcstring.html">QCString</a> encoded_string = codec-&gt;<a href="qtextcodec.html#fromUnicode">fromUnicode</a>( string );

    ...; // use encoded_string in 8-bit operations
</pre>
 
<p> For converting Unicode to local 8-bit encodings,
a shortcut is available: the
<a href="qstring.html#local8Bit">local8Bit</a>() method of
<a href="qstring.html">QString</a> returns such 8-bit data. Another useful shortcut
is the <a href="qstring.html#utf8">utf8</a>() method, which
returns text in the 8-bit UTF-8 encoding - interesting in
that it perfectly preserves Unicode information while looking
like plain US-ASCII if the Unicode is wholly US-ASCII.
<p> For converting the other way, there are the <a href="qstring.html#fromUtf8">QString::fromUtf8</a>() and
<a href="qstring.html#fromLocal8Bit">QString::fromLocal8Bit</a>() convenience functions, or the general code,
demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode
conversion:
<p> <pre>
    <a href="qcstring.html">QCString</a> encoded_string = ...; // Some ISO 8859-5 encoded text.

    <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::<a href="qtextcodec.html#codecForName">codecForName</a>("ISO 8859-5");
    <a href="qstring.html">QString</a> string = codec-&gt;<a href="qtextcodec.html#toUnicode">toUnicode</a>(encoded_string);

    ...; // Use string in all of Qt's QString operations.
</pre>
 
<p> Ideally Unicode I/O should be used as this maximizes the portability
of documents between users around the world, but in reality it is
useful to support all the appropriate encodings that your users will
need to process existing documents. In general, Unicode (UTF16 or
UTF8) is best for information transferred between arbitrary
people, while within a language or national group, a local standard
is often more appropriate. The most important encoding to support is
the one returned by <a href="qtextcodec.html#codecForLocale">QTextCodec::codecForLocale</a>(), as this is the one
the user is most likely to need for communicating with other people
and applications (this is the codec used by local8Bit()).
<p> Since most Unix systems do not have built-in support for converting
between local 8-bit encodings and Unicode, it may be necessary to
write your own <a href="qtextcodec.html">QTextCodec</a> subclass. Depending on the urgency, it
may be useful to contact Trolltech technical support or ask on
the qt-interest mailing list to see if someone else is already working
on supporting the encoding. A useful interim measure can be to
use the <a href="qtextcodec.html#loadCharmapFile">QTextCodec::loadCharmapFile</a>() function to build a data-driven
codec, although this approach has a memory and speed penalty,
especially with dynamically loaded libraries. For details of writing
your own QTextCodec, see the main QTextCodec class documentation.
<p> <!-- index localization --><a name="localization"></a>
<p> <h3> Localize
</h3>
<a name="1-7"></a><p> Localization is the process of adapting to local conventions
such as date and time presentations. Such localizations can be
accomplished using appropriate tr() strings, even "magic" words,
as this somewhat contrived example shows:
<p> <pre>
    void Clock::setTime(const <a href="qtime.html">QTime</a>&amp; t)
    {
        if ( tr("AMPM") == "AMPM" ) {
            // 12-hour clock
        } else {
            // 24-hour clock
        }
    }
</pre>
 
<p> Localizing images is not recommended. Choose clear icons that are
appropriate for all localities, rather than relying on local puns or
stretched metaphors.
<p> <h2> System Support
</h2>
<a name="2"></a><p> Operating systems and window systems supporting Unicode are still in
the early stages of development. The level of support
available in the underlying system influences the support Qt provides
on that platform, but applications written with Qt need not generally
be too concerned with the actual limitations.
<p> <h3> Unix/X11
</h3>
<a name="2-1"></a><p> <ul>
<li> Locale-oriented fonts and input methods. Qt hides these and
provides Unicode input and output.
<li> Filesystem conventions such as
<a href="http://www.ietf.org/rfc/rfc2279.txt">UTF-8</a>
are under development
in some Unix variants. All Qt file functions allow Unicode,
but convert all filenames to the local 8-bit encoding, as
this is the Unix convention
(see <a href="qfile.html#setEncodingFunction">QFile::setEncodingFunction</a>()
to explore alternative encodings).
<li> File I/O defaults to the local 8-bit encoding,
with Unicode options in <a href="qtextstream.html">QTextStream</a>.
</ul>
<p> <h3> Windows 95/98/NT
</h3>
<a name="2-2"></a><p> <ul>
<li> Qt provides full Unicode support, including input methods, fonts,
clipboard, drag-and-drop and file names.
<li> File I/O defaults to Latin-1, with Unicode options in QTextStream.
Note that some Windows programs do not understand big-endian
Unicode text files even though that is the order prescribed by
the Unicode Standard in the absence of higher-level protocols.
<li> Unlike programs written with MFC or plain winlib, Qt programs
are portable between Windows 95/98 and Windows NT.
<em>You do not need different binaries to support Unicode.</em>
</ul>
<p> <h2> Supporting More Input Methods
</h2>
<a name="3"></a><p> While Trolltech doesn't have the resources or expertise in all the
languages of the world to immediately include support in Qt, we are
very keen to work with people who do have the expertise. Over the
next few minor version numbers, we hope to add support for your
language of choice, until everyone can use Qt and all the programs
developed with Qt, regardless of their language.
<p> Languages with single-byte encodings
(European Latin-1 and KOI8-R, etc.) and multi-byte encodings 
(East Asian EUC-JP, etc.) are supported. 
Support for the "complex" encodings - those requiring
right-to-left input or complex character composition (eg. Arabic,
Hebrew, and Thai script) is implemented, but the range of Indic scripts
(Hindi, Devanagari, Bengali, etc.) is still under development. 
The current state of activity is:
<p> <center><table cellpadding="4" cellspacing="2" border="0">
<tr bgcolor="#a2c511"> <th valign="top">Encodings <th valign="top">Status
<tr bgcolor="#f0f0f0"> 
<td valign="top">All encodings on Windows
<td valign="top">The local encoding is always supported.
<tr bgcolor="#d0d0d0">
<td valign="top">ISO standard encodings
ISO 8859-1,
ISO 8859-2,
ISO 8859-3,
ISO 8859-4,
ISO 8859-5,
ISO 8859-7,
ISO 8859-9, and
ISO 8859-15
<td valign="top">Fully supported.
<tr bgcolor="#f0f0f0">
<td valign="top">KOI8-R
<td valign="top">Fully supported.
<tr bgcolor="#d0d0d0">
<td valign="top">eucJP, JIS, and ShiftJIS
<td valign="top">Fully supported. Uses eucJP with the XIM protocol on X11,
and the IME Windows NT in Japanese Windows NT.
Serika Kurusugawa and others are assisting with this effort.
<a href="ftp://ftp.sra.co.jp/pub/x11/kinput2/">kinput2</a>
is the tested input method for X11.
<tr bgcolor="#f0f0f0">
<td valign="top">eucKR
<td valign="top">Supported.
Mizi Research are assisting with this effort.
<a href="http://www.mizi.com">hanIM</a>
is the tested input method.
<tr bgcolor="#d0d0d0">
<td valign="top">Big5
<td valign="top">Qt contains a Big5 codec developed by Ming Che-Chuang.
Testing is underway with the xcin (2.5.x) XIM server.
<tr bgcolor="#f0f0f0">
<td valign="top">eucTW
<td valign="top">Under external development.
</table></center>
<p> More information on the support of different writing systems in Qt can be found
in <a href="scripts.html">the documentation about writing systems</a>.
<p> If you are interested in contributing to existing efforts, or
supporting new encodings beyond those mentioned above, your
work can be considered for inclusion in the official Qt distribution,
or just included with your application.
<p> Eventually, we hope to help Unix become as Unicode-oriented as
Windows is becoming. This means better font support in the font servers,
with new developments like the True Type font servers
<a href="http://www.dcs.ed.ac.uk/home/jec/programs/xfsft/">xfsft</a>,
<a href="ftp://sunsite.unc.edu/pub/Linux/X11/fonts/">xfstt</a>,
and x-tt, as well as
<a href="http://www.ietf.org/rfc/rfc2279.txt">UTF-8</a>
(a Unicode encoding) filenames such as with the
<a href="http://www.sun.com/software/white-papers/wp-unicode/">Unicode
support in Solaris 7</a>.
<p> <h2> Note about Locales on X11
</h2>
<a name="4"></a><p> Many Unix distributions contain only partial support for some locales.
For example, if you have a <tt>/usr/share/locale/ja_JP.EUC</tt>
directory, this does not necessarily mean you can display Japanese text;
you also need JIS encoded fonts (or Unicode fonts), and that
<tt>/usr/share/locale/ja_JP.EUC</tt> directory needs to be complete.
For best results, use complete locales from your system vendor.
<p> <h2> Relevant Qt Classes
</h2>
<a name="5"></a><p> These classes are relevant to internationalizing Qt applications.

<p><table width="100%">
<tr bgcolor=#f0f0f0><td><b><a href="qeucjpcodec.html">QEucJpCodec</a></b><td>Conversion to and from EUC-JP character sets
<tr bgcolor=#f0f0f0><td><b><a href="qeuckrcodec.html">QEucKrCodec</a></b><td>Conversion to and from EUC-KR character sets
<tr bgcolor=#f0f0f0><td><b><a href="qgbkcodec.html">QGbkCodec</a></b><td>Conversion to and from the Chinese GBK encoding
<tr bgcolor=#f0f0f0><td><b><a href="qhebrewcodec.html">QHebrewCodec</a></b><td>Conversion to and from visually ordered Hebrew
<tr bgcolor=#f0f0f0><td><b><a href="qjiscodec.html">QJisCodec</a></b><td>Conversion to and from JIS character sets
<tr bgcolor=#f0f0f0><td><b><a href="qsjiscodec.html">QSjisCodec</a></b><td>Conversion to and from Shift-JIS
<tr bgcolor=#f0f0f0><td><b><a href="qtextcodec.html">QTextCodec</a></b><td>Conversion between text encodings
<tr bgcolor=#f0f0f0><td><b><a href="qtextdecoder.html">QTextDecoder</a></b><td>State-based decoder
<tr bgcolor=#f0f0f0><td><b><a href="qtextencoder.html">QTextEncoder</a></b><td>State-based encoder
<tr bgcolor=#f0f0f0><td><b><a href="qtranslator.html">QTranslator</a></b><td>Internationalization support for text output
<tr bgcolor=#f0f0f0><td><b><a href="qtranslatormessage.html">QTranslatorMessage</a></b><td>Translator message and its properties
<tr bgcolor=#f0f0f0><td><b><a href="qtsciicodec.html">QTsciiCodec</a></b><td>Conversion to and from the Tamil TSCII encoding
</table>
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>