文章目录
- 下载 Qt Creator 源码
- 手动翻译多语言
- 自动翻译多语言
下载 Qt Creator 源码
Github: https://github.com/qt/qttools
笔记打算用 Qt 5.12.12 来编译 qt creator-linguist
所以笔者下载的是 tag - 5.12.12 ,解压后如下,先删除多余的文件,后续还要删除更多文件方便清晰的查看源码.
添加 header.pri
##指定 QtUiTools/private 所在目录.
INCLUDEPATH += D:/Qt/5.12.12/5.12.12/msvc2017_64/include/QtUiTools
INCLUDEPATH += D:/Qt/5.12.12/5.12.12/msvc2017_64/include/QtUiTools/5.12.12/QtUiTools
手动翻译多语言
在 .pro 中增加多语言, 在 Qt Creator 生成
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Ch/Ch.ts #英文->中文
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/ChT/ChT.ts #英文->中文繁体
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Japanese/Japanese.ts #英文->日语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Russian/Russian.ts #英文->俄语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/German/German.ts #英文->德语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/French/French.ts #英文->法语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Italian/Italian.ts #英文->意大利语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Polish/Polish.ts #英文->波兰语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Thai/Thai.ts #英文->泰语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Hindi/Hindi.ts #英文->印地语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Indonesian/Indonesian.ts #英文->印尼语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Spanish/Spanish.ts #英文->西班牙语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Portuguese/Portuguese.ts #英文->葡萄牙语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Korean/Korean.ts #英文->朝鲜语
TRANSLATIONS += $$PWD/bin/release/msvc_x64/appInfo/setting/Vietnamese/Vietnamese.ts #英文->越南语
更新翻译: 更新.ts 文件
发布翻译: 把 .ts 文件生成 .qm 文件,最终利用 QTranslator 类完成界面上显示的翻译.
文件->打开 .ts 文件
自动翻译多语言
.ts 格式, 表示未翻译
直接翻译.ts 文件的核心代码
bool LinguistDlg::translateTsFileUnfinished(const QString & strSrc,const QString & strDest,const QString &sourceLanguage,const QString &targetLanguage)
{QDomDocument doc;QString errorMsg;int errorLine = 0;int errorColumn = 0;{QFile iFile(strSrc);if(!iFile.open(QIODevice::ReadOnly)){setLastError(tr("Open file failed:%1").arg(iFile.errorString()));return false;}QByteArray baContent = iFile.readAll();bool bPase = doc.setContent(baContent,&errorMsg,&errorLine,&errorColumn);if(!bPase){setLastError(tr("Parser failed:[line:%1 column:%2]:%3").arg(errorLine).arg(errorColumn).arg(errorMsg));return false;}iFile.close();}QVariantMap map;map["source_language"] = sourceLanguage;map["target_language"] = targetLanguage;SCDebug<<"sourceLanguage:"<<sourceLanguage;SCDebug<<"targetLanguage:"<<targetLanguage;//自定义术语表,这里把 %1 ~ %9 不让飞书翻译.QVariantList glossary;for(int k=0; k<10; ++k){QVariantMap cMap;cMap.insert("from",QString("%%1").arg(k));cMap.insert("to",QString("%%1").arg(k));glossary.append(cMap);}//!!!Fix <message numerus=yes>标记为数字,如果不为数字则报错.map["glossary"] = glossary;QDomElement elemntTS = doc.elementsByTagName("TS").at(0).toElement();QDomNodeList nlContexts = elemntTS.elementsByTagName("context");int maxValue = nlContexts.count();for(int i=0;i<nlContexts.count(); i++){UiClass::init()->showWaittingWidget(tr("Translating...(%1/%2)").arg(i).arg(maxValue),this);QDomNode nodeContext = nlContexts.at(i);QDomNodeList nlMessages = nodeContext.toElement().elementsByTagName("message");for(int j=0;j<nlMessages.count(); ++j){QDomNode nodeMessage = nlMessages.at(j);QDomElement elementTranslation = nodeMessage.firstChildElement("translation");QString type = elementTranslation.attribute("type");if ("unfinished" == type){//未翻译的//nodeMessage.removeChild(elementTranslation);QDomElement elementSource = nodeMessage.firstChildElement("source");QDomElement elementLocation = nodeMessage.firstChildElement("location");map["text"] = elementSource.text();if(!_httpApiTr.translate(map)){SCWarning<<"Error:elementLocation:"<<elementLocation.attribute("filename")<<map["text"].toString();continue;}auto translate_text = map.value("translate_text").toString();//这里一定要先删除旧的再追加新的,否则无效.elementTranslation.removeAttribute("type");elementTranslation.removeChild(elementTranslation.firstChild());auto nodeText = doc.createTextNode(translate_text);elementTranslation.appendChild(nodeText);//SCDebug<<"Sucess:elementLocation:"<<elementLocation.attribute("filename")<<map["text"].toString()<<translate_text;}}}{QFile iFileWrite(strDest);if( !iFileWrite.open(QIODevice::WriteOnly)){setLastError(tr("Open file failed:%1").arg(iFileWrite.errorString()));return false;}iFileWrite.write(doc.toByteArray());iFileWrite.close();}return true;
}