前言
废话不多说,本文章详细完整介绍QT的多语言切换实现,以中英文为例。
创建项目
工程名称取名为:qt_multilang_demo,一路默认点击下一步,最终生成工程如下所示。
布局界面
简单搞下切换菜单,并添加一个Label,如图所示:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>350</x>
<y>200</y>
<width>191</width>
<height>61</height>
</rect>
</property>
<property name="font">
<font>
<family>Times New Roman</family>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Hello Multilanguage!</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>切换语言</string>
</property>
<addaction name="action_CN"/>
<addaction name="action_EN"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="action_CN">
<property name="text">
<string>中文</string>
</property>
</action>
<action name="action_EN">
<property name="text">
<string>英文</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
生成语言文件
- 修改工程文件.pro,增加语言文件生成脚本,如下面所示:
TRANSLATIONS += \
cn.ts \
en.ts
- 生成语言文件.ts,操作如下:
底部概要信息窗口会输入相关信息:
重新加载工程,也可以看到生成的语言文件:
- 翻译语言文件:
从QT安装目录启动自带的翻译工具Linguist
打开之前生成的cn.ts语言文件,准备翻译成中文:
然后将每条英文信息翻译成中文:
翻译完成后,将每条信息标记为已经翻译,然后保存:
未翻译的cn.ts文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
<name>MainWindow</name>
<message>
<location filename="mainwindow.ui" line="14"/>
<source>MainWindow</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="33"/>
<source>Hello Multilanguage!</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="48"/>
<source>Switch Language</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="58"/>
<source>Chinese</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="mainwindow.ui" line="63"/>
<source>English</source>
<translation type="unfinished"></translation>
</message>
</context>
</TS>
已翻译的cn.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>
<translation>主窗口</translation>
</message>
<message>
<location filename="mainwindow.ui" line="33"/>
<source>Hello Multilanguage!</source>
<translation>你好,多语言!</translation>
</message>
<message>
<location filename="mainwindow.ui" line="48"/>
<source>Switch Language</source>
<translation>切换语言</translation>
</message>
<message>
<location filename="mainwindow.ui" line="58"/>
<source>Chinese</source>
<translation>中文</translation>
</message>
<message>
<location filename="mainwindow.ui" line="63"/>
<source>English</source>
<translation>英文</translation>
</message>
</context>
</TS>
- 发布语言文件,操作如下:
操作后会生成cn.qm、en.qm二进制语言文件,这是最终需要使用的文件:
加载语言文件
- 添加菜单事件:
在菜单事件函数中添加以下代码,注意调用retranslateUi函数,否则不生效。另外,为方便理解这里就不封装复用代码了,大家想继续完善的话,可以把共同代码抽离出来封装独立函数。
void MainWindow::on_action_CN_triggered()
{
QTranslator translator;
translator.load("../qt_multilang_demo/cn.qm");
qApp->installTranslator(&translator);
ui->retranslateUi(this);
}
void MainWindow::on_action_EN_triggered()
{
QTranslator translator;
translator.load("../qt_multilang_demo/en.qm");
qApp->installTranslator(&translator);
ui->retranslateUi(this);
}
程序看效果
各位也可以直接下载示例:https://download.csdn.net/download/sdft06/87701464