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  · 

QTranslator Class Reference

The QTranslator class provides internationalization support for text output. More...

#include <qtranslator.h>

Inherits QObject.

List of all member functions.

Public Members

  • QTranslator ( QObject * parent = 0, const char * name = 0 )
  • ~QTranslator ()
  • QString find ( const char * context, const char * sourceText, const char * comment = 0 ) const  (obsolete)
  • virtual QTranslatorMessage findMessage ( const char * context, const char * sourceText, const char * comment = 0 ) const
  • bool load ( const QString & filename, const QString & directory = QString::null, const QString & search_delimiters = QString::null, const QString & suffix = QString::null )
  • bool load ( const uchar * data, int len )
  • void clear ()
  • enum SaveMode { Everything, Stripped }
  • bool save ( const QString & filename, SaveMode mode = Everything )
  • void insert ( const QTranslatorMessage & message )
  • void insert ( const char * context, const char * sourceText, const QString & translation )  (obsolete)
  • void remove ( const QTranslatorMessage & message )
  • void remove ( const char * context, const char * sourceText )  (obsolete)
  • bool contains ( const char * context, const char * sourceText, const char * comment = 0 ) const
  • void squeeze ( SaveMode mode = Everything )
  • void unsqueeze ()
  • QValueList<QTranslatorMessage> messages () const
  • bool isEmpty () const

Detailed Description

The QTranslator class provides internationalization support for text output.

An object of this class contains a set of QTranslatorMessage objects, each of which specifies a translation from a source language to a target language. QTranslator provides functions to look up translations, add new ones, remove them, load and save them, etc.

The most common use of QTranslator is to: load a translator file created with Qt Linguist, install it using QApplication::installTranslator(), and use it via QObject::tr(). For example:

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

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

        MyWidget m;
        app.setMainWidget( &m );
        m.show();

        return app.exec();
    }
    
Note that the translator must be created before the application's main window.

Most applications will never need to do anything else with this class. The other functions provided by this class are useful for applications that work on translator files.

We call a translation a "messsage". For this reason, translation files are sometimes referred to as "message files".

It is possible to lookup a translation using findMessage() (as tr() and QApplication::translate() do) and contains(), to insert a new translation messsage using insert(), and to remove one using remove().

Translation tools often need more information than the bare source text and translation, for example, context information to help the translator. But end-user programs that are using translations usually only need lookup. To cater for these different needs, QTranslator can use stripped translator files that use the minimum of memory and which support little more functionality than findMessage().

Thus, load() may not load enough information to make anything more than findMessage() work. save() has an argument indicating whether to save just this minimum of information or to save everything.

"Everything" means that for each translation item the following information is kept:

  • The translated text - the return value from tr().
  • The input key:
    • The source text - usually the argument to tr().
    • The context - usually the class name for the tr() caller.
    • The comment - a comment that helps disambiguate different uses of the same text in the same context.

The minimum for each item is just the information necessary for findMessage() to return the right text. This may include the source, context and comment, but usually it is just a hash value and the translated text.

For example, the "Cancel" in a dialog might have "Anuluj" when the program runs in Polish (in this case the source text would be "Cancel"). The context would (normally) be the dialog's class name; there would normally be no comment, and the translated text would be "Anuluj".

But it's not always so simple. The Spanish version of a printer dialog with settings for two-sided printing and binding would probably require both "Activado" and "Activada" as translations for "Enabled". In this case the source text would be "Enabled" in both cases, and the context would be the dialog's class name, but the two items would have disambiguating comments such as "two-sided printing" for one and "binding" for the other. The comment enables the translator to choose the appropriate gender for the Spanish version, and enables Qt to distinguish between translations.

Note that when QTranslator loads a stripped file, most functions do not work. The functions that do work with stripped files are explicitly documented as such.

See also QTranslatorMessage, QApplication::installTranslator(), QApplication::removeTranslator(), QObject::tr(), QApplication::translate(), Environment Classes, and Internationalization with Qt.


Member Type Documentation

QTranslator::SaveMode

This enum type defines how QTranslator writes translation files. There are two modes:

  • QTranslator::Everything - files are saved with all available information
  • QTranslator::Stripped - files are saved with just enough information for end-user applications

Note that when QTranslator loads a stripped file, most functions do not work. The functions that do work with stripped files are explicitly documented as such.


Member Function Documentation

QTranslator::QTranslator ( QObject * parent = 0, const char * name = 0 )

Constructs an empty message file object that is not connected to any file. The object is called name with parent parent.

QTranslator::~QTranslator ()

Destroys the object and frees any allocated resources.

void QTranslator::clear ()

Empties this translator of all contents.

This function works with stripped translator files.

bool QTranslator::contains ( const char * context, const char * sourceText, const char * comment = 0 ) const

Returns TRUE if this message file contains a message with the key (context, sourceText, comment); otherwise returns FALSE.

This function works with stripped translator files.

(This is is a one-liner that calls findMessage().)

QString QTranslator::find ( const char * context, const char * sourceText, const char * comment = 0 ) const

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Please use findMessage() instead.

Returns the translation for the key (context, sourceText, comment) or QString::null if there is none in this translator.

QTranslatorMessage QTranslator::findMessage ( const char * context, const char * sourceText, const char * comment = 0 ) const [virtual]

Returns the QTranslatorMessage for the key (context, sourceText, comment). If none is found, also tries (context, sourceText, "").

void QTranslator::insert ( const QTranslatorMessage & message )

Inserts message into this message file.

This function does not work with stripped translator files. It may appear to, but that is not dependable.

See also remove().

void QTranslator::insert ( const char * context, const char * sourceText, const QString & translation )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

bool QTranslator::isEmpty () const

Returns TRUE if this translator is empty, otherwise returns FALSE. This function works with stripped and unstripped translation files.

bool QTranslator::load ( const QString & filename, const QString & directory = QString::null, const QString & search_delimiters = QString::null, const QString & suffix = QString::null )

Loads filename, which may be an absolute file name or relative to directory. The previous contents of this translator object is discarded. Returns TRUE if the file is loaded successfully; otherwise returns FALSE.

If the full file name does not exist, other file names are tried in the following order:

  1. File name with suffix appended (".qm" if the suffix is QString::null).
  2. File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is QString::null).
  3. File name stripped and suffix appended.
  4. File name stripped further, etc.

For example, an application running in the fr_CA locale (French-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"). load() would then try to open the first existing readable file from this list:

  1. /opt/foolib/foo.fr_ca
  2. /opt/foolib/foo.fr_ca.qm
  3. /opt/foolib/foo.fr
  4. /opt/foolib/foo.fr.qm
  5. /opt/foolib/foo
  6. /opt/foolib/foo.qm

See also save().

Example: i18n/main.cpp.

bool QTranslator::load ( const uchar * data, int len )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Loads the .qm file data data of length len into the translator. Returns TRUE if the data is loaded successfully; otherwise returns FALSE.

The data is not copied. The caller must be able to guarantee that data will not be deleted or modified.

QValueList<QTranslatorMessage> QTranslator::messages () const

Returns a list of the messages in the translator. This function is rather slow. Because it is seldom called, it's optimized for simplicity and small size, rather than speed.

If you want to iterate over the list, you should iterate over a copy, e.g.

    QValueList<QTranslatorMessage> list = myTranslator.messages();
    QValueList<QTranslatorMessage>::Iterator it = list.begin();
    while ( it != list.end() ) {
        process_message( *it );
        ++it;
    }
  

void QTranslator::remove ( const QTranslatorMessage & message )

Removes message from this translator.

This function works with stripped translator files.

See also insert().

void QTranslator::remove ( const char * context, const char * sourceText )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Removes the translation associated to the key (context, sourceText, "") from this translator.

bool QTranslator::save ( const QString & filename, SaveMode mode = Everything )

Saves this message file to filename, overwriting the previous contents of filename. If mode is Everything (the default), all the information is preserved. If mode is Stripped, any information that is not necessary for findMessage() is stripped away.

See also load().

void QTranslator::squeeze ( SaveMode mode = Everything )

Converts this message file to the compact format used to store message files on disk.

You should never need to call this directly; save() and other functions call it as necessary. mode is for internal use.

See also save() and unsqueeze().

void QTranslator::unsqueeze ()

Converts this message file into an easily modifiable data structure, less compact than the format used in the files.

You should never need to call this function; it is called by insert() and friends as necessary.

See also squeeze().


This file is part of the Qt toolkit. Copyright © 1995-2005 Trolltech. All Rights Reserved.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard

Logo

Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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.3
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