QTranslator¶
The QTranslator
class provides internationalization support for text output. More…
Synopsis¶
Functions¶
Virtual functions¶
Detailed Description¶
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, and install it using installTranslator()
.
Here’s an example main()
function using the QTranslator
:
if __name__ == "__main__": app = QApplication([]) translator = QTranslator() # look up e.g. :/i18n/myapp_de.qm if (translator.load(QLocale(), "myapp"), QLatin1String("_"), QLatin1String(":/i18n")) QCoreApplication.installTranslator(translator) hello = QPushButton(QCoreApplication.translate("main", "Hello world!")) hello.resize(100, 30) hello.show() sys.exit(app.exec())
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.
Looking up Translations¶
It is possible to look up a translation using translate()
(as tr()
and 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 disambiguation - an optional string 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 disambiguations such as “two-sided printing” for one and “binding” for the other. The disambiguation enables the translator to choose the appropriate gender for the Spanish version, and enables Qt to distinguish between translations.
Using Multiple Translations¶
Multiple translation files can be installed in an application. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched for translations first and the earliest translation file is searched last. The search stops as soon as a translation containing a matching string is found.
This mechanism makes it possible for a specific translation to be “selected” or given priority over the others; simply uninstall the translator from the application by passing it to the removeTranslator()
function and reinstall it with installTranslator()
. It will then be the first translation to be searched for matching strings.
See also
installTranslator()
removeTranslator()
tr()
translate()
I18N ExampleHello tr() ExampleArrow Pad ExampleTroll Print Example
- class PySide6.QtCore.QTranslator([parent=None])¶
- Parameters
parent –
PySide6.QtCore.QObject
Constructs an empty message file object with parent parent
that is not connected to any file.
- PySide6.QtCore.QTranslator.filePath()¶
- Return type
str
Returns the path of the loaded translation file.
The file path is empty if no translation was loaded yet, the loading failed, or if the translation was not loaded from a file.
- PySide6.QtCore.QTranslator.isEmpty()¶
- Return type
bool
Returns true
if this translator is empty, otherwise returns false
. This function works with stripped and unstripped translation files.
- PySide6.QtCore.QTranslator.language()¶
- Return type
str
Returns the target language as stored in the translation file.
- PySide6.QtCore.QTranslator.load(locale, filename[, prefix=""[, directory=""[, suffix=""]]])¶
- Parameters
locale –
PySide6.QtCore.QLocale
filename – str
prefix – str
directory – str
suffix – str
- Return type
bool
Loads filename
+ prefix
+ ui language name
+ 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:
File name without
suffix
appended.File name with ui language part after a “_” character stripped and
suffix
.File name with ui language part stripped without
suffix
appended.File name with ui language part stripped further, etc.
For example, an application running in the locale
with the following ui languages
- “es”, “fr-CA”, “de” might call load(QLocale(), “foo”, “.”, “/opt/foolib”, “.qm”). load()
would replace ‘-’ (dash) with ‘_’ (underscore) in the ui language and then try to open the first existing readable file from this list:
/opt/foolib/foo.es.qm
/opt/foolib/foo.es
/opt/foolib/foo.fr_CA.qm
/opt/foolib/foo.fr_CA
/opt/foolib/foo.fr.qm
/opt/foolib/foo.fr
/opt/foolib/foo.de.qm
/opt/foolib/foo.de
/opt/foolib/foo.qm
/opt/foolib/foo
.
/opt/foolib/foo
On operating systems where file system is case sensitive, QTranslator
also tries to load a lower-cased version of the locale name.
- PySide6.QtCore.QTranslator.load(filename[, directory=""[, search_delimiters=""[, suffix=""]]])
- Parameters
filename – str
directory – str
search_delimiters – str
suffix – str
- Return type
bool
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
.
If directory
is not specified, the current directory is used (i.e., as currentPath()
).
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:
File name without
suffix
appended.File name with text after a character in
search_delimiters
stripped (”_.” is the default forsearch_delimiters
if it is an empty string) andsuffix
.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”). would then try to open the first existing readable file from this list:
/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
Usually, it is better to use the (const QLocale
&, const QString
&, const QString
&, const QString
&, const QString
&) function instead, because it uses uiLanguages()
and not simply the locale name, which refers to the formatting of dates and numbers and not necessarily the UI language.
- PySide6.QtCore.QTranslator.load(data[, directory=""])
- Parameters
data – str
directory – str
- Return type
bool
This function overloads load()
.
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.
directory
is only used to specify the base directory when loading the dependencies of a QM file. If the file does not have dependencies, this argument is ignored.
- PySide6.QtCore.QTranslator.translate(context, sourceText[, disambiguation=None[, n=-1]])¶
- Parameters
context – str
sourceText – str
disambiguation – str
n – int
- Return type
str
Returns the translation for the key (context
, sourceText
, disambiguation
). If none is found, also tries (context
, sourceText
, “”). If that still fails, returns a null string.
Note
Incomplete translations may result in unexpected behavior: If no translation for (context
, sourceText
, “”) is provided, the method might in this case actually return a translation for a different disambiguation
.
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”).
If you need to programmatically insert translations into a QTranslator
, this function can be reimplemented.
See also
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.