From: DUC ANH HOANG Date: Tue, 23 Apr 2024 13:49:12 +0000 (+0200) Subject: LightApp_msg_customize: initialize X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=3b5e2ee9c56caedd44757a791c93e26d129b0c35;p=modules%2Fgui.git LightApp_msg_customize: initialize --- diff --git a/src/LightAppMsg_Customize/AppNameCustomize.cpp b/src/LightAppMsg_Customize/AppNameCustomize.cpp new file mode 100644 index 000000000..0defc2e1b --- /dev/null +++ b/src/LightAppMsg_Customize/AppNameCustomize.cpp @@ -0,0 +1,138 @@ +#include "LightApp_msg.h" + +using namespace LightApp_msg; + +bool qmTots(const QString &qmFilePath, const QString &tsFilePath) { + QProcess process; + + QString command = "lconvert"; + QStringList arguments; + arguments << qmFilePath; + arguments << "-o"; + arguments << tsFilePath; + + process.start(command, arguments); + + process.waitForFinished(); + + if (process.exitCode()== 0) + return true; + else + return false; +} + +bool tsToqm(const QString &tsFilePath, const QString &qmFilePath){ + QProcess process; + + QString command = "lrelease"; + QStringList arguments; + arguments << tsFilePath; + arguments << "-qm"; + arguments << qmFilePath; + + process.start(command, arguments); + + process.waitForFinished(); + + if (process.exitCode()== 0) + return true; + else + { + qDebug()<< QString("Can not compile %1 to %2").arg(tsFilePath,qmFilePath); + return false; + } +} + +bool LightApp_msg_patching(const QString& app_name,const QString& qmFilePath) +{ + // Get GUI_ROOT_DIR + QByteArray gui_root_dir_env = qgetenv("GUI_ROOT_DIR"); + if (gui_root_dir_env.isEmpty()) + { + qCritical()<< "GUI_ROOT_DIR must be defined"; + return false; + } + + QString gui_root_dir = QString::fromUtf8(gui_root_dir_env); + QDir gui_root_qdir(gui_root_dir); + QString qmFilePathAbs(gui_root_qdir.filePath(qmFilePath)); + QStringList qmFilePathAbs_splited = qmFilePathAbs.split("/"); + QString tsFilePath(qmFilePathAbs_splited[qmFilePathAbs_splited.size() - 1]); + tsFilePath.replace(".qm",".ts"); + + if (qmTots(qmFilePathAbs,tsFilePath)) + qInfo() <<"qmTots OK"; + else + { + qDebug() <<"qmTots KO"; + return false; + } + + try + { + QFile input_tsFile(tsFilePath); + if(!input_tsFile.open(QFile::ReadOnly | QFile::Text)) { + qDebug() << "Could not open tsfile"; + throw; + } + std::unique_ptr> Context_list(new QList); + + LightApp_msg_handling LAMsg_handling(std::move(Context_list)); + + LAMsg_handling.read(&input_tsFile); + + LAMsg_handling.GetContextbyName("@default")->GetMessagebySrc("APP_NAME")->Set_Translation(app_name); + + input_tsFile.close(); + + // Write in tsfile + QFile output_tsFile(tsFilePath); + if (!output_tsFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug() << "Cannot open file for writing."; + throw; + } + QTextStream out(&output_tsFile); + out << LAMsg_handling.write(); + output_tsFile.close(); + } + catch(...) + { + qCritical() << "Something wrong with " << qmFilePathAbs; + return false; + } + + // write to qm file + if (tsToqm(tsFilePath,qmFilePathAbs)) + qInfo() <<"tsToqm OK"; + else + { + qDebug() <<"tsToqm KO"; + return false; + } + + return true; + +} +int main(int argc, char *argv[]) { + + if (argc < 2) { + qDebug() << "Usage: app_name must be declared"; + return 1; + } + + QString app_name = QString::fromUtf8(argv[1]); + QStringList qmFilePath_list = {"share/salome/resources/gui/LightApp_msg_fr.qm", + "share/salome/resources/gui/LightApp_msg_en.qm", + "share/salome/resources/gui/LightApp_msg_ja.qm"}; + + for (const QString& qmFilePath:qmFilePath_list) + { + if(!LightApp_msg_patching(app_name,qmFilePath)) + { + qDebug() << "LightApp_msg_patching failed!!!!"; + return 1; + } + } + return 0; + +} diff --git a/src/LightAppMsg_Customize/AppNameCustomize.pro b/src/LightAppMsg_Customize/AppNameCustomize.pro new file mode 100644 index 000000000..5fa86fcc1 --- /dev/null +++ b/src/LightAppMsg_Customize/AppNameCustomize.pro @@ -0,0 +1,7 @@ +TEMPLATE = app +QT += core gui +CONFIG += c++11 +SOURCES += \ + AppNameCustomize.cpp \ + LightApp_msg.cpp +HEADERS += LightApp_msg.h \ No newline at end of file diff --git a/src/LightAppMsg_Customize/LightApp_msg.cpp b/src/LightAppMsg_Customize/LightApp_msg.cpp new file mode 100644 index 000000000..716801495 --- /dev/null +++ b/src/LightAppMsg_Customize/LightApp_msg.cpp @@ -0,0 +1,163 @@ +#include "LightApp_msg.h" + +using namespace LightApp_msg; + +QString CustomEntityResolver::resolveUndeclaredEntity(const QString &name) { + if (name == "&") + return "&"; + else if (name == """) + return """; + else if (name == "'") + return "'"; + return QString(); +} + + +QString Context::ExportContext() +{ + QString context_string; + context_string.append(QString("\n")); + context_string.append(QString("%1\n").arg(Context_name)); + foreach (const Message& msg, Message_list) + { + context_string.append(QString("\n%1\n%2\n\n").arg(msg.GetSource(),msg.GetTranslation())); + } + context_string.append(QString("\n")); + + return context_string; +} + +Message* Context::GetMessagebySrc(const QString& src) +{ + uint i(0); + while(i < Message_list.size() && Message_list[i].GetSource() != src) + i++; + if (i < Message_list.size()) + return &Message_list[i]; +} + + + +Context* LightApp_msg_handling::GetContextbyName(const QString& name) +{ + int i(0); + while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) + i++; + if (i < Context_list->size()) + return &(*Context_list)[i]; + else + { + qDebug()<<"Context with name " << name << " doesn't exist in LightApp_msg_handling"; + return nullptr; + } +} + +bool LightApp_msg_handling::HaveContext(const QString& name) +{ + int i(0); + while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name) + i++; + if (i < Context_list->size()) + return true; + else + return false; +} + +QString LightApp_msg_handling::write() +{ + QString updateXml; + updateXml.append(header); + for(auto ct:*Context_list) + updateXml.append(ct.ExportContext()); + updateXml.append(""); + + return updateXml; + +} +void LightApp_msg_handling::read(QIODevice *device) +{ + + //header + QTextStream textStream(device); + // Read 3 first header line of ts file + for (int i = 0; i < 3 && !textStream.atEnd(); ++i) { + header.append(textStream.readLine() + "\n"); + } + device->seek(0); + + //context + xmlReader.setDevice(device); + //CustomEntityResolver resolver; + //xmlReader.setEntityResolver(&resolver); + + while(xmlReader.readNextStartElement()){ + if(xmlReader.name().toString() == "context") + readContext(); + } + +} + +void LightApp_msg_handling::readContext() +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "context"); + + while(xmlReader.readNextStartElement()){ + if(xmlReader.name() == "name") + { + QString name = xmlReader.readElementText(); + if (!HaveContext(name)) + Context_list->append(Context(name)); + else + qWarning()<<"Context with name " << name << " is already appended in LightApp_msg_handling"; + } + else if (xmlReader.name() == "message") + { + readMessage(); + } + else + xmlReader.skipCurrentElement(); + } +} + +void LightApp_msg_handling::readMessage() +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "message"); + Message msg = Message("",""); + Message& msg_ref = msg; + while(xmlReader.readNextStartElement()){ + if(xmlReader.name() == "source") + readSrc(msg_ref); + else if(xmlReader.name() == "translation") + readTranslation(msg_ref); + else + xmlReader.skipCurrentElement(); + + } + + (*Context_list)[Context_list->size() - 1].AddMsg(msg); +} + +void LightApp_msg_handling::readSrc(Message& msg) +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "source"); + QString src = xmlReader.readElementText(); + + msg.Set_Source(src); +} + +void LightApp_msg_handling::readTranslation(Message& msg) +{ + Q_ASSERT(xmlReader.isStartElement() && + xmlReader.name() == "translation"); + + QString trl = xmlReader.readElementText(); + trl.replace("&","&"); + trl.replace("\"","""); + trl.replace("\'","'"); + trl.replace("<","<"); + trl.replace(">",">"); + msg.Set_Translation(trl); +} diff --git a/src/LightAppMsg_Customize/LightApp_msg.h b/src/LightAppMsg_Customize/LightApp_msg.h new file mode 100644 index 000000000..c4b322f60 --- /dev/null +++ b/src/LightAppMsg_Customize/LightApp_msg.h @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace LightApp_msg +{ + +class Message +{ + public: + Message(const QString& src, const QString& trl):source(src),translation(trl){}; + QString GetSource() const { return source ;} + QString GetTranslation() const { return translation ;} + void Set_Source(const QString& str) { source = str; } + void Set_Translation(const QString& str) { translation = str; } + private: + QString source; + QString translation; +}; + +class Context +{ + public: + Context(const QString& name):Context_name(name){}; + QString GetContext_name(){return Context_name;} + void AddMsg(Message& msg) {Message_list.append(msg);} + void AddMsg(const QString& src, const QString& trl) {Message_list.append(Message(src,trl));} + Message* GetMessagebySrc(const QString&); + QList& GetMessageList(){return Message_list;} + QString ExportContext(); + private: + QString Context_name; + QList Message_list; +}; + +class LightApp_msg_handling +{ +public: + LightApp_msg_handling(std::unique_ptr> Context_list): + Context_list(std::move(Context_list)){}; + void read(QIODevice *device); + QString write(); + Context* GetContextbyName(const QString &name); + bool HaveContext(const QString &name); +private: + QXmlStreamReader xmlReader; + std::unique_ptr> Context_list; + QString header; + + void readContext(); + void readMessage(); + void readSrc(Message& msg); + void readTranslation(Message& msg); +}; + +} + +class CustomEntityResolver : public QXmlStreamEntityResolver { +public: + QString resolveUndeclaredEntity(const QString &name); +}; \ No newline at end of file