QT-自定义本地翻译模块,简单实用


前言

QT的国际语言每次如果有字符串变动,都需要重新再编译一下,真的是顶不住,说实话,用起来真的是不习惯。
为了解决这个问题,我们设计基本的逻辑:

1、将控件名称保存到本地xml,也就是先初始化一个需要我们要翻译的文本。
2、在xml填入翻译的内容。
3、软件显示我们翻译的内容,这里就是有技巧,我们希望是自动去翻译这些控件,而不是程序四处都看到我们翻译的过程。

一、演示效果

1、本地xml格式内容如下,启动source的属性是我们程序自动生成的,我们当然是希望没翻译之前,这些需要我们翻译的字段先填好。
在这里插入图片描述

2、界面显示效果如下:
在这里插入图片描述
在这里插入图片描述

二、核心模块

1.本地xml的操作过程

代码如下:

#include "LanguageXml.h"
#include <QMutex>
#include <QFile>

LanguageXml::LanguageXml(QObject *parent)
	: QObject(parent)
{
	
}

LanguageXml::~LanguageXml()
{
}

//插入链表
void LanguageXml::insertItem(QString strSource, QString strTranslate)
{
	m_itemsHash.insert(strSource,strTranslate);
}

//读取本地链表
bool LanguageXml::readItems(QString strFilePath)
{
	if (strFilePath.isEmpty())
		return false;

	QMutex mutex;
	mutex.lock();
	QFile file(strFilePath);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		mutex.unlock();
		return false;
	}

	QXmlStreamReader reader(&file);
	while (!reader.atEnd())
	{
		reader.readNext();
		QString strElementName = reader.name().toString();
		if (reader.isStartElement() && strElementName == QString("Item"))
		{
			QString strSource;
			QString strTranslation;
			auto attr = reader.attributes();

			if (attr.hasAttribute("Source"))
				strSource = attr.value("Source").toString();

			if (attr.hasAttribute("Translation"))
				strTranslation = attr.value("Translation").toString();

			m_itemsHash[strSource] = strTranslation;
		}
	}

	file.close();
	mutex.unlock();

	return true;
}

//保存本地
bool LanguageXml::saveItems(QString strFilePath)
{
	if (strFilePath.isEmpty())
		return false;

	static QMutex mutex;
	mutex.lock();
	QFile file(strFilePath);
	file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
	QXmlStreamWriter writer(&file);

	writer.setAutoFormatting(true);
	writer.writeStartDocument();

	if (true)
	{
		writer.writeStartElement("LanguageItems");  

		auto keys = m_itemsHash.keys();
		for (int j = 0; j < keys.size(); j++)
		{
			if (true)
			{
				writer.writeStartElement("Item"); 
				writer.writeAttribute("Source", keys[j]);
				writer.writeAttribute("Translation", m_itemsHash[keys[j]]);

				writer.writeEndElement();   
			}
		}

		writer.writeEndElement();  
	}

	writer.writeEndDocument();

	file.close();

	mutex.unlock();

	return true;
}

//返回链表
QHash<QString, QString> LanguageXml::getItemsHash()
{
	return m_itemsHash;
}

2、翻译模块

代码如下:

#include "ControlsTranslate.h"
#include "LanguageXml.h"
#include <QPushButton>
#include <QToolButton>
#include <QGroupBox>
#include <QCheckBox>
#include <QRadioButton>
#include <QLineEdit>
#include <QDateTimeEdit>
#include <QLocale>
#include <QTableWidget>
#include <QTreeWidget>
#include <QLabel>
#include <QComboBox>
#include <QApplication>
#include <QFile>
#include <QMainWindow>

ControlsTranslate::ControlsTranslate(QObject *parent)
	: QObject(parent)
{
	m_pLanguageXml = new LanguageXml(parent);
	m_objectList = getAllObject(parent);
	initialTranslateFile();

}

ControlsTranslate::~ControlsTranslate()
{
}

//获取所有控件
QObjectList ControlsTranslate::getAllObject(QObject * parent)
{
	QObjectList childrenList, tempList;
	if (parent)
		childrenList = parent->children();

	if (childrenList.isEmpty())
		return childrenList;

	tempList = childrenList;
	foreach(QObject* obj, tempList)
	{
		QObjectList lst = getAllObject(obj);
		if (!lst.isEmpty())
			childrenList.append(lst);
	}

	return childrenList;
}

//翻译控件
void ControlsTranslate::translateAllControls(const QString &strLanguage)
{
	if (!m_objectList.isEmpty())
	{
		foreach(QObject *obj, m_objectList)
		{

			auto pPushButton = qobject_cast<QPushButton *>(obj);
			if (pPushButton != nullptr)
				pPushButton->setText(translate(pPushButton->text()));

			auto pToolButton = qobject_cast<QToolButton *>(obj);
			if (pToolButton != nullptr)
				pToolButton->setText(translate(pToolButton->text()));

			auto pWidget = qobject_cast<QWidget *>(obj);
			if (pWidget != nullptr)
				pWidget->setWindowTitle(translate(pWidget->windowTitle()));

			auto pGroupBox = qobject_cast<QGroupBox *>(obj);
			if (pGroupBox != nullptr)
				pGroupBox->setTitle(translate(pGroupBox->title()));

			auto pLabel = qobject_cast<QLabel *>(obj);
			if (pLabel != nullptr)
				pLabel->setText(translate(pLabel->text()));

			auto pCheckBox = qobject_cast<QCheckBox *>(obj);
			if (pCheckBox != nullptr)
				pCheckBox->setText(translate(pCheckBox->text()));

			auto pRadioButton = qobject_cast<QRadioButton *>(obj);
			if (pRadioButton != nullptr)
				pRadioButton->setText(translate(pRadioButton->text()));

			auto pLineEdit = qobject_cast<QLineEdit *>(obj);
			if (pLineEdit != nullptr)
			{
				pLineEdit->setText(translate(pLineEdit->text()));
				pLineEdit->setPlaceholderText(translate(pLineEdit->placeholderText()));
			}

			auto pDateTimeEdit = qobject_cast<QDateTimeEdit *>(obj);
			if (pDateTimeEdit != nullptr)
			{
				if (strLanguage.compare("ChineseT") == 0)
					pDateTimeEdit->setLocale(QLocale(QLocale::Chinese, QLocale::Taiwan));  
				else if (strLanguage.compare("English") == 0)
					pDateTimeEdit->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
				else if (strLanguage.compare("Korea") == 0)
					pDateTimeEdit->setLocale(QLocale(QLocale::Korean, QLocale::SouthKorea));
			}

			auto pDateEdit = qobject_cast<QDateEdit *>(obj);
			if (pDateEdit != nullptr)
			{
				if (strLanguage.compare("ChineseT") == 0)
					pDateEdit->setLocale(QLocale(QLocale::Chinese, QLocale::Taiwan));
				else if (strLanguage.compare("English") == 0)
					pDateEdit->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
				else if (strLanguage.compare("Korea") == 0)
					pDateEdit->setLocale(QLocale(QLocale::Korean, QLocale::SouthKorea));
			}

			auto pTableWidget = qobject_cast<QTableWidget *>(obj);
			if (pTableWidget != nullptr)
			{
				for (int i = 0; i < pTableWidget->columnCount(); i++)
				{
					if (pTableWidget->horizontalHeaderItem(i))
						pTableWidget->horizontalHeaderItem(i)->setText(translate(pTableWidget->horizontalHeaderItem(i)->text()));
				}
			}

			auto pTabWidget = qobject_cast<QTabWidget *>(obj);
			if (pTabWidget != nullptr)
			{
				for (int i = 0; i < pTabWidget->count(); i++)
					pTabWidget->setTabText(i, translate(pTabWidget->tabText(i)));
			}

			auto pTreeWidget = qobject_cast<QTreeWidget *>(obj);
			if (pTreeWidget != nullptr)
			{
				QTreeWidgetItemIterator item(pTreeWidget);
				while (*item)
				{
					(*item)->setText(0, translate((*item)->text(0)));

					QTreeWidgetItem *parent = (*item)->parent();
					if (NULL == parent)
						(*item)->setHidden(true);

					++item;
				}
			}

			auto pComboBox = qobject_cast<QComboBox *>(obj);
			if (pComboBox != nullptr)
			{
				for (int i = 0; i < pComboBox->count(); i++)
					pComboBox->setItemText(i, translate(pComboBox->itemText(i)));
			}
		}
	}

}

//翻译
QString ControlsTranslate::translate(QString strName)
{
	auto hash = m_pLanguageXml->getItemsHash();
	auto itFind = hash.find(strName);
	if (itFind != hash.end())
		return hash[strName];
	else
		return strName;

	return "";
}

//初始化
void ControlsTranslate::initialTranslateFile()
{
	QString strFilePath = QApplication::applicationDirPath() + "/transalte.xml";

	// 先初始化
	if (!m_objectList.isEmpty())
	{
		foreach(QObject *obj, m_objectList)
		{
			
			auto pPushButton = qobject_cast<QPushButton *>(obj);
			if (pPushButton != nullptr)
				m_pLanguageXml->insertItem(pPushButton->text(), "");

			auto pToolButton = qobject_cast<QToolButton *>(obj);
			if (pToolButton != nullptr)
				m_pLanguageXml->insertItem(pToolButton->text(), "");

			auto pWidget = qobject_cast<QWidget *>(obj);
			if (pWidget != nullptr)
				m_pLanguageXml->insertItem(pWidget->windowTitle(), "");

			auto pGroupBox = qobject_cast<QGroupBox *>(obj);
			if (pGroupBox != nullptr)
				m_pLanguageXml->insertItem(pGroupBox->title(), "");

			auto pLabel = qobject_cast<QLabel *>(obj);
			if (pLabel != nullptr)
				m_pLanguageXml->insertItem(pLabel->text(), "");

			auto pCheckBox = qobject_cast<QCheckBox *>(obj);
			if (pCheckBox != nullptr)
				m_pLanguageXml->insertItem(pCheckBox->text(), "");

			auto pRadioButton = qobject_cast<QRadioButton *>(obj);
			if (pRadioButton != nullptr)
				m_pLanguageXml->insertItem(pRadioButton->text(), "");

			auto pLineEdit = qobject_cast<QLineEdit *>(obj);
			if (pLineEdit != nullptr)
			{
				m_pLanguageXml->insertItem(pLineEdit->text(), "");
				m_pLanguageXml->insertItem(pLineEdit->placeholderText(), "");
			}

			auto pTableWidget = qobject_cast<QTableWidget *>(obj);
			if (pTableWidget != nullptr)
			{
				for (int i = 0; i < pTableWidget->columnCount(); i++)
				{
					if (pTableWidget->horizontalHeaderItem(i))
						m_pLanguageXml->insertItem(pTableWidget->horizontalHeaderItem(i)->text(), "");
				}
			}

			auto pTabWidget = qobject_cast<QTabWidget *>(obj);
			if (pTabWidget != nullptr)
			{
				for (int i = 0; i < pTabWidget->count(); i++)
					m_pLanguageXml->insertItem(pTabWidget->tabText(i), "");
			}

			auto pTreeWidget = qobject_cast<QTreeWidget *>(obj);
			if (pTreeWidget != nullptr)
			{
				QTreeWidgetItemIterator item(pTreeWidget);
				while (*item)
				{
					m_pLanguageXml->insertItem((*item)->text(0), "");

					QTreeWidgetItem *parent = (*item)->parent();
					if (NULL == parent)
						(*item)->setHidden(true);

					++item;
				}
			}

			auto pComboBox = qobject_cast<QComboBox *>(obj);
			if (pComboBox != nullptr)
			{
				for (int i = 0; i < pComboBox->count(); i++)
					m_pLanguageXml->insertItem(pComboBox->itemText(i), "");
			}
		}
	}

	// 再赋值
	m_pLanguageXml->readItems(strFilePath);

	if (m_objectList.size() > 0)
		m_pLanguageXml->saveItems(strFilePath);
}

三、程序链接

https://download.csdn.net/download/u013083044/87262164

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值