The QTranslator 类提供用于文本输出的国际化支持。 更多...
| 头: | #include <QTranslator> |
| 继承: | QObject |
| QTranslator (QObject * parent = 0) | |
| ~QTranslator () | |
| virtual bool | isEmpty () 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 ) |
| virtual QString | translate (const char * context , const char * sourceText , const char * disambiguation = 0) const |
| QString | translate (const char * context , const char * sourceText , const char * disambiguation , int n ) const |
The QTranslator 类提供用于文本输出的国际化支持。
此类的对象包含一组从源语言到目标语言的翻译。 QTranslator provides functions to look up translations in a translation file. Translation files are created using Qt Linguist .
最常见用法对于
QTranslator
is to: load a translation file, install it using
QApplication::installTranslator
(),和使用它凭借
QObject::tr
(). Here's the
main()
function from the
Hello tr()
范例:
int main(int argc, char *argv[]) { QApplication app(argc, argv); QTranslator translator; translator.load("hellotr_la"); app.installTranslator(&translator); QPushButton hello(QPushButton::tr("Hello world!")); hello.resize(100, 30); hello.show(); return app.exec(); }
注意:翻译器必须被创建 before 应用程序的 Widget。
大多数应用程序从不需要对此类做任何其它事情。由此类提供的其它函数对工作于翻译文件的应用程序很有用。
它是可能的,查找翻译使用 translate () (作为 tr () 和 QApplication::translate () 做的)。 translate () 函数最多接受 3 参数:
例如,对话框中的 Cancel 可能为 Anuluj (在此情况下,source text 将是 Cancel),当以波兰语运行程序时。context (通常) 是对话框类名;通常没有注释,翻译文本将是 Anuluj。
但并不总是那么简单。带 two-sided printing (双面打印) 和 binding (装订) 设置的西班牙语版打印机对话框,可能要求将 Enabled 翻译成 Activado 和 Activada 两者。在此情况下,2 案例中的 source text 将是 Enabled,而 context 将是对话框类名,但这 2 项将消除歧义,譬如一个是 two-sided printing,另一个是 binding。disambiguation (消除歧义) 使 translator (翻译器) 能够为西班牙语版选择适当语性,并使 Qt 能够区分翻译。
应用程序可以安装多个翻译文件。翻译搜索次序与其安装次序相反,因此,首先搜索最近安装的翻译文件,最后搜索最早安装的翻译文件。一旦找到包含匹配字符串的翻译,就停止搜索。
此机制使 selected 的特定翻译,或优先于其它翻译成为可能;只需从应用程序卸载 translator (翻译器),通过将它传递给 QApplication::removeTranslator () 函数并重新安装它采用 QApplication::installTranslator ()。那么,它将是搜索匹配字符串的第一翻译。
另请参阅 QApplication::installTranslator (), QApplication::removeTranslator (), QObject::tr (), QApplication::translate (), I18N 范例 , Hello tr () 范例, Arrow Pad 范例 ,和 Troll Print 范例 .
构造空的消息文件对象采用父级 parent 未连接到任何文件。
销毁对象并释放任何分配资源。
[虚拟]
bool
QTranslator::
isEmpty
() const
Returns true if this translator is empty, otherwise returns false. This function works with stripped and unstripped translation files.
加载 filename + suffix (.qm 若 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 applicationDirPath() ).
将丢弃此翻译器对象的先前内容。
若文件名不存在,按以下次序尝试其它文件名:
例如,以 fr_CA 区域设置 (讲法语的加拿大) 运行的应用程序可能调用 load("foo.fr_ca", "/opt/folib")。那么,Load() 将试着打开来自该列表的首个现有可读文件:
/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
加载 filename + prefix + ui language name + suffix (.qm 若 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 语言 - "es", "fr-CA", "de" might call load( QLocale::system (), "foo", ".", "/opt/foolib", ".qm"). load () 将替换 - (短划线) 采用 _ (下划线) 按 UI 语言,然后试着打开来自此列表的首个现有可读文件:
/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 还会试着加载区域设置名称的小写版本。
该函数在 Qt 4.8 引入。
此函数重载 load ().
加载 QM 文件数据 data 的长度 len 进翻译器。
不拷贝数据。调用者必须能够保证 data 不会被删除 (或修改)。
[虚拟]
QString
QTranslator::
translate
(const
char
*
context
, const
char
*
sourceText
, const
char
*
disambiguation
= 0) const
返回翻译对于键 ( context , sourceText , disambiguation )。若未找到,还会尝试 ( context , sourceText , ""). If that still fails, returns an empty string.
If you need to programatically insert translations in to a QTranslator ,可以重实现此函数。
另请参阅 load ().
此函数重载 translate ().
返回翻译对于键 ( context , sourceText , disambiguation )。若未找到,还会尝试 ( context , sourceText , ""). If that still fails, returns an empty string.
若 n 不是 -1,用于选择合适翻译形式 (如 "%n file found" vs. "%n files found")。
另请参阅 load ().