PySide.QtCore.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. PySide.QtCore.QTranslator provides functions to look up translations in a translation file. Translation files are created using Qt Linguist .
The most common use of PySide.QtCore.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 Hello tr() example:
def main(args):
app = QApplication(args)
translator = QTranslator()
translator.load("hellotr_la")
app.installTranslator(translator)
hello = QPushButton(QPushButton.tr("Hello world!"))
hello.resize(100, 30)
hello.show()
return app.exec_()
注意:翻译器必须被创建 before 应用程序的 Widget。
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.
它是可能的,查找翻译使用 PySide.QtCore.QTranslator.translate() (as tr() and QApplication.translate() do). The PySide.QtCore.QTranslator.translate() function takes up to three parameters:
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.
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 QApplication.removeTranslator() function and reinstall it with QApplication.installTranslator() . It will then be the first translation to be searched for matching strings.
另请参阅
QApplication.installTranslator() QApplication.removeTranslator() QObject.tr() QApplication.translate() I18N 范例 Hello tr() Example Arrow Pad Example Troll Print Example
| 参数: | parent – PySide.QtCore.QObject |
|---|
构造空消息文件对象采用父级 parent 未连接到任何文件。
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if this translator is empty, otherwise returns false. This function works with stripped and unstripped translation files.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
加载 filename + suffix (”.qm” if the suffix 未指定),可能是绝对文件名或相对于 directory . Returns true if the translation is successfully loaded; otherwise returns false.
若 directory is not specified, the directory of the application's executable is used (i.e., as PySide.QtCore.QCoreApplication.applicationDirPath() ).
将丢弃此翻译者对象的先前内容。
若文件名不存在,则按以下次序尝试其它文件名:
For example, an application running in the fr_CA locale (French-speaking Canada) might call load(“foo.fr_ca”, “/opt/foolib”). PySide.QtCore.QTranslator.load() would then try to open the first existing readable file from this list:
| 参数: | data – PySide.QtCore.uchar |
|---|---|
| 返回类型: | PySide.QtCore.bool |
此函数重载 PySide.QtCore.QTranslator.load() .
加载 QM 文件数据 data 的长度 len 进翻译者。
The data is not copied. The caller must be able to guarantee that data will not be deleted or modified.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
加载 filename + prefix + ui language name + suffix (”.qm” if the suffix 未指定),可能是绝对文件名或相对于 directory . Returns true if the translation is successfully loaded; otherwise returns false.
将丢弃此翻译者对象的先前内容。
若文件名不存在,则按以下次序尝试其它文件名:
For example, an application running in the locale with the following ui languages - “es”, “fr-CA”, “de” might call load( QLocale.system() , “foo”, ”.”, “/opt/foolib”, ”.qm”). PySide.QtCore.QTranslator.load() would replace ‘-‘ (dash) with ‘ _ ‘ (underscore) in the ui language and then try to open the first existing readable file from this list:
在文件系统区分大小写的操作系统, PySide.QtCore.QTranslator 还会试着加载区域设置名称的小写版本。
| 参数: |
|
|---|---|
| 返回类型: |
unicode |
返回翻译对于键 ( context , sourceText , disambiguation )。若未找到,还会尝试 ( context , sourceText , “”). If that still fails, returns an empty string.
If you need to programatically insert translations in to a PySide.QtCore.QTranslator ,此函数可以被重实现。
| 参数: |
|
|---|---|
| 返回类型: |
unicode |
此函数重载 PySide.QtCore.QTranslator.translate() .
返回翻译对于键 ( context , sourceText , disambiguation )。若未找到,还会尝试 ( context , sourceText , “”). If that still fails, returns an empty string.
若 n is not -1, it is used to choose an appropriate form for the translation (e.g. “%n file found” vs. “%n files found”).