QTranslator 類

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 參數:

  • The context - 通常是類名對於 tr () 調用者。
  • The source text - 通常是自變量對於 tr ().
  • The disambiguation - 幫助消除同一上下文中同一文本不同用法歧義的可選字符串。

例如,對話框中的 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 範例 .

成員函數文檔編製

QTranslator:: QTranslator ( QObject * parent = 0)

構造空的消息文件對象采用父級 parent 未連接到任何文件。

QTranslator:: ~QTranslator ()

銷毀對象並釋放任何分配資源。

[虛擬] bool QTranslator:: isEmpty () const

Returns true if this translator is empty, otherwise returns false. This function works with stripped and unstripped translation files.

bool QTranslator:: load (const QString & filename , const QString & directory = QString(), const QString & search_delimiters = QString(), const QString & suffix = QString())

加載 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() ).

將丟棄此翻譯器對象的先前內容。

若文件名不存在,按以下次序嘗試其它文件名:

  1. 文件名不帶 suffix 追加。
  2. 文件名帶文本後於字符 search_delimiters 剝離 ("_." 是默認的對於 search_delimiters 若它是空字符串) 和 suffix .
  3. 文件名剝離不帶 suffix 追加。
  4. 文件名進一步剝離,等等。

例如,以 fr_CA 區域設置 (講法語的加拿大) 運行的應用程序可能調用 load("foo.fr_ca", "/opt/folib")。那麼,Load() 將試著打開來自該列錶的首個現有可讀文件:

  1. /opt/foolib/foo.fr_ca.qm
  2. /opt/foolib/foo.fr_ca
  3. /opt/foolib/foo.fr.qm
  4. /opt/foolib/foo.fr
  5. /opt/foolib/foo.qm
  6. /opt/foolib/foo

bool QTranslator:: load (const QLocale & locale , const QString & filename , const QString & prefix = QString(), const QString & directory = QString(), const QString & suffix = QString())

加載 filename + prefix + ui language name + suffix (.qm 若 suffix 未指定),可能是絕對文件名或相對於 directory . Returns true if the translation is successfully loaded; otherwise returns false.

將丟棄此翻譯器對象的先前內容。

若文件名不存在,按以下次序嘗試其它文件名:

  1. 文件名不帶 suffix 追加。
  2. 文件名帶 UI 語言部分剝離 _ 字符和 suffix .
  3. 文件名帶 UI 語言部分剝離不帶 suffix 追加。
  4. 文件名帶 UI 語言部分的進一步剝離,等等。

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 語言,然後試著打開來自此列錶的首個現有可讀文件:

  1. /opt/foolib/foo.es.qm
  2. /opt/foolib/foo.es
  3. /opt/foolib/foo.fr_CA.qm
  4. /opt/foolib/foo.fr_CA
  5. /opt/foolib/foo.de.qm
  6. /opt/foolib/foo.de
  7. /opt/foolib/foo.fr.qm
  8. /opt/foolib/foo.fr
  9. /opt/foolib/foo.qm
  10. /opt/foolib/foo .
  11. /opt/foolib/foo

在文件係統區分大小寫的操作係統, QTranslator 還會試著加載區域設置名稱的小寫版本。

該函數在 Qt 4.8 引入。

bool QTranslator:: load (const uchar * data , int len )

此函數重載 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 ().

QString QTranslator:: translate (const char * context , const char * sourceText , const char * disambiguation , int n ) const

此函數重載 translate ().

返迴翻譯對於鍵 ( context , sourceText , disambiguation )。若未找到,還會嘗試 ( context , sourceText , ""). If that still fails, returns an empty string.

n 不是 -1,用於選擇閤適翻譯形式 (如 "%n file found" vs. "%n files found")。

另請參閱 load ().