QTranslatorclass provides internationalization support for text output. 更多 …
An object of this class contains a set of translations from a source language to a target language.
QTranslatorprovides functions to look up translations in a translation file. Translation files are created using Qt Linguist.The most common use of
QTranslatoris to: load a translation file, install it usinginstallTranslator(), and use it viatr(). Here’s an examplemain()function using theQTranslator:int main(int argc, char *argv[]) { QApplication app(argc, argv); QTranslator translator; // look up e.g. :/translations/myapp_de.qm if (translator.load(QLocale(), QLatin1String("myapp"), QLatin1String("_"), QLatin1String(":/translations"))) QCoreApplication::installTranslator(&translator); QPushButton hello(QCoreApplication::translate("main", "Hello world!")); hello.resize(100, 30); hello.show(); return QCoreApplication::exec(); }注意:翻译器必须被创建 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.
它是可能的,查找翻译使用
translate()(astr()andtranslate()do). Thetranslate()function takes up to three parameters:
context - 通常是类名对于
tr()caller.源文本 - usually the argument to
tr().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.
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 withinstallTranslator(). It will then be the first translation to be searched for matching strings.另请参阅
installTranslator()removeTranslator()tr()translate()I18N 范例Hello tr() ExampleArrow Pad ExampleTroll Print 范例
PySide2.QtCore.QTranslator.
filePath
(
)
¶
unicode
返回加载的翻译文件路径。
The file path is empty if no translation was loaded yet, the loading failed, or if the translation was not loaded from a file.
PySide2.QtCore.QTranslator.
isEmpty
(
)
¶
bool
返回
true
若此翻译者为空,否则返回
false
。此函数处理剥离和未剥离翻译文件。
PySide2.QtCore.QTranslator.
language
(
)
¶
unicode
返回存储在翻译文件中的目标语言。
PySide2.QtCore.QTranslator.
load
(
locale
,
filename
[
,
prefix=""
[
,
directory=""
[
,
suffix=""
]
]
]
)
¶
locale
–
QLocale
filename – unicode
prefix – unicode
directory – unicode
suffix – unicode
bool
加载
filename
+
prefix
+
ui
language
name
+
suffix
(“.qm” if the
suffix
未指定),可能是绝对文件名或相对于
directory
。返回
true
若成功加载翻译;否则返回
false
.
将丢弃此翻译者对象的先前内容。
若文件名不存在,则按以下次序尝试其它文件名:
文件名不带
suffix
追加。
File name with ui language part after a “_” character stripped and
suffix
.
File name with ui language part stripped without
suffix
追加。
File name with ui language part stripped further, etc.
例如,应用程序运行在
locale
采用下列
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.de.qm
/opt/foolib/foo.de
/opt/foolib/foo.fr.qm
/opt/foolib/foo.fr
/opt/foolib/foo.qm
/opt/foolib/foo
.
/opt/foolib/foo
在文件系统区分大小写的操作系统,
QTranslator
还会试着加载区域设置名称的小写版本。
PySide2.QtCore.QTranslator.
load
(
filename
[
,
directory=""
[
,
search_delimiters=""
[
,
suffix=""
]
]
]
)
¶
filename – unicode
directory – unicode
search_delimiters – unicode
suffix – unicode
bool
加载
filename
+
suffix
(“.qm” if the
suffix
未指定),可能是绝对文件名或相对于
directory
。返回
true
若成功加载翻译;否则返回
false
.
若
directory
is not specified, the current directory is used (i.e., as
currentPath()
).
将丢弃此翻译者对象的先前内容。
若文件名不存在,则按以下次序尝试其它文件名:
文件名不带
suffix
追加。
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
追加。
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.
PySide2.QtCore.QTranslator.
load
(
data
[
,
directory=""
]
)
¶
data
–
uchar
directory – unicode
bool
此函数重载
load()
.
加载 QM 文件数据
data
的长度
len
进翻译者。
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.
PySide2.QtCore.QTranslator.
translate
(
context
,
sourceText
[
,
disambiguation=None
[
,
n=-1
]
]
)
¶
context – str
sourceText – str
disambiguation – str
n
–
int
unicode
返回翻译对于键 (
context
,
sourceText
,
disambiguation
)。若未找到,还会尝试 (
context
,
sourceText
, “”). If that still fails, returns a null string.
注意
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
.
若
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 programatically insert translations into a
QTranslator
,此函数可以被重实现。
另请参阅