1在工程中,新建资源文件,命名为translator
在工程目录中的资源文件结构
2修改pro
在pro文件中添加
TRANSLATIONS += \
tr_zh.ts \
tr_en.ts
3代码中所有需要翻译的字符串用tr("")包含 eg: tr(“中文内容”)
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton->setText(tr("button1"));
ui->pushButton_2->setText(tr("button2"));
ui->label->setText(tr("label"));
}
4qt点击更新翻译(lupdate)
此时会在工程目录新建生成了 tr_en.ts 和 tr_zh.ts 两个文件
5打开tr_zh.ts (翻译成中文)
方法一、双击tr_zh.ts文件,自动打开Qt Linguist软件,进行编辑,写入翻译内容,后点击对勾,保存即可
方法二、文本编辑tr_zh.ts
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="zh_CN">
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="14"/>
<source>MainWindow</source>
<translatorcomment>主对话框AAA啊</translatorcomment>
<translation>主对话框AAA</translation>
</message>
<message>
<location filename="mainwindow.ui" line="27"/>
<location filename="mainwindow.ui" line="40"/>
<source>PushButton</source>
<translatorcomment>按钮AAA</translatorcomment>
<translation>按钮</translation>
</message>
<message>
<location filename="mainwindow.ui" line="53"/>
<source>TextLabel</source>
<translatorcomment>内容111啊</translatorcomment>
<translation>内容111</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="9"/>
<source>button1</source>
<translatorcomment>第一个按钮111</translatorcomment>
<translation>第一个按钮</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="10"/>
<source>button2</source>
<translatorcomment>第二个按钮222</translatorcomment>
<translation>第二个按钮</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="12"/>
<source>label</source>
<translatorcomment>第一个文本111</translatorcomment>
<translation>第一个文本</translation>
</message>
</context>
</TS>
6打开tr_en.ts (翻译成英文)
方法一、双击tr_en.ts文件,自动打开Qt Linguist软件,进行编辑,写入翻译内容,后点击对勾,保存即可
方法二、文本编辑
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1" language="en_US">
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="14"/>
<source>MainWindow</source>
<translatorcomment>English main window-comments</translatorcomment>
<translation>English main window</translation>
</message>
<message>
<location filename="mainwindow.ui" line="27"/>
<location filename="mainwindow.ui" line="40"/>
<source>PushButton</source>
<translatorcomment>button</translatorcomment>
<translation>button</translation>
</message>
<message>
<location filename="mainwindow.ui" line="53"/>
<source>TextLabel</source>
<translation>textaaa</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="9"/>
<source>button1</source>
<translatorcomment>first button1111</translatorcomment>
<translation>first button</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="10"/>
<source>button2</source>
<translatorcomment>second button222</translatorcomment>
<translation>second button</translation>
</message>
<message>
<location filename="mainwindow.cpp" line="12"/>
<source>label</source>
<translatorcomment>textaaa1111</translatorcomment>
<translation>textaaa</translation>
</message>
</context>
</TS>
7如下图,点击发布翻译(lrelease)
会在项目中生成 tr_en.qm 和 tr_zh.qm 两个文件
8将qm文件添加到工程中
右键添加现有文件
选择文件
结果:
9使用语言
找到Path
代码
#include "mainwindow.h"
#include <QApplication>
#include <QTranslator>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTranslator translator;
if(translator.load(":/tr_zh.qm")){
qApp->installTranslator(&translator);
}else{
qDebug()<<"load Chinese file error";
}
MainWindow w;
w.show();
return a.exec();
}
10运行结果
注:翻译文件加载的位置必须在界面实例化之前完成,否则是没有效果的
完整实例下载地址:https://download.csdn.net/download/w09103419/15482481