Sophie

Sophie

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

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/examples/i18n/i18n.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Internationalization</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</h1>

   
<p> 
This example shows how to internationalize applications. Start it with
<pre># i18n de</pre><p> to get a german version and with
<pre># i18n en</pre><p> to get the english version.
<p> Refer also to <a href="i18n.html">the internationalization documentation</a>.
<p> <hr>
<p> Header file:
<p> <pre>/****************************************************************************
** $Id:  qt/mywidget.h   3.0.2   edited Oct 12 12:18 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;

class MyWidget : public <a href="qmainwindow.html">QMainWindow</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>

public:
    MyWidget( <a href="qwidget.html">QWidget</a>* parent=0, const char* name = 0 );

signals:
    void closed();

protected:
    void closeEvent(QCloseEvent*);

private:
    static void initChoices(QWidget* parent);
};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** $Id:  qt/mywidget.cpp   3.0.2   edited Oct 12 12:18 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qbuttongroup-h.html">qbuttongroup.h</a>&gt;
#include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qlistbox-h.html">qlistbox.h</a>&gt;
#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
#include &lt;<a href="qaccel-h.html">qaccel.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;

#include "mywidget.h"

<a name="f406"></a>MyWidget::MyWidget( <a href="qwidget.html">QWidget</a>* parent, const char* name )
        : <a href="qmainwindow.html">QMainWindow</a>( parent, name )
{
    <a href="qvbox.html">QVBox</a>* central = new <a href="qvbox.html">QVBox</a>(this);
<a name="x1358"></a>    central-&gt;<a href="qframe.html#setMargin">setMargin</a>( 5 );
<a name="x1359"></a>    central-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
    <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>(central);

    <a href="qpopupmenu.html">QPopupMenu</a>* file = new <a href="qpopupmenu.html">QPopupMenu</a>(this);
    file-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>("E&amp;xit"), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()),
<a name="x1356"></a>            QAccel::<a href="qaccel.html#stringToKey">stringToKey</a>(<a href="qobject.html#tr">tr</a>("Ctrl+Q")) );
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( <a href="qobject.html#tr">tr</a>("&amp;File"), file );

    <a href="qwidget.html#setCaption">setCaption</a>( <a href="qobject.html#tr">tr</a>( "Internationalization Example" ) );

    <a href="qstring.html">QString</a> l;
    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( <a href="qobject.html#tr">tr</a>("Language: English") );

    ( void )new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "The Main Window" ), central );

    <a href="qbuttongroup.html">QButtonGroup</a>* gbox = new <a href="qbuttongroup.html">QButtonGroup</a>( 1, QGroupBox::Horizontal,
                                      <a href="qobject.html#tr">tr</a>( "View" ), central );
    (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Perspective" ), gbox );
    (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Isometric" ), gbox );
    (void)new <a href="qradiobutton.html">QRadioButton</a>( <a href="qobject.html#tr">tr</a>( "Oblique" ), gbox );

    initChoices(central);
}

static const char* choices[] = {
    QT_TRANSLATE_NOOP( "MyWidget", "First" ),
    QT_TRANSLATE_NOOP( "MyWidget", "Second" ),
    QT_TRANSLATE_NOOP( "MyWidget", "Third" ),
    0
};

void <a name="f407"></a>MyWidget::initChoices(QWidget* parent)
{
    <a href="qlistbox.html">QListBox</a>* lb = new <a href="qlistbox.html">QListBox</a>( parent );
    for ( int i = 0; choices[i]; i++ )
<a name="x1360"></a>        lb-&gt;<a href="qlistbox.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( choices[i] ) );
}

<a name="x1362"></a>void MyWidget::<a href="qwidget.html#closeEvent">closeEvent</a>(QCloseEvent* e)
{
    QWidget::<a href="qwidget.html#closeEvent">closeEvent</a>(e);
    emit closed();
}
</pre>

<p> <hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/main.cpp   3.0.2   edited Dec 6 13:41 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qtranslator-h.html">qtranslator.h</a>&gt;
#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qcheckbox-h.html">qcheckbox.h</a>&gt;
#include &lt;<a href="qvbox-h.html">qvbox.h</a>&gt;
#include &lt;<a href="qlayout-h.html">qlayout.h</a>&gt;
#include &lt;<a href="qbuttongroup-h.html">qbuttongroup.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qsignalmapper-h.html">qsignalmapper.h</a>&gt;
#include &lt;<a href="qtextcodec-h.html">qtextcodec.h</a>&gt;
#include &lt;stdlib.h&gt;

#if defined(Q_OS_UNIX)
#include &lt;unistd.h&gt;
#endif

#include "mywidget.h"

//#define USE_I18N_FONT

class QVDialog : public <a href="qdialog.html">QDialog</a> {
public:
    QVDialog(QWidget *parent=0, const char *name=0, bool modal=FALSE,
             WFlags f=0) : <a href="qdialog.html">QDialog</a>(parent,name,modal,f)
    {
        <a href="qvboxlayout.html">QVBoxLayout</a>* vb = new <a href="qvboxlayout.html">QVBoxLayout</a>(this,8);
<a name="x1373"></a>        vb-&gt;<a href="qlayout.html#setAutoAdd">setAutoAdd</a>(TRUE);
        hb = 0;
        sm = new <a href="qsignalmapper.html">QSignalMapper</a>(this);
<a name="x1377"></a>        connect(sm,SIGNAL(<a href="qsignalmapper.html#mapped">mapped</a>(int)),this,SLOT(done(int)));
    }
    void addButtons( const <a href="qstring.html">QString</a>&amp; cancel=QString::null,
                    const <a href="qstring.html">QString</a>&amp; ok=QString::null,
                    const <a href="qstring.html">QString</a>&amp; mid1=QString::null,
                    const <a href="qstring.html">QString</a>&amp; mid2=QString::null,
                    const <a href="qstring.html">QString</a>&amp; mid3=QString::null)
    {
<a name="x1379"></a>        addButton(ok.<a href="qstring.html#isNull">isNull</a>() ? tr("OK") : ok, 1);
        if ( !mid1.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid1,2);
        if ( !mid2.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid2,3);
        if ( !mid3.<a href="qstring.html#isNull">isNull</a>() ) addButton(mid3,4);
        addButton(cancel.<a href="qstring.html#isNull">isNull</a>() ? tr("Cancel") : cancel, 0);
    }

    void addButton( const <a href="qstring.html">QString</a>&amp; text, int result )
    {
        if ( !hb )
            hb = new <a href="qhbox.html">QHBox</a>(this);
        <a href="qpushbutton.html">QPushButton</a> *c = new <a href="qpushbutton.html">QPushButton</a>(text, hb);
<a name="x1378"></a>        sm-&gt;<a href="qsignalmapper.html#setMapping">setMapping</a>(c,result);
<a name="x1376"></a>        connect(c,SIGNAL(<a href="qbutton.html#clicked">clicked</a>()),sm,SLOT(<a href="qsignalmapper.html#map">map</a>()));
    }

private:
    <a href="qsignalmapper.html">QSignalMapper</a> *sm;
    <a href="qhbox.html">QHBox</a> *hb;
};

MyWidget* showLang(QString lang)
{

    static QTranslator *translator = 0;

<a name="x1369"></a>    qApp-&gt;<a href="qapplication.html#setPalette">setPalette</a>(QPalette(QColor(220-rand()%64,220-rand()%64,220-rand()%64)));

    lang = "mywidget_" + lang + ".qm";
    <a href="qfileinfo.html">QFileInfo</a> fi( lang );

<a name="x1372"></a>    if ( !fi.<a href="qfileinfo.html#exists">exists</a>() ) {
<a name="x1374"></a>        QMessageBox::<a href="qmessagebox.html#warning">warning</a>( 0, "File error",
                              QString("Cannot find translation for language: "+lang+
                                      "\n(try eg. 'de', 'ko' or 'no')") );
        return 0;
    }
    if ( translator ) {
<a name="x1367"></a>        qApp-&gt;<a href="qapplication.html#removeTranslator">removeTranslator</a>( translator );
        delete translator;
    }
    translator = new <a href="qtranslator.html">QTranslator</a>( 0 );
<a name="x1381"></a>    translator-&gt;<a href="qtranslator.html#load">load</a>( lang, "." );
<a name="x1365"></a>    qApp-&gt;<a href="qapplication.html#installTranslator">installTranslator</a>( translator );
    MyWidget *m = new MyWidget;
<a name="x1382"></a>    m-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - i18n - " + m-&gt;<a href="qwidget.html#caption">caption</a>() );
    return m;
}

int main( int argc, char** argv )
{
    <a href="qapplication.html">QApplication</a> app( argc, argv );

    const char* qm[]=
        { "cs", "de", "el", "en", "eo", "fr", "it", "jp", "ko", "no", "ru", "zh", 0 };

#if defined(Q_OS_UNIX)
    srand( getpid() &lt;&lt; 2 );
#endif

    <a href="qstring.html">QString</a> lang;
    if ( argc == 2 )
        lang = argv[1];

    if ( argc != 2 || lang == "all" ) {
        QVDialog dlg(0,0,TRUE);
        QCheckBox* qmb[sizeof(qm)/sizeof(qm[0])];
        int r;
        if ( lang == "all" ) {
            r = 2;
        } else {
            <a href="qbuttongroup.html">QButtonGroup</a> *bg = new <a href="qbuttongroup.html">QButtonGroup</a>(4,Qt::Vertical,"Choose Locales",&amp;dlg);
<a name="x1380"></a>            <a href="qstring.html">QString</a> loc = QTextCodec::<a href="qtextcodec.html#locale">locale</a>();
            for ( int i=0; qm[i]; i++ ) {
                qmb[i] = new <a href="qcheckbox.html">QCheckBox</a>((const char*)qm[i],bg);
                qmb[i]-&gt;setChecked( loc == qm[i] );
            }
            dlg.addButtons("Cancel","OK","All");
<a name="x1371"></a>            r = dlg.<a href="qdialog.html#exec">exec</a>();
        }
        if ( r ) {
<a name="x1363"></a>            bool tight = qApp-&gt;<a href="qapplication.html#desktop">desktop</a>()-&gt;screen()-&gt;width() &lt; 1024;
            int x=5;
            int y=25;
            for ( int i=0; qm[i]; i++ ) {
                if ( r == 2 || qmb[i]-&gt;isChecked() ) {
                    MyWidget* w = showLang((const char*)qm[i]);

                    if( w == 0 ) exit( 0 );
                    QObject::<a href="qobject.html#connect">connect</a>(w, SIGNAL(closed()), qApp, SLOT(<a href="qapplication.html#quit">quit</a>()));
<a name="x1384"></a>                    w-&gt;<a href="qwidget.html#setGeometry">setGeometry</a>(x,y,197,356);
                    w-&gt;<a href="qwidget.html#show">show</a>();
                    if ( tight ) {
                        x += 8;
                        y += 8;
                    } else {
                        x += 205;
                        if ( x &gt; 1000 ) {
                            x = 5;
                            y += 384;
                        }
                    }
                }
            }
        } else {
            exit( 0 );
        }
    } else {
        <a href="qstring.html">QString</a> lang = argv[1];
        <a href="qwidget.html">QWidget</a>* m = showLang(lang);
        app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( m );
        m-&gt;<a href="qwidget.html#setCaption">setCaption</a>("Qt Example - i18n");
        m-&gt;<a href="qwidget.html#show">show</a>();
    }

#ifdef USE_I18N_FONT
    memorymanager-&gt;savePrerenderedFont(font.handle(),FALSE);
#endif

    // While we run "all", kill them all
    return app.<a href="qapplication.html#exec">exec</a>();

}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- 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>