Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Fonctions  · 

Internationalization

This example shows how to internationalize applications. Start it with

# i18n de

to get a german version and with

# i18n en

to get the english version.

Refer also to the internationalization documentation.


Header file:

/****************************************************************************
** $Id:  qt/mywidget.h   3.0.6   edited Oct 12 2001 $
**
** 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 <qmainwindow.h>
#include <qstring.h>

class MyWidget : public QMainWindow
{
    Q_OBJECT

public:
    MyWidget( QWidget* parent=0, const char* name = 0 );

signals:
    void closed();

protected:
    void closeEvent(QCloseEvent*);

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

#endif


Implementation:

/****************************************************************************
** $Id:  qt/mywidget.cpp   3.0.6   edited Oct 12 2001 $
**
** 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 <qbuttongroup.h>
#include <qradiobutton.h>
#include <qlabel.h>
#include <qlistbox.h>
#include <qcombobox.h>
#include <qlabel.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qaccel.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qstatusbar.h>
#include <qapplication.h>

#include "mywidget.h"

MyWidget::MyWidget( QWidget* parent, const char* name )
        : QMainWindow( parent, name )
{
    QVBox* central = new QVBox(this);
    central->setMargin( 5 );
    central->setSpacing( 5 );
    setCentralWidget(central);

    QPopupMenu* file = new QPopupMenu(this);
    file->insertItem( tr("E&xit"), qApp, SLOT(quit()),
            QAccel::stringToKey(tr("Ctrl+Q")) );
    menuBar()->insertItem( tr("&File"), file );

    setCaption( tr( "Internationalization Example" ) );

    QString l;
    statusBar()->message( tr("Language: English") );

    ( void )new QLabel( tr( "The Main Window" ), central );

    QButtonGroup* gbox = new QButtonGroup( 1, QGroupBox::Horizontal,
                                      tr( "View" ), central );
    (void)new QRadioButton( tr( "Perspective" ), gbox );
    (void)new QRadioButton( tr( "Isometric" ), gbox );
    (void)new QRadioButton( tr( "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 MyWidget::initChoices(QWidget* parent)
{
    QListBox* lb = new QListBox( parent );
    for ( int i = 0; choices[i]; i++ )
        lb->insertItem( tr( choices[i] ) );
}

void MyWidget::closeEvent(QCloseEvent* e)
{
    QWidget::closeEvent(e);
    emit closed();
}


Main:

/****************************************************************************
** $Id:  qt/main.cpp   3.0.6   edited Dec 6 2001 $
**
** 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 <qapplication.h>
#include <qtranslator.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include <qcheckbox.h>
#include <qvbox.h>
#include <qlayout.h>
#include <qbuttongroup.h>
#include <qpushbutton.h>
#include <qsignalmapper.h>
#include <qtextcodec.h>
#include <stdlib.h>

#if defined(Q_OS_UNIX)
#include <unistd.h>
#endif

#include "mywidget.h"

//#define USE_I18N_FONT

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

    void addButton( const QString& text, int result )
    {
        if ( !hb )
            hb = new QHBox(this);
        QPushButton *c = new QPushButton(text, hb);
        sm->setMapping(c,result);
        connect(c,SIGNAL(clicked()),sm,SLOT(map()));
    }

private:
    QSignalMapper *sm;
    QHBox *hb;
};

MyWidget* showLang(QString lang)
{

    static QTranslator *translator = 0;

    qApp->setPalette(QPalette(QColor(220-rand()%64,220-rand()%64,220-rand()%64)));

    lang = "mywidget_" + lang + ".qm";
    QFileInfo fi( lang );

    if ( !fi.exists() ) {
        QMessageBox::warning( 0, "File error",
                              QString("Cannot find translation for language: "+lang+
                                      "\n(try eg. 'de', 'ko' or 'no')") );
        return 0;
    }
    if ( translator ) {
        qApp->removeTranslator( translator );
        delete translator;
    }
    translator = new QTranslator( 0 );
    translator->load( lang, "." );
    qApp->installTranslator( translator );
    MyWidget *m = new MyWidget;
    m->setCaption("Qt Example - i18n - " + m->caption() );
    return m;
}

int main( int argc, char** argv )
{
    QApplication 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() << 2 );
#endif

    QString 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 {
            QButtonGroup *bg = new QButtonGroup(4,Qt::Vertical,"Choose Locales",&dlg);
            QString loc = QTextCodec::locale();
            for ( int i=0; qm[i]; i++ ) {
                qmb[i] = new QCheckBox((const char*)qm[i],bg);
                qmb[i]->setChecked( loc == qm[i] );
            }
            dlg.addButtons("Cancel","OK","All");
            r = dlg.exec();
        }
        if ( r ) {
            bool tight = qApp->desktop()->screen()->width() < 1024;
            int x=5;
            int y=25;
            for ( int i=0; qm[i]; i++ ) {
                if ( r == 2 || qmb[i]->isChecked() ) {
                    MyWidget* w = showLang((const char*)qm[i]);

                    if( w == 0 ) exit( 0 );
                    QObject::connect(w, SIGNAL(closed()), qApp, SLOT(quit()));
                    w->setGeometry(x,y,197,356);
                    w->show();
                    if ( tight ) {
                        x += 8;
                        y += 8;
                    } else {
                        x += 205;
                        if ( x > 1000 ) {
                            x = 5;
                            y += 384;
                        }
                    }
                }
            }
        } else {
            exit( 0 );
        }
    } else {
        QString lang = argv[1];
        QWidget* m = showLang(lang);
        app.setMainWidget( m );
        m->setCaption("Qt Example - i18n");
        m->show();
    }

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

    // While we run "all", kill them all
    return app.exec();

}

See also Examples.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 3.0
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web