1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/i18n.doc:36 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Internationalization with Qt</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <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>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p> <!-- index internationalization --><a name="internationalization"></a><!-- index i18n --><a name="i18n"></a>
|
---|
36 | <p> The internationalization of an application is the process of making
|
---|
37 | the application usable by people in countries other than one's own.
|
---|
38 | <p> <!-- toc -->
|
---|
39 | <ul>
|
---|
40 | <li><a href="#1"> Step by Step
|
---|
41 | </a>
|
---|
42 | <ul>
|
---|
43 | <li><a href="#1-1"> Use QString for all User-visible Text
|
---|
44 | </a>
|
---|
45 | <li><a href="#1-2"> Use tr() for all Literal Text
|
---|
46 | </a>
|
---|
47 | <li><a href="#1-3"> Use QKeySequence() for Accelerator Values
|
---|
48 | </a>
|
---|
49 | <li><a href="#1-4"> Use QString::arg() for Dynamic Text
|
---|
50 | </a>
|
---|
51 | <li><a href="#1-5"> Produce Translations
|
---|
52 | </a>
|
---|
53 | <li><a href="#1-6"> Support for Encodings
|
---|
54 | </a>
|
---|
55 | <li><a href="#1-7"> Localize
|
---|
56 | </a>
|
---|
57 | </ul>
|
---|
58 | <li><a href="#2"> Dynamic Translation
|
---|
59 | </a>
|
---|
60 | <li><a href="#3"> System Support
|
---|
61 | </a>
|
---|
62 | <ul>
|
---|
63 | <li><a href="#3-1"> Unix/X11
|
---|
64 | </a>
|
---|
65 | <li><a href="#3-2"> Windows
|
---|
66 | </a>
|
---|
67 | </ul>
|
---|
68 | <li><a href="#4"> Note about Locales on X11
|
---|
69 | </a>
|
---|
70 | <li><a href="#5"> Relevant Qt Classes
|
---|
71 | </a>
|
---|
72 | </ul>
|
---|
73 | <!-- endtoc -->
|
---|
74 |
|
---|
75 | <p> In some cases internationalization is simple, for example, making a US
|
---|
76 | application accessible to Australian or British users may require
|
---|
77 | little more than a few spelling corrections. But to make a US
|
---|
78 | application usable by Japanese users, or a Korean application usable
|
---|
79 | by German users, will require that the software operate not only in
|
---|
80 | different languages, but use different input techniques, character
|
---|
81 | encodings and presentation conventions.
|
---|
82 | <p> Qt tries to make internationalization as painless as possible for
|
---|
83 | developers. All input widgets and text drawing methods in Qt offer
|
---|
84 | built-in support for all supported languages. The built-in font engine
|
---|
85 | is capable of correctly and attractively rendering text that contains
|
---|
86 | characters from a variety of different writing systems at the same
|
---|
87 | time.
|
---|
88 | <p> Qt supports most languages in use today, in particular:
|
---|
89 | <ul>
|
---|
90 | <li> All East Asian languages (Chinese, Japanese and Korean)
|
---|
91 | <li> All Western languages (using Latin script)
|
---|
92 | <li> Arabic
|
---|
93 | <li> Cyrillic languages (Russian)
|
---|
94 | <li> Greek
|
---|
95 | <li> Hebrew
|
---|
96 | <li> Thai and Lao
|
---|
97 | <li> All scripts in Unicode 3.2 that do not require special processing
|
---|
98 | </ul>
|
---|
99 | <p> On Windows NT/2000/XP and Unix/X11 with Xft (client side font support)
|
---|
100 | the following languages are also supported:
|
---|
101 | <ul>
|
---|
102 | <li> Bengali
|
---|
103 | <li> Devanagari
|
---|
104 | <li> Dhivehi (Thaana)
|
---|
105 | <li> Gujarati
|
---|
106 | <li> Gurmukhi
|
---|
107 | <li> Kannada
|
---|
108 | <li> Khmer
|
---|
109 | <li> Malayalam (X11 only)
|
---|
110 | <li> Myanmar (X11 only)
|
---|
111 | <li> Syriac
|
---|
112 | <li> Tamil
|
---|
113 | <li> Telugu
|
---|
114 | <li> Tibetan (X11 only)
|
---|
115 | </ul>
|
---|
116 | <p> Many of these writing systems exhibit special features:
|
---|
117 | <p> <ul>
|
---|
118 | <p> <li> <b>Special line breaking behavior.</b> Some of the Asian languages are
|
---|
119 | written without spaces between words. Line breaking can occur either
|
---|
120 | after every character (with exceptions) as in Chinese, Japanese and
|
---|
121 | Korean, or after logical word boundaries as in Thai.
|
---|
122 | <p> <li> <b>Bidirectional writing.</b> Arabic and Hebrew are written from right to
|
---|
123 | left, except for numbers and embedded English text which is written
|
---|
124 | left to right. The exact behavior is defined in the <a href="http://www.unicode.org/unicode/reports/tr9/">Unicode Technical Report
|
---|
125 | #9</a>.
|
---|
126 | <p> <li> <b>Non spacing or diacritical marks</b> (accents or umlauts in European
|
---|
127 | languages). Some languages such as Vietnamese make extensive use of
|
---|
128 | these marks and some characters can have more than one mark at the
|
---|
129 | same time to clarify pronunciation.
|
---|
130 | <p> <li> <b>Ligatures.</b> In special contexts, some pairs of characters get
|
---|
131 | replaced by a combined glyph forming a ligature. Common examples are
|
---|
132 | the fl and fi ligatures used in typesetting US and European books.
|
---|
133 | <p> </ul>
|
---|
134 | <p> Qt tries to take care of all the special features listed above. You
|
---|
135 | usually don't have to worry about these features so long as you use
|
---|
136 | Qt's input widgets (e.g. <a href="qlineedit.html">QLineEdit</a>, <a href="qtextedit.html">QTextEdit</a>, and derived classes)
|
---|
137 | and Qt's display widgets (e.g. <a href="qlabel.html">QLabel</a>).
|
---|
138 | <p> Support for these writing systems is transparent to the programmer
|
---|
139 | and completely encapsulated in Qt's text engine. This means that you
|
---|
140 | don't need to have any knowledge about the writing system used in a
|
---|
141 | particular language, except for the following small points:
|
---|
142 | <ul>
|
---|
143 | <p> <li> <a href="qpainter.html#drawText">QPainter::drawText</a>( int x, int y, const <a href="qstring.html">QString</a> &str ) will always
|
---|
144 | draw the string with it's left edge at the position specified with
|
---|
145 | the x, y parameters. This will usually give you left aligned strings.
|
---|
146 | Arabic and Hebrew application strings are usually right
|
---|
147 | aligned, so for these languages use the version of drawText() that
|
---|
148 | takes a <a href="qrect.html">QRect</a> since this will align in accordance with the language.
|
---|
149 | <p> <li> When you write your own text input controls, use <a href="qfontmetrics.html#charWidth">QFontMetrics::charWidth</a>() to determine the width of a character in a
|
---|
150 | string. In some languages (e.g. Arabic or languages from the Indian
|
---|
151 | subcontinent), the width and shape of a glyph changes depending on the
|
---|
152 | surrounding characters. Writing input controls usually requires a
|
---|
153 | certain knowledge of the scripts it is going to be used in. Usually
|
---|
154 | the easiest way is to subclass <a href="qlineedit.html">QLineEdit</a> or <a href="qtextedit.html">QTextEdit</a>.
|
---|
155 | <p> </ul>
|
---|
156 | <p> The following sections give some information on the status
|
---|
157 | of the internationalization (i18n) support in Qt.
|
---|
158 | <p> See also the <a href="linguist-manual.html">Qt Linguist</a> manual.
|
---|
159 | <p> <h2> Step by Step
|
---|
160 | </h2>
|
---|
161 | <a name="1"></a><p> Writing multi-platform international software with Qt is a gentle,
|
---|
162 | incremental process. Your software can become internationalized in
|
---|
163 | the following stages:
|
---|
164 | <p> <h3> Use <a href="qstring.html">QString</a> for all User-visible Text
|
---|
165 | </h3>
|
---|
166 | <a name="1-1"></a><p> Since QString uses the Unicode encoding internally, every
|
---|
167 | language in the world can be processed transparently using
|
---|
168 | familiar text processing operations. Also, since all Qt
|
---|
169 | functions that present text to the user take a QString as a
|
---|
170 | parameter, there is no char* to QString conversion overhead.
|
---|
171 | <p> Strings that are in "programmer space" (such as <a href="qobject.html">QObject</a> names
|
---|
172 | and file format texts) need not use QString; the traditional
|
---|
173 | char* or the <a href="qcstring.html">QCString</a> class will suffice.
|
---|
174 | <p> You're unlikely to notice that you are using Unicode;
|
---|
175 | QString, and <a href="qchar.html">QChar</a> are just like easier versions of the crude
|
---|
176 | const char* and char from traditional C.
|
---|
177 | <p> <h3> Use tr() for all Literal Text
|
---|
178 | </h3>
|
---|
179 | <a name="1-2"></a><p> Wherever your program uses <tt>"quoted text"</tt> for text that will
|
---|
180 | 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
|
---|
181 | to achieve this is to use <a href="qobject.html#tr">QObject::tr</a>(). For example, assuming the
|
---|
182 | <tt>LoginWidget</tt> is a subclass of QWidget:
|
---|
183 | <p> <pre>
|
---|
184 | LoginWidget::LoginWidget()
|
---|
185 | {
|
---|
186 | <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>( tr("Password:"), this );
|
---|
187 | ...
|
---|
188 | }
|
---|
189 | </pre>
|
---|
190 |
|
---|
191 | <p> This accounts for 99% of the user-visible strings you're likely to
|
---|
192 | write.
|
---|
193 | <p> If the quoted text is not in a member function of a
|
---|
194 | <a href="qobject.html">QObject</a> subclass, use either the tr() function of an
|
---|
195 | appropriate class, or the <a href="qapplication.html#translate">QApplication::translate</a>() function
|
---|
196 | directly:
|
---|
197 | <p> <pre>
|
---|
198 | void some_global_function( LoginWidget *logwid )
|
---|
199 | {
|
---|
200 | <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>(
|
---|
201 | LoginWidget::tr("Password:"), logwid );
|
---|
202 | }
|
---|
203 |
|
---|
204 | void same_global_function( LoginWidget *logwid )
|
---|
205 | {
|
---|
206 | <a href="qlabel.html">QLabel</a> *label = new <a href="qlabel.html">QLabel</a>(
|
---|
207 | qApp-><a href="qapplication.html#translate">translate</a>("LoginWidget", "Password:"),
|
---|
208 | logwid );
|
---|
209 | }
|
---|
210 | </pre>
|
---|
211 |
|
---|
212 | <p> If you need to have translatable text completely
|
---|
213 | outside a function, there are two macros to help: QT_TR_NOOP()
|
---|
214 | and QT_TRANSLATE_NOOP(). They merely mark the text for
|
---|
215 | extraction by the <em>lupdate</em> utility described below.
|
---|
216 | The macros expand to just the text (without the context).
|
---|
217 | <p> Example of QT_TR_NOOP():
|
---|
218 | <pre>
|
---|
219 | QString FriendlyConversation::greeting( int greet_type )
|
---|
220 | {
|
---|
221 | static const char* greeting_strings[] = {
|
---|
222 | QT_TR_NOOP( "Hello" ),
|
---|
223 | QT_TR_NOOP( "Goodbye" )
|
---|
224 | };
|
---|
225 | return tr( greeting_strings[greet_type] );
|
---|
226 | }
|
---|
227 | </pre>
|
---|
228 |
|
---|
229 | <p> Example of QT_TRANSLATE_NOOP():
|
---|
230 | <pre>
|
---|
231 | static const char* greeting_strings[] = {
|
---|
232 | QT_TRANSLATE_NOOP( "FriendlyConversation", "Hello" ),
|
---|
233 | QT_TRANSLATE_NOOP( "FriendlyConversation", "Goodbye" )
|
---|
234 | };
|
---|
235 |
|
---|
236 | QString FriendlyConversation::greeting( int greet_type )
|
---|
237 | {
|
---|
238 | return tr( greeting_strings[greet_type] );
|
---|
239 | }
|
---|
240 |
|
---|
241 | <a href="qstring.html">QString</a> global_greeting( int greet_type )
|
---|
242 | {
|
---|
243 | return qApp-><a href="qapplication.html#translate">translate</a>( "FriendlyConversation",
|
---|
244 | greeting_strings[greet_type] );
|
---|
245 | }
|
---|
246 | </pre>
|
---|
247 |
|
---|
248 | <p> If you disable the const char* to <a href="qstring.html">QString</a> automatic conversion
|
---|
249 | by compiling your software with the macro QT_NO_CAST_ASCII
|
---|
250 | defined, you'll be very likely to catch any strings you are
|
---|
251 | missing. See <a href="qstring.html#fromLatin1">QString::fromLatin1</a>() for more information.
|
---|
252 | Disabling the conversion can make programming a bit cumbersome.
|
---|
253 | <p> If your source language uses characters outside Latin-1, you
|
---|
254 | might find <a href="qobject.html#trUtf8">QObject::trUtf8</a>() more convenient than
|
---|
255 | <a href="qobject.html#tr">QObject::tr</a>(), as tr() depends on the
|
---|
256 | <a href="qapplication.html#defaultCodec">QApplication::defaultCodec</a>(), which makes it more fragile than
|
---|
257 | QObject::trUtf8().
|
---|
258 | <p> <h3> Use <a href="qkeysequence.html">QKeySequence</a>() for Accelerator Values
|
---|
259 | </h3>
|
---|
260 | <a name="1-3"></a><p> Accelerator values such as Ctrl+Q or Alt+F need to be
|
---|
261 | translated too. If you hardcode <tt>CTRL+Key_Q</tt> for "Quit" in
|
---|
262 | your application, translators won't be able to override
|
---|
263 | it. The correct idiom is
|
---|
264 | <p> <pre>
|
---|
265 | <a href="qpopupmenu.html">QPopupMenu</a> *file = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
|
---|
266 | file-><a href="qmenudata.html#insertItem">insertItem</a>( tr("&Quit"), this, SLOT(quit()),
|
---|
267 | QKeySequence(tr("Ctrl+Q", "File|Quit")) );
|
---|
268 | </pre>
|
---|
269 |
|
---|
270 | <p> <h3> Use <a href="qstring.html#arg">QString::arg</a>() for Dynamic Text
|
---|
271 | </h3>
|
---|
272 | <a name="1-4"></a><p> The QString::arg() functions offer a simple means for substituting
|
---|
273 | arguments:
|
---|
274 | <pre>
|
---|
275 | void FileCopier::showProgress( int done, int total,
|
---|
276 | const <a href="qstring.html">QString</a>& current_file )
|
---|
277 | {
|
---|
278 | label.setText( tr("%1 of %2 files copied.\nCopying: %3")
|
---|
279 | .arg(done)
|
---|
280 | .arg(total)
|
---|
281 | .arg(current_file) );
|
---|
282 | }
|
---|
283 | </pre>
|
---|
284 |
|
---|
285 | <p> In some languages the order of arguments may need to change, and this
|
---|
286 | can easily be achieved by changing the order of the % arguments. For
|
---|
287 | example:
|
---|
288 | <pre>
|
---|
289 | <a href="qstring.html">QString</a> s1 = "%1 of %2 files copied. Copying: %3";
|
---|
290 | <a href="qstring.html">QString</a> s2 = "Kopierer nu %3. Av totalt %2 filer er %1 kopiert.";
|
---|
291 |
|
---|
292 | <a href="qapplication.html#qDebug">qDebug</a>( s1.<a href="qstring.html#arg">arg</a>(5).arg(10).arg("somefile.txt").ascii() );
|
---|
293 | <a href="qapplication.html#qDebug">qDebug</a>( s2.<a href="qstring.html#arg">arg</a>(5).arg(10).arg("somefile.txt").ascii() );
|
---|
294 | </pre>
|
---|
295 |
|
---|
296 | <p> produces the correct output in English and Norwegian:
|
---|
297 | <pre>
|
---|
298 | 5 of 10 files copied. Copying: somefile.txt
|
---|
299 | Kopierer nu somefile.txt. Av totalt 10 filer er 5 kopiert.
|
---|
300 | </pre>
|
---|
301 |
|
---|
302 | <p> <h3> Produce Translations
|
---|
303 | </h3>
|
---|
304 | <a name="1-5"></a><p> Once you are using tr() throughout an application, you can start
|
---|
305 | producing translations of the user-visible text in your program.
|
---|
306 | <p> <a href="linguist-manual.html">Qt Linguist</a>'s manual provides
|
---|
307 | further information about Qt's translation tools, <em>Qt Linguist</em>, <em>lupdate</em> and <em>lrelease</em>.
|
---|
308 | <p> Translation of a Qt application is a three-step process:
|
---|
309 | <p> <ol type=1>
|
---|
310 | <p> <li> Run <em>lupdate</em> to extract translatable text from the C++ source
|
---|
311 | code of the Qt application, resulting in a message file for
|
---|
312 | translators (a <tt>.ts</tt> file). The utility recognizes the tr() construct
|
---|
313 | and the QT_*_NOOP macros described above and produces <tt>.ts</tt> files
|
---|
314 | (usually one per language).
|
---|
315 | <p> <li> Provide translations for the source texts in the <tt>.ts</tt> file, using
|
---|
316 | <em>Qt Linguist</em>. Since <tt>.ts</tt> files are in XML format, you can also
|
---|
317 | edit them by hand.
|
---|
318 | <p> <li> Run <em>lrelease</em> to obtain a light-weight message file (a <tt>.qm</tt>
|
---|
319 | 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
|
---|
320 | translator edits the <tt>.ts</tt> files, but the users of your application
|
---|
321 | only need the <tt>.qm</tt> files. Both kinds of files are platform and
|
---|
322 | locale independent.
|
---|
323 | <p> </ol>
|
---|
324 | <p> Typically, you will repeat these steps for every release of your
|
---|
325 | application. The <em>lupdate</em> utility does its best to reuse the
|
---|
326 | translations from previous releases.
|
---|
327 | <p> Before you run <em>lupdate</em>, you should prepare a project file. Here's
|
---|
328 | an example project file (<tt>.pro</tt> file):
|
---|
329 | <p> <pre>
|
---|
330 | HEADERS = funnydialog.h \
|
---|
331 | wackywidget.h
|
---|
332 | SOURCES = funnydialog.cpp \
|
---|
333 | main.cpp \
|
---|
334 | wackywidget.cpp
|
---|
335 | FORMS = fancybox.ui
|
---|
336 | TRANSLATIONS = superapp_dk.ts \
|
---|
337 | superapp_fi.ts \
|
---|
338 | superapp_no.ts \
|
---|
339 | superapp_se.ts
|
---|
340 | </pre>
|
---|
341 |
|
---|
342 | <p> When you run <em>lupdate</em> or <em>lrelease</em>, you must give the name of the
|
---|
343 | project file as a command-line argument.
|
---|
344 | <p> In this example, four exotic languages are supported: Danish, Finnish,
|
---|
345 | Norwegian and Swedish. If you use <a href="qmake-manual.html">qmake</a>, you usually don't need an extra project
|
---|
346 | file for <em>lupdate</em>; your <tt>qmake</tt> project file will work fine once
|
---|
347 | you add the <tt>TRANSLATIONS</tt> entry.
|
---|
348 | <p> In your application, you must <a href="qtranslator.html#load">QTranslator::load</a>() the translation
|
---|
349 | files appropriate for the user's language, and install them using <a href="qapplication.html#installTranslator">QApplication::installTranslator</a>().
|
---|
350 | <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.
|
---|
351 | <p> <em>linguist</em>, <em>lupdate</em> and <em>lrelease</em> are installed in the <tt>bin</tt>
|
---|
352 | subdirectory of the base directory Qt is installed into. Click Help|Manual
|
---|
353 | in <em>Qt Linguist</em> to access the user's manual; it contains a tutorial
|
---|
354 | to get you started.
|
---|
355 | <p> While these utilities offer a convenient way to create <tt>.qm</tt> files,
|
---|
356 | any system that writes <tt>.qm</tt> files is sufficient. You could make an
|
---|
357 | application that adds translations to a <a href="qtranslator.html">QTranslator</a> with
|
---|
358 | <a href="qtranslator.html#insert">QTranslator::insert</a>() and then writes a <tt>.qm</tt> file with
|
---|
359 | <a href="qtranslator.html#save">QTranslator::save</a>(). This way the translations can come from any
|
---|
360 | source you choose.
|
---|
361 | <p> <a name="qt-itself"></a>
|
---|
362 | Qt itself contains over 400 strings that will also need to be
|
---|
363 | translated into the languages that you are targeting. You will find
|
---|
364 | translation files for French and German in <tt>$QTDIR/translations</tt> as
|
---|
365 | well as a template for translating to other languages. (This directory
|
---|
366 | also contains some additional unsupported translations which may be
|
---|
367 | useful.)
|
---|
368 | <p> Typically, your application's main() function will look like this:
|
---|
369 | <pre>
|
---|
370 | int main( int argc, char **argv )
|
---|
371 | {
|
---|
372 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
373 |
|
---|
374 | // translation file for Qt
|
---|
375 | <a href="qtranslator.html">QTranslator</a> qt( 0 );
|
---|
376 | qt.<a href="qtranslator.html#load">load</a>( QString( "qt_" ) + QTextCodec::locale(), "." );
|
---|
377 | app.<a href="qapplication.html#installTranslator">installTranslator</a>( &qt );
|
---|
378 |
|
---|
379 | // translation file for application strings
|
---|
380 | <a href="qtranslator.html">QTranslator</a> myapp( 0 );
|
---|
381 | myapp.<a href="qtranslator.html#load">load</a>( QString( "myapp_" ) + QTextCodec::locale(), "." );
|
---|
382 | app.<a href="qapplication.html#installTranslator">installTranslator</a>( &myapp );
|
---|
383 |
|
---|
384 | ...
|
---|
385 |
|
---|
386 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
387 | }
|
---|
388 | </pre>
|
---|
389 |
|
---|
390 | <p> <h3> Support for Encodings
|
---|
391 | </h3>
|
---|
392 | <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
|
---|
393 | support many input and output encodings for your users' data. When an
|
---|
394 | application starts, the locale of the machine will determine the 8-bit
|
---|
395 | encoding used when dealing with 8-bit data: such as for font
|
---|
396 | selection, text display, 8-bit text I/O and character input.
|
---|
397 | <p> The application may occasionally require encodings other than the
|
---|
398 | default local 8-bit encoding. For example, an application in a
|
---|
399 | Cyrillic KOI8-R locale (the de-facto standard locale in Russia) might
|
---|
400 | need to output Cyrillic in the ISO 8859-5 encoding. Code for this
|
---|
401 | would be:
|
---|
402 | <p> <pre>
|
---|
403 | <a href="qstring.html">QString</a> string = ...; // some Unicode text
|
---|
404 |
|
---|
405 | <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::<a href="qtextcodec.html#codecForName">codecForName</a>( "ISO 8859-5" );
|
---|
406 | <a href="qcstring.html">QCString</a> encoded_string = codec-><a href="qtextcodec.html#fromUnicode">fromUnicode</a>( string );
|
---|
407 |
|
---|
408 | ...; // use encoded_string in 8-bit operations
|
---|
409 | </pre>
|
---|
410 |
|
---|
411 | <p> For converting Unicode to local 8-bit encodings, a shortcut is
|
---|
412 | available: the <a href="qstring.html#local8Bit">local8Bit</a>() method
|
---|
413 | of <a href="qstring.html">QString</a> returns such 8-bit data. Another useful shortcut is the
|
---|
414 | <a href="qstring.html#utf8">utf8</a>() method, which returns text in the
|
---|
415 | 8-bit UTF-8 encoding: this perfectly preserves Unicode information
|
---|
416 | while looking like plain US-ASCII if the text is wholly US-ASCII.
|
---|
417 | <p> For converting the other way, there are the <a href="qstring.html#fromUtf8">QString::fromUtf8</a>() and
|
---|
418 | <a href="qstring.html#fromLocal8Bit">QString::fromLocal8Bit</a>() convenience functions, or the general code,
|
---|
419 | demonstrated by this conversion from ISO 8859-5 Cyrillic to Unicode
|
---|
420 | conversion:
|
---|
421 | <p> <pre>
|
---|
422 | <a href="qcstring.html">QCString</a> encoded_string = ...; // Some ISO 8859-5 encoded text.
|
---|
423 |
|
---|
424 | <a href="qtextcodec.html">QTextCodec</a>* codec = QTextCodec::<a href="qtextcodec.html#codecForName">codecForName</a>("ISO 8859-5");
|
---|
425 | <a href="qstring.html">QString</a> string = codec-><a href="qtextcodec.html#toUnicode">toUnicode</a>(encoded_string);
|
---|
426 |
|
---|
427 | ...; // Use string in all of Qt's QString operations.
|
---|
428 | </pre>
|
---|
429 |
|
---|
430 | <p> Ideally Unicode I/O should be used as this maximizes the portability
|
---|
431 | of documents between users around the world, but in reality it is
|
---|
432 | useful to support all the appropriate encodings that your users will
|
---|
433 | need to process existing documents. In general, Unicode (UTF-16 or
|
---|
434 | UTF-8) is best for information transferred between arbitrary people,
|
---|
435 | while within a language or national group, a local standard is often
|
---|
436 | more appropriate. The most important encoding to support is the one
|
---|
437 | returned by <a href="qtextcodec.html#codecForLocale">QTextCodec::codecForLocale</a>(), as this is the one the user
|
---|
438 | is most likely to need for communicating with other people and
|
---|
439 | applications (this is the codec used by local8Bit()).
|
---|
440 | <p> Qt supports most of the more frequently used encodings natively. For a
|
---|
441 | complete list of supported encodings see the <a href="qtextcodec.html">QTextCodec</a>
|
---|
442 | documentation.
|
---|
443 | <p> In some cases and for less frequently used encodings it may be
|
---|
444 | necessary to write your own <a href="qtextcodec.html">QTextCodec</a> subclass. Depending on the
|
---|
445 | urgency, it may be useful to contact Trolltech technical support or
|
---|
446 | ask on the <tt>qt-interest</tt> mailing list to see if someone else is
|
---|
447 | already working on supporting the encoding. A useful interim measure
|
---|
448 | can be to use the <a href="qtextcodec.html#loadCharmapFile">QTextCodec::loadCharmapFile</a>() function to build a
|
---|
449 | data-driven codec, although this approach has a memory and speed
|
---|
450 | penalty, especially with dynamically loaded libraries. For details of
|
---|
451 | writing your own QTextCodec, see the main QTextCodec class
|
---|
452 | documentation.
|
---|
453 | <p> <!-- index localization --><a name="localization"></a>
|
---|
454 | <p> <h3> Localize
|
---|
455 | </h3>
|
---|
456 | <a name="1-7"></a><p> Localization is the process of adapting to local conventions, for
|
---|
457 | example presenting dates and times using the locally preferred
|
---|
458 | formats. Such localizations can be accomplished using appropriate tr()
|
---|
459 | strings.
|
---|
460 | <p> <pre>
|
---|
461 | void Clock::setTime(const <a href="qtime.html">QTime</a>& t)
|
---|
462 | {
|
---|
463 | if ( tr("AMPM") == "AMPM" ) {
|
---|
464 | // 12-hour clock
|
---|
465 | } else {
|
---|
466 | // 24-hour clock
|
---|
467 | }
|
---|
468 | }
|
---|
469 | </pre>
|
---|
470 |
|
---|
471 | <p> In the example, for the US we would leave the translation of "AMPM" as
|
---|
472 | it is and thereby use the 12-hour clock branch; but in Europe we would
|
---|
473 | translate it as something else (anything else, e.g. "EU") and this
|
---|
474 | will make the code use the 24-hour clock branch.
|
---|
475 | <p> Localizing images is not recommended. Choose clear icons that are
|
---|
476 | appropriate for all localities, rather than relying on local puns or
|
---|
477 | stretched metaphors.
|
---|
478 | <p> <h2> Dynamic Translation
|
---|
479 | </h2>
|
---|
480 | <a name="2"></a><p> Some applications, such as Qt Linguist, must be able to support changes
|
---|
481 | to the user's language settings while they are still running. To make
|
---|
482 | widgets aware of changes to the system language, implement a public
|
---|
483 | slot called <tt>languageChange()</tt> in each widget that needs to be notified.
|
---|
484 | In this slot, you should update the text displayed by widgets using the
|
---|
485 | <a href="qobject.html#tr">QObject::tr</a>(){tr()} function in the usual way; for example:
|
---|
486 | <p> <pre>
|
---|
487 | void MyWidget::languageChange()
|
---|
488 | {
|
---|
489 | titleLabel->setText(tr("Document Title"));
|
---|
490 | ...
|
---|
491 | okPushButton->setText(tr("&OK"));
|
---|
492 | }
|
---|
493 | </pre>
|
---|
494 |
|
---|
495 | <p> The default event handler for <a href="qwidget.html">QWidget</a> subclasses responds to the
|
---|
496 | <a href="qevent.html#Type-enum">LanguageChange</a> event, and will call this slot
|
---|
497 | when necessary; other application components can also connect signals
|
---|
498 | to this slot to force widgets to update themselves.
|
---|
499 | <p> <h2> System Support
|
---|
500 | </h2>
|
---|
501 | <a name="3"></a><p> Some of the operating systems and windowing systems that Qt runs on
|
---|
502 | only have limited support for Unicode. The level of support available
|
---|
503 | in the underlying system has some influence on the support that Qt can
|
---|
504 | provide on those platforms, although in general Qt applications need
|
---|
505 | not be too concerned with platform-specific limitations.
|
---|
506 | <p> <h3> Unix/X11
|
---|
507 | </h3>
|
---|
508 | <a name="3-1"></a><p> <ul>
|
---|
509 | <li> Locale-oriented fonts and input methods. Qt hides these and
|
---|
510 | provides Unicode input and output.
|
---|
511 | <li> Filesystem conventions such as
|
---|
512 | <a href="http://www.ietf.org/rfc/rfc2279.txt">UTF-8</a>
|
---|
513 | are under development
|
---|
514 | in some Unix variants. All Qt file functions allow Unicode,
|
---|
515 | but convert filenames to the local 8-bit encoding, as
|
---|
516 | this is the Unix convention
|
---|
517 | (see <a href="qfile.html#setEncodingFunction">QFile::setEncodingFunction</a>()
|
---|
518 | to explore alternative encodings).
|
---|
519 | <li> File I/O defaults to the local 8-bit encoding,
|
---|
520 | with Unicode options in <a href="qtextstream.html">QTextStream</a>.
|
---|
521 | </ul>
|
---|
522 | <p> <h3> Windows
|
---|
523 | </h3>
|
---|
524 | <a name="3-2"></a><p> <ul>
|
---|
525 | <li> Qt provides full Unicode support, including input methods, fonts,
|
---|
526 | clipboard, drag-and-drop and file names.
|
---|
527 | <li> File I/O defaults to Latin-1, with Unicode options in QTextStream.
|
---|
528 | Note that some Windows programs do not understand big-endian
|
---|
529 | Unicode text files even though that is the order prescribed by
|
---|
530 | the Unicode Standard in the absence of higher-level protocols.
|
---|
531 | <li> Unlike programs written with MFC or plain winlib, Qt programs
|
---|
532 | are portable between Windows 95/98 and Windows NT.
|
---|
533 | <em>You do not need different binaries to support Unicode.</em>
|
---|
534 | </ul>
|
---|
535 | <p> <h2> Note about Locales on X11
|
---|
536 | </h2>
|
---|
537 | <a name="4"></a><p> Many Unix distributions contain only partial support for some locales.
|
---|
538 | For example, if you have a <tt>/usr/share/locale/ja_JP.EUC</tt> directory,
|
---|
539 | this does not necessarily mean you can display Japanese text; you also
|
---|
540 | need JIS encoded fonts (or Unicode fonts), and the <tt>/usr/share/locale/ja_JP.EUC</tt> directory needs to be complete. For best
|
---|
541 | results, use complete locales from your system vendor.
|
---|
542 | <p> <h2> Relevant Qt Classes
|
---|
543 | </h2>
|
---|
544 | <a name="5"></a><p> These classes are relevant to internationalizing Qt applications.
|
---|
545 |
|
---|
546 | <p><table width="100%">
|
---|
547 | <tr bgcolor=#f0f0f0><td><b><a href="qbig5codec.html">QBig5Codec</a></b><td>Conversion to and from the Big5 encoding
|
---|
548 | <tr bgcolor=#f0f0f0><td><b><a href="qeucjpcodec.html">QEucJpCodec</a></b><td>Conversion to and from EUC-JP character sets
|
---|
549 | <tr bgcolor=#f0f0f0><td><b><a href="qeuckrcodec.html">QEucKrCodec</a></b><td>Conversion to and from EUC-KR character sets
|
---|
550 | <tr bgcolor=#f0f0f0><td><b><a href="qgb18030codec.html">QGb18030Codec</a></b><td>Conversion to and from the Chinese GB18030/GBK/GB2312 encoding
|
---|
551 | <tr bgcolor=#f0f0f0><td><b><a href="qgb2312codec.html">QGb2312Codec</a></b><td>Conversion to and from the Chinese GB2312 encoding
|
---|
552 | <tr bgcolor=#f0f0f0><td><b><a href="qgbkcodec.html">QGbkCodec</a></b><td>Conversion to and from the Chinese GBK encoding
|
---|
553 | <tr bgcolor=#f0f0f0><td><b><a href="qhebrewcodec.html">QHebrewCodec</a></b><td>Conversion to and from visually ordered Hebrew
|
---|
554 | <tr bgcolor=#f0f0f0><td><b><a href="qjiscodec.html">QJisCodec</a></b><td>Conversion to and from JIS character sets
|
---|
555 | <tr bgcolor=#f0f0f0><td><b><a href="qsjiscodec.html">QSjisCodec</a></b><td>Conversion to and from Shift-JIS
|
---|
556 | <tr bgcolor=#f0f0f0><td><b><a href="qtextcodec.html">QTextCodec</a></b><td>Conversion between text encodings
|
---|
557 | <tr bgcolor=#f0f0f0><td><b><a href="qtextdecoder.html">QTextDecoder</a></b><td>State-based decoder
|
---|
558 | <tr bgcolor=#f0f0f0><td><b><a href="qtextencoder.html">QTextEncoder</a></b><td>State-based encoder
|
---|
559 | <tr bgcolor=#f0f0f0><td><b><a href="qtranslator.html">QTranslator</a></b><td>Internationalization support for text output
|
---|
560 | <tr bgcolor=#f0f0f0><td><b><a href="qtranslatormessage.html">QTranslatorMessage</a></b><td>Translator message and its properties
|
---|
561 | <tr bgcolor=#f0f0f0><td><b><a href="qtsciicodec.html">QTsciiCodec</a></b><td>Conversion to and from the Tamil TSCII encoding
|
---|
562 | </table>
|
---|
563 | <!-- eof -->
|
---|
564 | <p><address><hr><div align=center>
|
---|
565 | <table width=100% cellspacing=0 border=0><tr>
|
---|
566 | <td>Copyright © 2007
|
---|
567 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
568 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
569 | </table></div></address></body>
|
---|
570 | </html>
|
---|