QTranslator 類

The QTranslator 類提供用於文本輸齣的國際化支持。 更多...

頭: #include <QTranslator>
qmake: QT += core
繼承: QObject

公共函數

QTranslator (QObject * parent = Q_NULLPTR)
~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 , const QString & directory = QString())
virtual QString translate (const char * context , const char * sourceText , const char * disambiguation = Q_NULLPTR, int n = -1) 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 QCoreApplication::installTranslator (),和使用它憑藉 QObject::tr ()。這裏的範例 main() 函數使用 QTranslator :

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")))
        app.installTranslator(&translator);
    QPushButton hello(QCoreApplication::translate("main", "Hello world!"));
    hello.resize(100, 30);
    hello.show();
    return app.exec();
}
					

注意:翻譯器必須被創建 before 應用程序的 Widget。

大多數應用程序從不需要對此類做任何其它事情。由此類提供的其它函數對工作於翻譯文件的應用程序很有用。

查找翻譯

它是可能的,查找翻譯使用 translate () (作為 tr () 和 QCoreApplication::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 (翻譯器),通過將它傳遞給 QCoreApplication::removeTranslator () 函數並重新安裝它采用 QCoreApplication::installTranslator ()。那麼,它將是搜索匹配字符串的第一翻譯。

另請參閱 QCoreApplication::installTranslator (), QCoreApplication::removeTranslator (), QObject::tr (), QCoreApplication::translate (), I18N 範例 , Hello tr () 範例, Arrow Pad 範例 ,和 Troll Print 範例 .

成員函數文檔編製

QTranslator:: QTranslator ( QObject * parent = Q_NULLPTR)

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

QTranslator:: ~QTranslator ()

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

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

返迴 true 若此翻譯器為空,否則返迴 false 。此函數處理剝離和未剝離翻譯文件。

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

加載 filename + suffix (.qm 若 suffix 未指定),可能是絕對文件名或相對於 directory 。返迴 true 若成功加載翻譯;否則返迴 false .

directory 未指定,使用當前目錄 (即 currentPath() ).

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

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

  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

通常,最好是使用 QTranslator::load(const QLocale &, const QString &, const QString &, const QString &, const QString &) 函數代替,因為它使用 QLocale::uiLanguages () 而不僅僅是區域設置名稱,指的是日期和數字格式,而不必是 UI 語言。

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 。返迴 true 若成功加載翻譯;否則返迴 false .

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

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

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

例如,應用程序運行在 locale 采用下列 UI 語言 -es、fr-CA、de 可以調用 load(QLocale(), "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 , const QString & directory = QString())

此函數重載 load ().

加載 QM 文件數據 data 的長度 len 進翻譯器。

不拷貝數據。調用者必須能夠保證 data 不會被刪除 (或修改)。

directory 僅用於指定基目錄,當加載 QM 文件的依賴時。若文件沒有依賴,忽略此自變量。

[虛擬] QString QTranslator:: translate (const char * context , const char * sourceText , const char * disambiguation = Q_NULLPTR, int n = -1) const

返迴翻譯對於鍵 ( context , sourceText , disambiguation )。若未找到,還會嘗試 ( context , sourceText , "")。若仍失敗,返迴 null 字符串。

注意: 不完整翻譯可能導緻意外行為:若沒有翻譯對於 ( context , sourceText , "") 的提供,在此情況下,方法可能實際返迴翻譯對於不同 disambiguation .

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

若需要以編程方式將翻譯插入 QTranslator ,可以重實現此函數。

另請參閱 load ().