qt语言国际化(翻译),并实现多窗口同时翻译

https://blog.csdn.net/qq_15181569/article/details/135934033

一、.pro文件中添加支持的语言

.pro文件中添加下面几句,支持中文和英文

TRANSLATIONS = lanague_cn.ts\
                lanague_en.ts
  • 1
  • 2

在这里插入图片描述

二、通过qt语言家更新翻译生成.ts文件

在这里插入图片描述
完成以后在工程目录可以看到.ts文件
在这里插入图片描述

三、通过linguist翻译文件

在这里插入图片描述
在这里插入图片描述
打开文件
在这里插入图片描述
将两个文件同时选中,打开。
在这里插入图片描述

四、Qt语言家发布文字生成.qm文件

在这里插入图片描述
在这里插入图片描述

五、使用翻译文件进行多语言切换

为了方便多窗口同时切换,使用单例类进行,源码如下:

languageHelper.h
  • 1
#ifndef LANGUAGEHELPER_H
#define LANGUAGEHELPER_H

#include <QObject>
#include <QMutex>
#include <QTranslator>
#include <QApplication>

class LanguageHelper : public QObject
{
Q_OBJECT
public:
enum language {
chinese,
english,
undefined
};
signals:
void languageChaned();
private:
explicit LanguageHelper();
~LanguageHelper();
LanguageHelper(LanguageHelper&) = delete;
LanguageHelper operator=(const LanguageHelper &) = delete;

public:
static LanguageHelper *getInstance()
{
static QMutex mutex;

    mutex<span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>languageHelper <span class="token operator">==</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</span>
    <span class="token punctuation">{<!-- --></span>
        languageHelper <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token function">LanguageHelper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    mutex<span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> languageHelper<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">quit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">bool</span> <span class="token function">changeLanguage</span><span class="token punctuation">(</span>language l<span class="token punctuation">)</span><span class="token punctuation">;</span>

private:
static LanguageHelper languageHelper;
QTranslator
translator = nullptr;
language currentLanguage = language::undefined;
};

#endif // LANGUAGEHELPER_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
languageHelper.cpp
  • 1
#include "languageHelper.h"

LanguageHelper *LanguageHelper::languageHelper = nullptr;

LanguageHelper::LanguageHelper()
{
this->translator = new QTranslator();

}

LanguageHelper::~LanguageHelper()
{
if(this->translator)
{
delete this->translator;
this->translator = nullptr;
}
qDebug()<<"~LanguageHelper";
}

void LanguageHelper::quit()
{
if(languageHelper)
delete languageHelper;
}

bool LanguageHelper::changeLanguage(language l)
{
if(l this->currentLanguage || l language::undefined)
return true;
bool ret = false;
switch(l)
{
case chinese:
{
if (this->translator != nullptr)
qApp->removeTranslator(this->translator);
ret = this->translator->load("E:/CompanyProjects/KPMG/winccTemp/language/Language/lanague_cn.qm");
if (ret)
{
bool install = qApp->installTranslator(this->translator);
if (install == false)
return false;
}
else
return false;

    <span class="token punctuation">}</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> english<span class="token operator">:</span>
    <span class="token punctuation">{<!-- --></span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span>translator <span class="token operator">!=</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</span>
            qApp<span class="token operator">-&gt;</span><span class="token function">removeTranslator</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span>translator<span class="token punctuation">)</span><span class="token punctuation">;</span>
        ret <span class="token operator">=</span> <span class="token keyword">this</span><span class="token operator">-&gt;</span>translator<span class="token operator">-&gt;</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string">"E:/CompanyProjects/KPMG/winccTemp/language/Language/lanague_en.qm"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
        <span class="token punctuation">{<!-- --></span>
            <span class="token keyword">bool</span> install <span class="token operator">=</span> qApp<span class="token operator">-&gt;</span><span class="token function">installTranslator</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token operator">-&gt;</span>translator<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>install <span class="token operator">==</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
                <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> undefined<span class="token operator">:</span>
    <span class="token punctuation">{<!-- --></span>

    <span class="token punctuation">}</span><span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ret<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
    <span class="token keyword">this</span><span class="token operator">-&gt;</span>currentLanguage <span class="token operator">=</span> l<span class="token punctuation">;</span>
    emit <span class="token function">languageChaned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

通过按钮切换语言:

void MainWindow::on_btnChinese_clicked()
{
    LanguageHelper::getInstance()->changeLanguage(LanguageHelper::chinese);
}

void MainWindow::on_btnEnglish_clicked()
{
LanguageHelper::getInstance()->changeLanguage(LanguageHelper::english);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

需要注意的是,在需要实时翻译的界面需要实现languageChaned信号的绑定,然后调用ui->retranslateUi(this);函数,如下:

    connect(LanguageHelper::getInstance(),&LanguageHelper::languageChaned,this,
            [=]()
            {
                ui->retranslateUi(this);
            });
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文章知识点与官方知识档案匹配,可进一步学习相关知识
MySQL入门技能树数据库组成82422 人正在系统学习中
posted @ 2024-03-30 13:54  mkmkbj  阅读(149)  评论(0编辑  收藏  举报