当前位置:网站首页>QT学习:使用JSON/XML等非ts文件实现多语言国际化
QT学习:使用JSON/XML等非ts文件实现多语言国际化
2022-07-29 05:07:00 【上官宏竹】
注意:如果不是CSDN网站显示本篇文章,请于底部点击“阅读原文”来阅读本篇文章!
QT学习:使用JSON/XML等非ts文件实现多语言国际化
qt默认的国际化是使用
Linguist + .ts,来实现国际化的。但一般公司内部可能存在翻译部门使用其他格式的语言文件,或者自己搭建了一个翻译网站,你能下载到的肯定不会是ts文件,而是比较常见的文件格式,比如xml/json等等。 现在我们使用JSON文件来存放翻译的key和翻译后的值value,但项目源码内部还是使用qt提供的
QObject::tr("key")以及qml使用的 qsTr("key")方法来使用翻译。 主要的方法也比较简单:
- 1、使用一个map类型,存储JSON解析后的key-value值
- 2、新建一个类
MyTranslator继承自QTranslator,然后重载QTranslator::translate方法 - 3、使用
QApplication::instance()->installTranslator注册MyTranslator的实例 - 4、代码内部使用
QObject::tr("key")方式实现国际化
下面说下MyTranslator的实现
解析Json
主要是将JSON文件中的key-value,解析后存放在map表中,供后面的translate使用
void MyTranslator::LoadLocalCachedInfo(QString langCode)
{
QString fileName = (QApplication::applicationDirPath() +
TRANS_DIR + "/" + LOCAL_TRANS_FILE_NAME).arg(langCode);
QFile langFile(fileName);
if(!langFile.exists()) {
return;
}
langFile.open(QIODevice::ReadOnly);
QByteArray byteInfo = langFile.readAll();
if (!LoadJsonTransInfo(byteInfo)) {
return;
}
emit LanguageChanged();
}
bool MyTranslator::LoadJsonTransInfo(QByteArray& byteinfo)
{
QJsonObject jsonObj;
QJsonParseError error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(byteinfo, &error));
if (jsonDoc.isEmpty() || jsonDoc.isNull()) {
QString errorStr = error.errorString();
return false;
}
m_loadedTransInfo.clear();
for(auto& transKey : jsonDoc.object().keys()) {
m_loadedTransInfo[transKey.toStdString()] =
jsonDoc.object()[transKey].toString().toStdString();
}
return true;
}
重载translate
按照[virtual] QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const的原型,重载一个即可。主要是通过传入的sourceText,在前面的map表中找到对应的value,即翻译字符串,返回即可。
QString MyTranslator::translate(const char *context,
const char *sourceText,
const char *disambiguation, int n) const
{
// if no result return original
if(m_loadedTransInfo.count(sourceText) <= 0) {
return sourceText;
}
// from loaded value
return QString::fromStdString(m_loadedTransInfo.at(std::string(sourceText)));
}
注册MyTranslator实例
注册实例,一定要在主线程,另一个是要在使用QObject::tr("key")之前注册。
QApplication::instance()->installTranslator(&MyTranslator::getInstance());
运行结果

中文JSON文件:
{
"button_name": "按钮名字",
"window_name": "主窗口名字"
}
完整实现可以微信搜索公众号:“上官宏竹”,关注并回复:“qt_language”,获取资源链接。有任何疑问也可以公众号里留言咨询。
微信公众号搜索:“上官宏竹”,关注并留言咨询,可接各类需求。
边栏推荐
- How does excel filter out the content you want? Excel table filtering content tutorial
- Wechat picture identification
- [untitled]
- Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
- Solve the warning prompt of MySQL mapping file
- 学习数据库的第一个程序
- 源码编译pytorch坑
- The representation of time series analysis: is the era of learning coming?
- NumPy基础
- P1009 [noip1998 popularization group] sum of factorials
猜你喜欢

How to install Office2010 installation package? How to install Office2010 installation package on computer

电脑无法打开excel表格怎么办?excel打不开的解决方法

Solution | get the relevant information about the current employees' highest salary in each department |

The method and detailed code of automatically pop-up and QQ group when players visit the website

Solve the warning prompt of MySQL mapping file

【微信小程序--解决display:flex最后一行对齐问题。(不连续排列会分到两边)】

Activity workflow table structure learning

Deadlock analysis using jstack, jconsole, and jvisualvm

Use annotation test in idea

Understand activity workflow
随机推荐
Apache POI实现Excel导入读取数据和写入数据并导出
ARFoundation从零开始8-Geospatial API(地理空间)开发
How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
Solution | get the relevant information about the current employees' highest salary in each department |
What if excel is stuck and not saved? The solution of Excel not saved but stuck
Reply from the Secretary of jindawei: the company is optimistic about the market prospect of NMN products and has launched a series of products
Excel怎么筛选出自己想要的内容?excel表格筛选内容教程
How does word view document modification traces? How word views document modification traces
Deadlock analysis using jstack, jconsole, and jvisualvm
网安学习-内网安全1
What servers are needed to build mobile app
Architecture analysis of three-tier project and parameter name injection of construction method
JDBC statement + resultset introduction
SM整合原来这么简单,步骤清晰(详细)
NumPy基础
Pivot table of odoo development tutorial
开区网站打开自动播放音乐的添加跟修改教程
JS daily question (11)
Mysql语句中的函数
ARFoundation从零开始9-AR锚点(AR Anchor)