当前位置:网站首页>QT learning: use non TS files such as json/xml to realize multilingual internationalization
QT learning: use non TS files such as json/xml to realize multilingual internationalization
2022-07-29 08:26:00 【Shangguan Hongzhu】
Be careful : If not CSDN The website shows this article , Please click at the bottom “ Read the original ” Read this article !
QT Study : Use JSON/XML Such as the ts Realize multilingual internationalization of files
qt The default internationalization is to use
Linguist + .ts, To achieve internationalization . However, there may be language documents in other formats used by the translation department within the company , Or build a translation website by yourself , What you can download is definitely not ts file , It is a common file format , such as xml/json wait . Now we use JSON File to store the translated key And the translated value value, But the project source code is still used internally qt Provided
QObject::tr("key") as well as qml The use of qsTr("key") Method to use translation . The main method is also relatively simple :
- 1、 Use one map type , Storage JSON The resolved key-value value
- 2、 Create a new class
MyTranslatorInherited fromQTranslator, And then reloadQTranslator::translateMethod - 3、 Use
QApplication::instance()->installTranslatorregisterMyTranslatorExample - 4、 Code internal use
QObject::tr("key")Way to achieve internationalization
Say below MyTranslator The implementation of the
analysis Json
Mainly is to JSON In the document key-value, Store it in map In the table , For the back translate Use
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;
}
heavy load translate
according to [virtual] QString QTranslator::translate(const char *context, const char *sourceText, const char *disambiguation = nullptr, int n = -1) const The prototype of the , Just reload one . Mainly through the incoming sourceText, In front of map Find the corresponding in the table value, Translation string , Return to .
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)));
}
register MyTranslator example
Registration instance , Be sure to be on the main thread , The other is to use QObject::tr("key") Previous registration .
QApplication::instance()->installTranslator(&MyTranslator::getInstance());
Running results

chinese JSON file :
{
"button_name": " Button name ",
"window_name": " Main window name "
}
The complete implementation can search official account through wechat :“ Shangguan Hongzhu ”, Pay attention to and reply to :“qt_language”, Get resource links . If you have any questions, you can also leave a message in official account .
WeChat official account search :“ Shangguan Hongzhu ”, Follow and leave a message for consultation , It can meet all kinds of needs .
边栏推荐
- Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
- The first week of postgraduate freshman training: deep learning and pytorch Foundation
- Leetcode Hot 100 (brush question 9) (301/45/517/407/offer62/mst08.14/)
- 125kHz wake-up function 2.4GHz single transmitter chip-si24r2h
- Unity shader learning (VI) achieving radar scanning effect
- Virtual augmentation and reality Part 2 (I'm a Firebird)
- Osgsimplegl3 example analysis
- 集群使用规范
- Cmake setting vs Startup running environment path
- Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
猜你喜欢

Unity multiplayer online framework mirror learning record (I)
![[opencv] - Operator (Sobel, canny, Laplacian) learning](/img/24/4e40408b61ec2c714b750b05a534c7.png)
[opencv] - Operator (Sobel, canny, Laplacian) learning

Flask reports an error runtimeerror: the session is unavailable because no secret key was set

Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT

Deep learning (2): image and character recognition

Gan: generate adversarial networks

01-01-osg GL3 environment setup

Crawling JS encrypted data of playwright actual combat case

Clion+opencv+aruco+cmake configuration

【OpenCV】-算子(Sobel、Canny、Laplacian)学习
随机推荐
The computer video pauses and resumes, and the sound suddenly becomes louder
ROS tutorial (Xavier)
Hc-sr04 use method and routine of ultrasonic ranging module (STM32)
Week 1 task deep learning and pytorch Foundation
Eggjs create application knowledge points
[opencv] - Operator (Sobel, canny, Laplacian) learning
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
集群使用规范
Search and recall classic questions (eight queens)
Usage of torch.tensor.to
RPC and rest
深度学习(2):图片文字识别
PostgreSQL manually creates hikaridatasource to solve the error cannot commit when autocommit is enabled
Hal library learning notes - 8 concept of serial communication
HC-SR04超声波测距模块使用方法和例程(STM32)
Day4: SQL server is easy to use
为了速率创建线程池,启动核心线程
Day15 (day16 extension): file contains vulnerability
Back up Google or other browser plug-ins
Cmake setting vs Startup running environment path