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/examples/i18n/i18n.doc:4 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>Internationalization</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</h1>
|
---|
33 |
|
---|
34 |
|
---|
35 | <p>
|
---|
36 | This example shows how to internationalize applications. Start it with
|
---|
37 | <pre># i18n de</pre><p> to get a german version and with
|
---|
38 | <pre># i18n en</pre><p> to get the english version.
|
---|
39 | <p> Refer also to <a href="i18n.html">the internationalization documentation</a>.
|
---|
40 | <p> <hr>
|
---|
41 | <p> Header file:
|
---|
42 | <p> <pre>/****************************************************************************
|
---|
43 | ** $Id: i18n-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
44 | **
|
---|
45 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
46 | **
|
---|
47 | ** This file is part of an example program for Qt. This example
|
---|
48 | ** program may be used, distributed and modified without limitation.
|
---|
49 | **
|
---|
50 | *****************************************************************************/
|
---|
51 |
|
---|
52 | #ifndef MYWIDGET_H
|
---|
53 | #define MYWIDGET_H
|
---|
54 |
|
---|
55 | #include <<a href="qmainwindow-h.html">qmainwindow.h</a>>
|
---|
56 | #include <<a href="qstring-h.html">qstring.h</a>>
|
---|
57 |
|
---|
58 | class MyWidget : public <a href="qmainwindow.html">QMainWindow</a>
|
---|
59 | {
|
---|
60 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
|
---|
61 |
|
---|
62 | public:
|
---|
63 | MyWidget( <a href="qwidget.html">QWidget</a>* parent=0, const char* name = 0 );
|
---|
64 |
|
---|
65 | signals:
|
---|
66 | void closed();
|
---|
67 |
|
---|
68 | protected:
|
---|
69 | void closeEvent(QCloseEvent*);
|
---|
70 |
|
---|
71 | private:
|
---|
72 | static void initChoices(QWidget* parent);
|
---|
73 | };
|
---|
74 |
|
---|
75 | #endif
|
---|
76 | </pre>
|
---|
77 |
|
---|
78 | <p> <hr>
|
---|
79 | <p> Implementation:
|
---|
80 | <p> <pre>/****************************************************************************
|
---|
81 | ** $Id: i18n-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
82 | **
|
---|
83 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
84 | **
|
---|
85 | ** This file is part of an example program for Qt. This example
|
---|
86 | ** program may be used, distributed and modified without limitation.
|
---|
87 | **
|
---|
88 | *****************************************************************************/
|
---|
89 |
|
---|
90 | #include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>
|
---|
91 | #include <<a href="qradiobutton-h.html">qradiobutton.h</a>>
|
---|
92 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
93 | #include <<a href="qlistbox-h.html">qlistbox.h</a>>
|
---|
94 | #include <<a href="qcombobox-h.html">qcombobox.h</a>>
|
---|
95 | #include <<a href="qlabel-h.html">qlabel.h</a>>
|
---|
96 | #include <<a href="qhbox-h.html">qhbox.h</a>>
|
---|
97 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
98 | #include <<a href="qaccel-h.html">qaccel.h</a>>
|
---|
99 | #include <<a href="qpopupmenu-h.html">qpopupmenu.h</a>>
|
---|
100 | #include <<a href="qmenubar-h.html">qmenubar.h</a>>
|
---|
101 | #include <<a href="qstatusbar-h.html">qstatusbar.h</a>>
|
---|
102 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
103 |
|
---|
104 | #include "mywidget.h"
|
---|
105 |
|
---|
106 | <a name="f525"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a>* parent, const char* name )
|
---|
107 | : <a href="qmainwindow.html">QMainWindow</a>( parent, name )
|
---|
108 | {
|
---|
109 | <a href="qvbox.html">QVBox</a>* central = new <a href="qvbox.html">QVBox</a>(this);
|
---|
110 | <a name="x1923"></a> central-><a href="qframe.html#setMargin">setMargin</a>( 5 );
|
---|
111 | <a name="x1924"></a> central-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
|
---|
112 | <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(central);
|
---|
113 |
|
---|
114 | <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>(this);
|
---|
115 | file-><a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("E&xit"), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()),
|
---|
116 | <a name="x1921"></a> QAccel::<a href="qaccel.html#stringToKey">stringToKey</a>(<a href="qobject.html#tr">tr</a>("Ctrl+Q")) );
|
---|
117 | <a href="qmainwindow.html#menuBar">menuBar</a>()->insertItem( <a href="qobject.html#tr">tr</a>("&File"), file );
|
---|
118 |
|
---|
119 | <a href="qwidget.html#setCaption">setCaption</a>( <a href="qobject.html#tr">tr</a>( "Internationalization Example" ) );
|
---|
120 |
|
---|
121 | <a href="qstring.html">QString</a> l;
|
---|
122 | <a href="qmainwindow.html#statusBar">statusBar</a>()->message( <a href="qobject.html#tr">tr</a>("Language: English") );
|
---|
123 |
|
---|
124 | ( void )new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "The Main Window" ), central );
|
---|
125 |
|
---|
126 | <a href="qbuttongroup.html">QButtonGroup</a>* gbox = new <a href="qbuttongroup.html">QButtonGroup</a>( 1, QGroupBox::Horizontal,
|
---|
127 | <a href="qobject.html#tr">tr</a>( "View" ), central );
|
---|
128 | (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Perspective" ), gbox );
|
---|
129 | (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Isometric" ), gbox );
|
---|
130 | (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Oblique" ), gbox );
|
---|
131 |
|
---|
132 | initChoices(central);
|
---|
133 | }
|
---|
134 |
|
---|
135 | static const char* choices[] = {
|
---|
136 | QT_TRANSLATE_NOOP( "MyWidget", "First" ),
|
---|
137 | QT_TRANSLATE_NOOP( "MyWidget", "Second" ),
|
---|
138 | QT_TRANSLATE_NOOP( "MyWidget", "Third" ),
|
---|
139 | 0
|
---|
140 | };
|
---|
141 |
|
---|
142 | void <a name="f526"></a>MyWidget::initChoices(QWidget* parent)
|
---|
143 | {
|
---|
144 | <a href="qlistbox.html">QListBox</a>* lb = new <a href="qlistbox.html">QListBox</a>( parent );
|
---|
145 | for ( int i = 0; choices[i]; i++ )
|
---|
146 | <a name="x1925"></a> lb-><a href="qlistbox.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( choices[i] ) );
|
---|
147 | }
|
---|
148 |
|
---|
149 | <a name="x1927"></a>void MyWidget::<a href="qwidget.html#closeEvent">closeEvent</a>(QCloseEvent* e)
|
---|
150 | {
|
---|
151 | QWidget::<a href="qwidget.html#closeEvent">closeEvent</a>(e);
|
---|
152 | emit closed();
|
---|
153 | }
|
---|
154 | </pre>
|
---|
155 |
|
---|
156 | <p> <hr>
|
---|
157 | <p> Main:
|
---|
158 | <p> <pre>/****************************************************************************
|
---|
159 | ** $Id: i18n-example.html 2051 2007-02-21 10:04:20Z chehrlic $
|
---|
160 | **
|
---|
161 | ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
|
---|
162 | **
|
---|
163 | ** This file is part of an example program for Qt. This example
|
---|
164 | ** program may be used, distributed and modified without limitation.
|
---|
165 | **
|
---|
166 | *****************************************************************************/
|
---|
167 |
|
---|
168 | #include <<a href="qapplication-h.html">qapplication.h</a>>
|
---|
169 | #include <<a href="qtranslator-h.html">qtranslator.h</a>>
|
---|
170 | #include <<a href="qfileinfo-h.html">qfileinfo.h</a>>
|
---|
171 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>>
|
---|
172 | #include <<a href="qcheckbox-h.html">qcheckbox.h</a>>
|
---|
173 | #include <<a href="qvbox-h.html">qvbox.h</a>>
|
---|
174 | #include <<a href="qlayout-h.html">qlayout.h</a>>
|
---|
175 | #include <<a href="qbuttongroup-h.html">qbuttongroup.h</a>>
|
---|
176 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>>
|
---|
177 | #include <<a href="qsignalmapper-h.html">qsignalmapper.h</a>>
|
---|
178 | #include <<a href="qtextcodec-h.html">qtextcodec.h</a>>
|
---|
179 | #include <stdlib.h>
|
---|
180 |
|
---|
181 | #if defined(Q_OS_UNIX)
|
---|
182 | #include <unistd.h>
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | #include "mywidget.h"
|
---|
186 |
|
---|
187 | //#define USE_I18N_FONT
|
---|
188 |
|
---|
189 | class QVDialog : public <a href="qdialog.html">QDialog</a> {
|
---|
190 | public:
|
---|
191 | QVDialog(QWidget *parent=0, const char *name=0, bool modal=FALSE,
|
---|
192 | WFlags f=0) : <a href="qdialog.html">QDialog</a>(parent,name,modal,f)
|
---|
193 | {
|
---|
194 | <a href="qvboxlayout.html">QVBoxLayout</a>* vb = new <a href="qvboxlayout.html">QVBoxLayout</a>(this,8);
|
---|
195 | <a name="x1938"></a> vb-><a href="qlayout.html#setAutoAdd">setAutoAdd</a>(TRUE);
|
---|
196 | hb = 0;
|
---|
197 | sm = new <a href="qsignalmapper.html">QSignalMapper</a>(this);
|
---|
198 | <a name="x1946"></a> connect(sm,SIGNAL(<a href="qsignalmapper.html#mapped">mapped</a>(int)),this,SLOT(done(int)));
|
---|
199 | }
|
---|
200 | void addButtons( const <a href="qstring.html">QString</a>& cancel=QString::null,
|
---|
201 | const <a href="qstring.html">QString</a>& ok=QString::null,
|
---|
202 | const <a href="qstring.html">QString</a>& mid1=QString::null,
|
---|
203 | const <a href="qstring.html">QString</a>& mid2=QString::null,
|
---|
204 | const <a href="qstring.html">QString</a>& mid3=QString::null)
|
---|
205 | {
|
---|
206 | <a name="x1948"></a> addButton(ok.<a href="qstring.html#isNull">isNull</a>() ? QObject::tr("OK") : ok, 1);
|
---|
207 | if ( !mid1.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid1,2);
|
---|
208 | if ( !mid2.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid2,3);
|
---|
209 | if ( !mid3.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid3,4);
|
---|
210 | addButton(cancel.<a href="qstring.html#isNull">isNull</a>() ? QObject::tr("Cancel") : cancel, 0);
|
---|
211 | }
|
---|
212 |
|
---|
213 | void addButton( const <a href="qstring.html">QString</a>& text, int result )
|
---|
214 | {
|
---|
215 | if ( !hb )
|
---|
216 | hb = new <a href="qhbox.html">QHBox</a>(this);
|
---|
217 | <a href="qpushbutton.html">QPushButton</a> *c = new <a href="qpushbutton.html">QPushButton</a>(text, hb);
|
---|
218 | <a name="x1947"></a> sm-><a href="qsignalmapper.html#setMapping">setMapping</a>(c,result);
|
---|
219 | <a name="x1945"></a> connect(c,SIGNAL(<a href="qbutton.html#clicked">clicked</a>()),sm,SLOT(<a href="qsignalmapper.html#map">map</a>()));
|
---|
220 | }
|
---|
221 |
|
---|
222 | private:
|
---|
223 | <a href="qsignalmapper.html">QSignalMapper</a> *sm;
|
---|
224 | <a href="qhbox.html">QHBox</a> *hb;
|
---|
225 | };
|
---|
226 |
|
---|
227 | MyWidget* showLang(QString lang)
|
---|
228 | {
|
---|
229 |
|
---|
230 | static QTranslator *translator = 0;
|
---|
231 |
|
---|
232 | <a name="x1934"></a> qApp-><a href="qapplication.html#setPalette">setPalette</a>(QPalette(QColor(220-rand()%64,220-rand()%64,220-rand()%64)));
|
---|
233 |
|
---|
234 | lang = "mywidget_" + lang + ".qm";
|
---|
235 | <a href="qfileinfo.html">QFileInfo</a> fi( lang );
|
---|
236 |
|
---|
237 | <a name="x1937"></a> if ( !fi.<a href="qfileinfo.html#exists">exists</a>() ) {
|
---|
238 | <a name="x1940"></a> QMessageBox::<a href="qmessagebox.html#warning">warning</a>( 0, "File error",
|
---|
239 | QString("Cannot find translation for language: "+lang+
|
---|
240 | "\n(try eg. 'de', 'ko' or 'no')") );
|
---|
241 | return 0;
|
---|
242 | }
|
---|
243 | if ( translator ) {
|
---|
244 | <a name="x1932"></a> qApp-><a href="qapplication.html#removeTranslator">removeTranslator</a>( translator );
|
---|
245 | delete translator;
|
---|
246 | }
|
---|
247 | translator = new <a href="qtranslator.html">QTranslator</a>( 0 );
|
---|
248 | <a name="x1950"></a> translator-><a href="qtranslator.html#load">load</a>( lang, "." );
|
---|
249 | <a name="x1930"></a> qApp-><a href="qapplication.html#installTranslator">installTranslator</a>( translator );
|
---|
250 | MyWidget *m = new MyWidget;
|
---|
251 | <a name="x1951"></a> m-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - <a href="i18n.html#i18n">i18n</a> - " + m-><a href="qwidget.html#caption">caption</a>() );
|
---|
252 | return m;
|
---|
253 | }
|
---|
254 |
|
---|
255 | int main( int argc, char** argv )
|
---|
256 | {
|
---|
257 | <a href="qapplication.html">QApplication</a> app( argc, argv );
|
---|
258 |
|
---|
259 | const char* qm[]=
|
---|
260 | { "ar", "cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh", 0 };
|
---|
261 |
|
---|
262 | #if defined(Q_OS_UNIX)
|
---|
263 | srand( getpid() << 2 );
|
---|
264 | #endif
|
---|
265 |
|
---|
266 | <a href="qstring.html">QString</a> lang;
|
---|
267 | if ( argc == 2 )
|
---|
268 | lang = argv[1];
|
---|
269 |
|
---|
270 | if ( argc != 2 || lang == "all" ) {
|
---|
271 | QVDialog dlg(0,0,TRUE);
|
---|
272 | QCheckBox* qmb[sizeof(qm)/sizeof(qm[0])];
|
---|
273 | int r;
|
---|
274 | if ( lang == "all" ) {
|
---|
275 | r = 2;
|
---|
276 | } else {
|
---|
277 | <a href="qbuttongroup.html">QButtonGroup</a> *bg = new <a href="qbuttongroup.html">QButtonGroup</a>(4,Qt::Vertical,"Choose Locales",&dlg);
|
---|
278 | <a name="x1949"></a> <a href="qstring.html">QString</a> loc = QTextCodec::<a href="qtextcodec.html#locale">locale</a>();
|
---|
279 | for ( int i=0; qm[i]; i++ ) {
|
---|
280 | qmb[i] = new <a href="qcheckbox.html">QCheckBox</a>((const char*)qm[i],bg);
|
---|
281 | qmb[i]->setChecked( loc == qm[i] );
|
---|
282 | }
|
---|
283 | dlg.addButtons("Cancel","OK","All");
|
---|
284 | <a name="x1936"></a> r = dlg.<a href="qdialog.html#exec">exec</a>();
|
---|
285 | }
|
---|
286 | if ( r ) {
|
---|
287 | <a name="x1928"></a> <a href="qrect.html">QRect</a> screen = qApp-><a href="qapplication.html#desktop">desktop</a>()->availableGeometry();
|
---|
288 | bool tight = screen.<a href="qrect.html#width">width</a>() < 1024;
|
---|
289 | <a name="x1942"></a> int x=screen.<a href="qrect.html#left">left</a>()+5;
|
---|
290 | int y=screen.<a href="qrect.html#top">top</a>()+25;
|
---|
291 | for ( int i=0; qm[i]; i++ ) {
|
---|
292 | if ( r == 2 || qmb[i]->isChecked() ) {
|
---|
293 | MyWidget* w = showLang((const char*)qm[i]);
|
---|
294 |
|
---|
295 | if( w == 0 ) exit( 0 );
|
---|
296 | QObject::<a href="qobject.html#connect">connect</a>(w, SIGNAL(closed()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()));
|
---|
297 | <a name="x1953"></a> w-><a href="qwidget.html#setGeometry">setGeometry</a>(x,y,197,356);
|
---|
298 | w-><a href="qwidget.html#show">show</a>();
|
---|
299 | if ( tight ) {
|
---|
300 | x += 8;
|
---|
301 | y += 8;
|
---|
302 | } else {
|
---|
303 | x += 205;
|
---|
304 | if ( x > 1000 ) {
|
---|
305 | x = 5;
|
---|
306 | y += 384;
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | } else {
|
---|
312 | exit( 0 );
|
---|
313 | }
|
---|
314 | } else {
|
---|
315 | <a href="qstring.html">QString</a> lang = argv[1];
|
---|
316 | <a href="qwidget.html">QWidget</a>* m = showLang(lang);
|
---|
317 | app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( m );
|
---|
318 | m-><a href="qwidget.html#setCaption">setCaption</a>("Qt Example - i18n");
|
---|
319 | m-><a href="qwidget.html#show">show</a>();
|
---|
320 | }
|
---|
321 |
|
---|
322 | #ifdef USE_I18N_FONT
|
---|
323 | memorymanager->savePrerenderedFont(font.handle(),FALSE);
|
---|
324 | #endif
|
---|
325 |
|
---|
326 | // While we run "all", kill them all
|
---|
327 | return app.<a href="qapplication.html#exec">exec</a>();
|
---|
328 |
|
---|
329 | }
|
---|
330 | </pre>
|
---|
331 |
|
---|
332 | <p>See also <a href="examples.html">Examples</a>.
|
---|
333 |
|
---|
334 | <!-- eof -->
|
---|
335 | <p><address><hr><div align=center>
|
---|
336 | <table width=100% cellspacing=0 border=0><tr>
|
---|
337 | <td>Copyright © 2007
|
---|
338 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
339 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
340 | </table></div></address></body>
|
---|
341 | </html>
|
---|