Qt logo

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. Also see the internationalization documentation.
Header file:
/****************************************************************************
** $Id: mywidget.h,v 1.2 1999/06/15 21:42:35 warwick Exp $
**
** Copyright (C) 1992-1999 Troll Tech 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 );

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

#endif

Implementation:
/****************************************************************************
** $Id: mywidget.cpp,v 1.3 1999/06/15 22:30:33 warwick Exp $
**
** Copyright (C) 1992-1999 Troll Tech 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] ) ); 
}

Main:
/****************************************************************************
** $Id: main.cpp,v 1.5 1999/06/16 00:41:13 warwick Exp $
**
** Copyright (C) 1992-1999 Troll Tech 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 "mywidget.h"

main( int argc, char** argv )
{
    QApplication app( argc, argv );

    QString lang;
    if ( argc != 2 ) {
        int i = QMessageBox::information(0, "Language?", "Which language?",
                    "Deutsch", "English", "Norsk" );
        switch ( i ) {
          case 0: lang = "de"; break;
          case 1: lang = "en"; break;
          case 2: lang = "no"; break;
        }
    } else {
        lang = argv[1];
    }

    QString lfile = "mywidget_" + lang + ".qm";

    QFileInfo fi( lfile );
    if ( !fi.exists() ) {
        QMessageBox::warning( 0, "File error",
                  QString("Cannot find translation for language: "+lang+
                  "\n(try 'de', 'en' or 'no')") );
        return 0;
    }

    QTranslator translator( 0 );
    translator.load( lfile, "." );
    app.installTranslator( &translator );

    MyWidget m;
    m.resize( 400, 300 );
    app.setMainWidget( &m );
    m.show();

    return app.exec();
}


Copyright 1999 Troll TechTrademarks
Qt version 2.0.2