QTranslator Class
The QTranslator class provides internationalization support for text output. More…
| Header | #include <QTranslator> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QTranslator(QObject *parent = nullptr) | |
| ~QTranslator() | |
| virtual QString | translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const |
| virtual bool | isEmpty() const |
| QString | language() const |
| QString | filePath() const |
| bool | load(const QString &filename, const QString &directory = QString(), const QString &search_delimiters = QString(), const QString &suffix = QString()) |
| bool | load(const QLocale &locale, const QString &filename, const QString &prefix = QString(), const QString &directory = QString(), const QString &suffix = QString()) |
| bool | load(const uchar *data, int len, const QString &directory = QString()) |
Detailed Description
A QTranslator holds a set of translations loaded from a .qm file, as produced by Qt's lrelease tool. Install it on the application with QCoreApplication::installTranslator(); tr() and QCoreApplication::translate() then return its translations:
QDir::setSearchPaths(u"i18n"_s, {QCoreApplication::applicationDirPath() + u"/translations"_s});
QTranslator translator;
if (translator.load(QLocale(), u"myapp"_s, u"_"_s, u"i18n:"_s))
QCoreApplication::installTranslator(&translator);
The .qm files can be shipped next to the program, as here, or compiled into it as resources; see The Nt Resource System.
Plural forms are selected with the numerus rules stored in the file, and %n in the result is replaced by the count.
See also QCoreApplication::installTranslator().
Differences from Qt
The files listed as dependencies of a .qm file are not loaded, and the obsolete message tags of very old files are not understood.
Member Function Documentation
QTranslator::QTranslator(QObject *parent = nullptr)
Constructs an empty translator with the given parent.
QTranslator::~QTranslator()
Destroys the translator, removing it from the application first if it is installed.
QString QTranslator::filePath() const
Returns the absolute path of the loaded file, or an empty string if the translations were loaded from memory or nothing is loaded.
This function was introduced in 5.15.
bool QTranslator::isEmpty() const
Returns true if the translator holds no translations; otherwise returns false.
QString QTranslator::language() const
Returns the target language stored in the loaded file, such as "de_DE", or an empty string.
This function was introduced in 5.15.
bool QTranslator::load(const QString &filename, const QString &directory = QString(), const QString &search_delimiters = QString(), const QString &suffix = QString())
Loads the translations from the file filename, relative to directory if it is not absolute, and returns true on success.
If the file does not exist, the function tries the name with the suffix suffix (".qm" by default) and without it, then removes the part of the name after the last of the search_delimiters ("_." by default) and tries again. For "myapp_de_DE" it tries myapp_de_DE.qm, myapp_de_DE, myapp_de.qm, myapp_de, myapp.qm and myapp.
The translations previously held by the translator are discarded.
See also translate() and isEmpty().
bool QTranslator::load(const QLocale &locale, const QString &filename, const QString &prefix = QString(), const QString &directory = QString(), const QString &suffix = QString())
This is an overloaded function.
Loads the translations for the user interface languages of locale and returns true on success.
For each language of QLocale::uiLanguages(), with dashes replaced by underscores, the function tries directory/filename + prefix + language + suffix and the same without suffix, then with the language shortened ("fr_CA" to "fr"). Finally it tries filename + suffix, filename + prefix and filename. The default suffix is ".qm".
This function was introduced in 4.8.
bool QTranslator::load(const uchar *data, int len, const QString &directory = QString())
This is an overloaded function.
Loads the translations from the len bytes at data, which must hold the contents of a .qm file, and returns true on success. The data is copied. directory is accepted for compatibility; dependencies are not loaded.
This function was introduced in 4.5.
QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const
Returns the translation of sourceText in context with the disambiguation comment disambiguation, or a null QString if the translator has none. If n is not negative, the plural form for the count n is returned; %n is not replaced here.
A translation with a disambiguation comment is found only with that comment; if there is none, the translation without a comment is used.
See also QCoreApplication::translate().