Qt logo

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


Detailed Description

The QTranslator class provides internationalization support for text output.

The class is conceptually very simple: An object of this class contains a set of translations from the reference language to a target language, and provides functions to add, look up and remove such translations as well as the ability to load and save the object to a file.

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

Slightly more advanced usage of QTranslator includes direct lookup using find(), adding new translations using insert() and removing existing ones using remove() or even clear(), and testing whether the QTranslator contains a translation using contains().

The hash() function mentioned is a variant on the standard ELF hash, modified to work well with Unicode strings in UCS-2 format. Its algorithm is not specified beyond the fact that it will remain unchanged in future versions of Qt.

To examine the contents of a QTranslator, use QTranslatorIterator.

See also: QTranslatorIterator, QApplication::installTranslator, QApplication::removeTranslator(), QObject::tr() and QApplication::translate().

Examples: i18n/main.cpp


Member Function Documentation

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

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

QTranslator::~QTranslator ()

Destroys the object and frees any allocated resources.

void QTranslator::clear ()

Empties this translator of all contents.

bool QTranslator::contains ( const char * scope, const char * key ) const

Returns TRUE if this message file contains a message with hash value h, and FALSE if it does not.

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

QString QTranslator::find ( const char * scope, const char * message ) const [virtual]

Returns the translation for (scope, key), or QString::null in case there is none in this translator.

void QTranslator::insert ( const char * scope, const char * message, const QString & translation )

Inserts translation of message into this message file.

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, load("foo_bar.baz", "/opt/foolib") will search for:
  1. /opt/foolib/foo_bar.baz
  2. /opt/foolib/foo_bar.baz.qm
  3. /opt/foolib/foo_bar
  4. /opt/foolib/foo_bar.qm
  5. /opt/foolib/foo
  6. /opt/foolib/foo.qm

Examples: i18n/main.cpp

void QTranslator::remove ( const char * scope, const char * message )

Removes the string for h from this message file. If there is no string for h, this function does nothing.

void QTranslator::squeeze ()

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.

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() etc. as necessary.

See also: squeeze().


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


This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.


Copyright İ 1999 Troll TechTrademarks
Qt version 2.0.2