Qt Toolkit - QTranslator Class

| Classes - Annotated - Tree - Functions - Home - Structure

# 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, const char * name = 0 )
- ~QTranslator ()
- QString find ( const char *, const char *, const char * ) const
- virtual QString find ( const char *, const char * ) const (obsolete)
- QTranslatorMessage findMessage ( const char *, const char *, const char * ) const
- bool load ( const QString & filename, const QString & directory = QString::null, const QString & search_delimiters = QString::null, const QString & suffix = QString::null )
- enum SaveMode { Everything, Stripped }
- bool save ( const QString & filename, SaveMode mode = Everything )
- void clear ()
- void insert ( const QTranslatorMessage & )
- void insert ( const char *, const char *, const QString & ) (obsolete)
- void remove ( const QTranslatorMessage & )
- void remove ( const char *, const char * ) (obsolete)
- bool contains ( const char *, const char *, const char * ) const
- bool contains ( const char *, const char * ) const (obsolete)
- void squeeze ( SaveMode )
- void squeeze ()
- void unsqueeze ()
- QValueList<QTranslatorMessage> messages () 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 such translations, add new ones, remove, load and save them, etc.

The most common use of QTranslator is expected to be loading a translator file, installing it using QApplication::installTranslator(), and using it via QObject::tr(), like this:

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

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

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

        return app.exec();
      }

Most applications will never need to do anything else with this class. However, applications that work on translator files need the other functions in this class.

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

Since end-user programs and translation tools have rather different requirements, QTranslator can use stripped translator files in a way that uses a minimum of memory and provides very little functionality other 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 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 - the argument to tr(), normally.
  - The context - usually the class name for the tr() caller.
  - The comment - a comment which helps disambiguate different uses of the same text in the same context.

The minimum is, for each item, just the information that is necessary for findMessage() to return the right text. This may include the source, context and comment, but usually 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 which 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 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() and QApplication::translate().

Examples: i18n/main.cpp

## Member Type Documentation

### QTranslator::SaveMode

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

- `Everything` - files are saved with all contents
- `Stripped` - files are saved with just what's needed for end-users

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, const char * name = 0 )

Constructs an empty message file, not connected to any file.

### QTranslator::~QTranslator ()

Destructs 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 ) const

Returns TRUE if this message file contains a message with the key ( context, sourceText, comment ), and FALSE if it does not.

This function works with stripped translator files.

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

### bool QTranslator::contains ( const char * context, const char * sourceText ) const

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This version of the function assumes that the comment is "".

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

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

This function works with stripped translator files.

See also findMessage.

### QString QTranslator::find ( const char * context, const char * sourceText ) const `[virtual]`

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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

### QTranslatorMessage QTranslator::findMessage ( const char * context, const char * sourceText, const char * comment ) const

Returns the QTranslatorMessage for the key ( context, sourceText, comment ).

### void QTranslator::insert ( const QTranslatorMessage & message )

Inserts message into this message file.

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

See also remove().

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

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

### 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. If the full filename does not exist, other filenames are tried in the following order:

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

For example, an application running in the fr_CA locale (Frech-speaking Canada) might call load("foo.fr_ca", "/opt/foolib"), which would then try to open these files:

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().

### QValueList<QTranslatorMessage> QTranslator::messages () const

Returns a list of the messages in the translator. This function is somewhat slow; since it's seldom called it's optimized for simplicity and small size, not speed.

### 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 function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

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` (this is the default), all the information is preserved. If mode is `Stripped,` all information that is not necessary for findMessage() is stripped away.

See also load().

### void QTranslator::squeeze ( SaveMode mode )

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.

See also save() and unsqueeze().

### void QTranslator::squeeze ()

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This function calls squeeze( Everything ). It is provided for compatibility; in Qt 3.0 it will be replaced by a default argument.

### 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().

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.