QTranslator(3qt)
okay so bestie, you've stumbled onto the QTranslator(3qt) page and honestly? slay. this is the Qt class that does internationalization for text output, and it's lowkey the main character of making your app speak French, Polish, Spanish, whatever. let's get into it π
πΈ NAME πΈ
QTranslator β the internationalization icon herself π
β¨ SYNOPSIS β¨
#include <qtranslator.h>
Inherits QObject.
π«Ά 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
look at her. so many little functions. she's doing so much for us and we don't even say thank you π
π DESCRIPTION π
The QTranslator class provides internationalization support for text output. that's the whole point of her existence, periodt.
An object of this class contains a set of QTranslatorMessage objects, and each one of those specifies a translation going from a source language to a target language. QTranslator hands you functions to look up translations, add new ones, remove them, load and save them, etc. etc. β the full toolkit, no cap.
The most common use of QTranslator is basically a three-step routine:
- load a translator file created with Qt Linguist π¨
- install it using
QApplication::installTranslator() - use it via
QObject::tr()
For example, bestie:
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 this, it matters: the translator must be created before the application's main window. do not be cute about the ordering.
Most applications will literally never need to do anything else with this class. ever. The other functions she provides are useful for applications that work on translator files β i.e. the tooling girlies, not the everyday app girlies.
We call a translation a "messsage". (yes, that's the spelling, and because of that, translation files are sometimes referred to as "message files".) π
So you can:
- look up a translation using
findMessage()β which is exactly whattr()andQApplication::translate()do β and alsocontains() - insert a new translation messsage using
insert() - remove one using
remove()
Translation tools often need more information than just the bare source text and translation. like, context information to help the translator out. but end-user programs that are using translations usually only need lookup. To cater to these two very different needs, QTranslator can use stripped translator files β files that use the minimum of memory and which support little more functionality than findMessage().
So yeah: 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 source text β usually the argument to
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, bestie. 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. gender agreement ate, fr fr π
important: 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. so read the fine print, okay?
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 informationQTranslator::Strippedβ files are saved with just enough information for end-user applications
and again, because it's that serious: 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. she's brand new, she's empty, she's ready β¨
QTranslator::~QTranslator ()
Destroys the object and frees any allocated resources. rest in peace queen π«Ά
void QTranslator::clear ()
Empties this translator of all contents. like, everything gone. clean slate energy.
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. vibe check: is it in there or not π
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. do not text her back, she's your ex.
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, ""). she's persistent, she'll try the backup plan too π
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. do not trust the illusion, bestie.
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. equal opportunity emptiness check π
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. out with the old, in with the new π
If the full file name does not exist, other file names are tried in the following order:
- File name with suffix appended (
".qm"if the suffix isQString::null). - File name with text after a character in search_delimiters stripped (
"_."is the default for search_delimiters if it isQString::null). - File name stripped and 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:
/opt/foolib/foo.fr_ca/opt/foolib/foo.fr_ca.qm/opt/foolib/foo.fr/opt/foolib/foo.fr.qm/opt/foolib/foo/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. so keep that pointer alive, bestie, or she's gonna be reading garbage π
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. she's not built for speed, she's built for vibes π¦
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. bye bestie π
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 )β okay so this one writes your whole message file out tofilename, and heads up bestie, it overwrites whatever was in there before π no merge, no merge era, it's a clean slate. IfmodeisEverything(which is the default, the standard, the moment β¨) then all the information is preserved β every last crumb. IfmodeisStripped, then anything that isn't necessary forfindMessage()gets stripped away π gone, deleted, not invited.
Also peep load() for the reverse vibe π«Ά
void QTranslator::squeeze ( SaveMode mode = Everything )β this one converts this message file into the compact format that's used to store message files on disk. It's giving compression era ποΈβ¨
fr fr you should never need to call this directly πsave()and its little friends call it whenever they need to. Andmode? That's for internal use only, mind your business π
Also peep save() and unsqueeze() πΈ
void QTranslator::unsqueeze ()β the opposite slay π¦ this one converts this message file into an easily modifiable data structure, which is less compact than the format that actually lives in the files. We're decompressing, we're stretching out, we're taking up space and thriving π
ngl you should never need to call this function π it gets called by insert() and its friends whenever they need it. They got it handled, trust π€
Also peep squeeze() π
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. π periodt.
π AUTHOR π
Generated automatically from the source code. π€ (yes, the robot wrote it, and it ate, honestly)
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά (they're being so polite about it, we love to see it)
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. π
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qtranslator.3qt) and the Qt version (3.3.8). π
and that's a wrap on this whole page bestie, you made it to the end ππ shoutout to Trolltech AS, who shipped this on 2 February 2007, and to the QTranslator(3qt) man page itself β iconic, no notes, we stan a legend πβ¨
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms