QTranslator使用

pro文件中添加
TRANSLATIONS += my_CN.ts(my_CN名字自起)

打開qt 命令行工具,執行lupdate F:\qt_test\untitled1\untitled1.pro(路徑爲項目的pro路徑)
在這裏插入圖片描述

完成後會在項目下生成my_CN.ts文件

打開qt的 Linguist工具
在這裏插入圖片描述
將生成的ts文件拖進來,代碼中用tr()修飾的文本都會在這裏顯示,在對應的地方寫上翻譯
在這裏插入圖片描述
點擊 文件–>發佈 後會在當前項目中生成my_CN.qm文件。

代碼中加載qm文件即可,注意路徑問題

#include "dialog.h"
#include <QApplication>
#include <QTranslator>
#include <QtDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("./my_CN.qm");
    a.installTranslator(&translator);
    Dialog w;
    w.show();
    w.setWindowTitle(QObject::tr("hello"));

    return a.exec();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章