QTranslator Class Reference

#include <qtranslator.h>

Inheritance diagram for QTranslator:

Inheritance graph
[legend]
Collaboration diagram for QTranslator:

Collaboration graph
[legend]
List of all members.

Detailed Description

The QTranslator class provides internationalization support for text output.

An object of this class contains a set of translations from a source language to a target language. QTranslator provides functions to look up translations in a translation file. Translation files are created using {Qt Linguist}.

The most common use of QTranslator is to: load a translation file, install it using QApplication::installTranslator(), and use it via QObject::tr(). Here's the main() function from the {linguist/hellotr}{Hello tr()} example:

linguist/hellotr/main.cpp main( }

Note that the translator must be created before the application's widgets.

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.

It is possible to lookup a translation using translate() (as tr() and QApplication::translate() do). The translate() function takes up to three parameters:

The context - usually the class name for the tr() caller. The {source text} - usually the argument to tr(). The comment - an optional comment that helps disambiguate different uses of the same text in the same context.

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.

See also:
QApplication::installTranslator(), QApplication::removeTranslator(), QObject::tr(), QApplication::translate(), {I18N Example}, {Hello tr() Example}, {Arrow Pad Example}, {Troll Print Example}

Definition at line 38 of file qtranslator.h.

Public Member Functions

 QTranslator (QObject *parent=0)
 ~QTranslator ()
virtual QString translate (const char *context, const char *sourceText, const char *comment=0) const
QString translate (const char *context, const char *sourceText, const char *comment, int n) const
virtual bool isEmpty () const
bool load (const QString &filename, const QString &directory=QString(), const QString &search_delimiters=QString(), const QString &suffix=QString())
bool load (const uchar *data, int len)


Constructor & Destructor Documentation

QTranslator::QTranslator ( QObject parent = 0  )  [explicit]

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

Definition at line 289 of file qtranslator.cpp.

00290     : QObject(*new QTranslatorPrivate, parent)
00291 {
00292 }

QTranslator::~QTranslator (  ) 

Destroys the object and frees any allocated resources.

Definition at line 310 of file qtranslator.cpp.

References d, QCoreApplication::instance(), and QCoreApplication::removeTranslator().

00311 {
00312     if (QCoreApplication::instance())
00313         QCoreApplication::instance()->removeTranslator(this);
00314     Q_D(QTranslator);
00315     d->clear();
00316 }

Here is the call graph for this function:


Member Function Documentation

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

Returns the translation for the key (context, sourceText, comment). If none is found, also tries (context, sourceText, ""). If that still fails, returns an empty string.

See also:
load()

Definition at line 745 of file qtranslator.cpp.

References d.

Referenced by LanguageChooser::languageName(), translate(), and QCoreApplication::translate().

00746 {
00747     Q_D(const QTranslator);
00748     return d->do_translate(context, sourceText, comment, -1);
00749 }

QString QTranslator::translate ( const char *  context,
const char *  sourceText,
const char *  comment,
int  n 
) const

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, comment). If none is found, also tries (context, sourceText, ""). If that still fails, returns an empty string.

If n is not -1, it is used to choose an appropriate form for the translation (e.g. "%n file found" vs. "%n files found").

See also:
load()

Definition at line 764 of file qtranslator.cpp.

References d, and translate().

00766 {
00767     Q_D(const QTranslator);
00768     // this step is necessary because the 3-parameter translate() overload is virtual
00769     if (n == -1)
00770         return translate(context, sourceText, comment);
00771     return d->do_translate(context, sourceText, comment, n);
00772 }

Here is the call graph for this function:

bool QTranslator::isEmpty (  )  const [virtual]

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

Definition at line 778 of file qtranslator.cpp.

References d.

Referenced by QCoreApplication::installTranslator().

00779 {
00780     Q_D(const QTranslator);
00781     return !d->unmapPointer && !d->unmapLength && !d->messageArray &&
00782            !d->offsetArray && !d->contextArray;
00783 }

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

Loads filename + suffix (".qm" if the suffix is not specified), which may be an absolute file name or relative to directory. Returns true if the translation is successfully loaded; otherwise returns false.

The previous contents of this translator object are discarded.

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

1 File name without suffix appended. File name with text after a character in search_delimiters stripped ("_." is the default for search_delimiters if it is an empty string) and suffix. File name stripped without suffix appended. 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.qm /opt/foolib/foo.fr_ca /opt/foolib/foo.fr.qm /opt/foolib/foo.fr /opt/foolib/foo.qm /opt/foolib/foo

Definition at line 353 of file qtranslator.cpp.

References d, QFile::encodeName(), QString::fromLatin1(), i, int, isLetter(), QString::isNull(), l, QString::lastIndexOf(), QString::length(), MAP_FAILED, MAP_FILE, QFile::open(), ptr, QIODevice::read(), QIODevice::ReadOnly, QFile::size(), QString::size(), and QString::startsWith().

Referenced by LanguageChooser::checkBoxToggled(), QDesigner::initialize(), LanguageChooser::languageName(), TrPreviewTool::loadTranslation(), and main().

00356 {
00357     Q_D(QTranslator);
00358     d->clear();
00359 
00360     QString prefix;
00361 
00362     const int l = filename.size();
00363     if (!((l > 0 && filename[0] == QLatin1Char('/'))
00364 #ifdef Q_WS_WIN
00365           || (l >= 2 && filename[0].isLetter() && filename[1] == QLatin1Char(':'))
00366           || (l >= 1 && filename[0] == QLatin1Char('\\'))
00367 #endif
00368             )) {
00369         prefix = directory;
00370     }
00371 
00372     if (prefix.length()) {
00373         if (prefix[int(prefix.length()-1)] != QLatin1Char('/'))
00374             prefix += QLatin1Char('/');
00375     }
00376 
00377     QString fname = filename;
00378     QString realname;
00379     QString delims;
00380     delims = search_delimiters.isNull() ? QString::fromLatin1("_.") : search_delimiters;
00381 
00382     for (;;) {
00383         QFileInfo fi;
00384 
00385         realname = prefix + fname + (suffix.isNull() ? QString::fromLatin1(".qm") : suffix);
00386         fi.setFile(realname);
00387         if (fi.isReadable())
00388             break;
00389 
00390         realname = prefix + fname;
00391         fi.setFile(realname);
00392         if (fi.isReadable())
00393             break;
00394 
00395         int rightmost = 0;
00396         for (int i = 0; i < (int)delims.length(); i++) {
00397             int k = fname.lastIndexOf(delims[i]);
00398             if (k > rightmost)
00399                 rightmost = k;
00400         }
00401 
00402         // no truncations? fail
00403         if (rightmost == 0)
00404             return false;
00405 
00406         fname.truncate(rightmost);
00407     }
00408 
00409     // realname is now the fully qualified name of a readable file.
00410 
00411     bool ok = false;
00412 
00413 #ifdef QT_USE_MMAP
00414 
00415 #ifndef MAP_FILE
00416 #define MAP_FILE 0
00417 #endif
00418 #ifndef MAP_FAILED
00419 #define MAP_FAILED -1
00420 #endif
00421 
00422     int fd = -1;
00423     if (!realname.startsWith(QLatin1Char(':')))
00424         fd = QT_OPEN(QFile::encodeName(realname), O_RDONLY,
00425 #if defined(Q_OS_WIN)
00426                      _S_IREAD | _S_IWRITE
00427 #else
00428                      0666
00429 #endif
00430             );
00431     if (fd >= 0) {
00432         struct stat st;
00433         if (!fstat(fd, &st)) {
00434             char *ptr;
00435             ptr = reinterpret_cast<char *>(
00436                 mmap(0, st.st_size,             // any address, whole file
00437                      PROT_READ,                 // read-only memory
00438                      MAP_FILE | MAP_PRIVATE,    // swap-backed map from file
00439                      fd, 0));                   // from offset 0 of fd
00440             if (ptr && ptr != reinterpret_cast<char *>(MAP_FAILED)) {
00441                 d->used_mmap = true;
00442                 d->unmapPointer = ptr;
00443                 d->unmapLength = st.st_size;
00444                 ok = true;
00445             }
00446         }
00447         ::close(fd);
00448     }
00449 #endif // QT_USE_MMAP
00450 
00451     if (!ok) {
00452         QFile file(realname);
00453         d->unmapLength = file.size();
00454         if (!d->unmapLength)
00455             return false;
00456         d->unmapPointer = new char[d->unmapLength];
00457 
00458         if (file.open(QIODevice::ReadOnly))
00459             ok = (d->unmapLength == (uint)file.read(d->unmapPointer, d->unmapLength));
00460 
00461         if (!ok) {
00462             delete [] d->unmapPointer;
00463             d->unmapPointer = 0;
00464             d->unmapLength = 0;
00465             return false;
00466         }
00467     }
00468 
00469     return d->do_load(reinterpret_cast<const uchar *>(d->unmapPointer), d->unmapLength);
00470 }

Here is the call graph for this function:

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

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

Loads the .qm file data data of length len into the translator.

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

Definition at line 482 of file qtranslator.cpp.

References d.

00483 {
00484     Q_D(QTranslator);
00485     d->clear();
00486     return d->do_load(data, len);
00487 }


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 19:36:12 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1