1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Config_Translator.cpp
4 // Created: 31 May 2016
5 // Author: Vitaly SMETANNIKOV
7 #include "Config_Translator.h"
8 #include "Config_XMLReader.h"
9 #include "Config_Common.h"
14 class Config_TSReader : public Config_XMLReader
17 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
19 const Config_Translator::Translator& translator() const { return myTranslator; }
22 /// Overloaded method. Defines how to process each node
23 virtual void processNode(xmlNodePtr theNode);
25 Config_Translator::Translator myTranslator;
28 void Config_TSReader::processNode(xmlNodePtr theNode)
30 static std::string aName;
31 static std::string aSource;
32 std::string aTranslat;
34 if (isNode(theNode, "context", NULL)) {
36 } else if (isNode(theNode, "name", NULL)) {
37 aName = getContent(theNode);
38 } else if (isNode(theNode, "message", NULL)) {
40 } else if (isNode(theNode, "source", NULL)) {
41 aSource = getContent(theNode);
42 } else if (isNode(theNode, "translation", NULL)) {
43 aTranslat = getContent(theNode);
44 if ((aName.size() > 0) && (aSource.size() > 0))
45 myTranslator[aName][aSource] = aTranslat;
49 //******************************************************************************
50 //******************************************************************************
51 //******************************************************************************
52 Config_Translator::Translator Config_Translator::myTranslator;
54 #ifdef MISSED_TRANSLATION
55 Config_Translator::Translator Config_Translator::myMissed;
59 bool Config_Translator::load(const std::string& theFileName)
61 Config_TSReader aReader(theFileName);
64 const Translator& aTranslator = aReader.translator();
65 Translator::const_iterator aIt;
67 Dictionary aDictionary;
68 for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
69 aContext = (*aIt).first;
70 aDictionary = (*aIt).second;
71 if (myTranslator.count(aContext) == 0) {
72 myTranslator[aContext] = aDictionary;
74 Dictionary::const_iterator aDictIt;
75 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
76 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
84 std::string Config_Translator::translate(std::shared_ptr<Events_InfoMessage> theInfo)
86 std::string aContext = theInfo->context();
87 std::string aMessage = theInfo->message();
88 std::list<std::string> aParameters = theInfo->parameters();
89 return translate(aContext, aMessage, aParameters);
93 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
95 std::string aResult = theString;
96 std::list<std::string>::const_iterator aIt;
100 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
102 sprintf_s(aBuf, "%d", i);
103 std::string aCode = std::string("%") + std::string(aBuf);
104 size_t aPos = aResult.find(aCode);
105 if (aPos != std::string::npos) {
106 std::string aFirst = aResult.substr(0, aPos);
107 std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
108 aResult = aFirst + aParam + aLast;
114 std::string Config_Translator::translate(const std::string& theContext,
115 const std::string& theMessage,
116 const std::list<std::string>& theParams)
118 if (myTranslator.count(theContext) > 0) {
119 if (myTranslator[theContext].count(theMessage) > 0) {
120 std::string aTranslation = myTranslator[theContext][theMessage];
121 if (theParams.size() > 0) {
122 aTranslation = insertParameters(aTranslation, theParams);
127 std::string aMsg = theMessage;
128 if (theParams.size() > 0) {
129 aMsg = insertParameters(aMsg, theParams);
132 #ifdef MISSED_TRANSLATION
133 myMissed[theContext][theMessage] = "";
140 #ifdef MISSED_TRANSLATION
141 void Config_Translator::saveMissedTranslations()
143 if (myMissed.size() == 0)
146 char* aPath = getenv("ROOT_DIR");
148 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
150 std::string aFile = aPath + std::string("/MissedTranslation.ts");
152 std::ofstream oFStream;
155 int aa = remove(aFile.c_str());
157 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
158 if (oFStream.is_open()) {
159 Translator::const_iterator aContIt;
160 Dictionary::const_iterator aDictIt;
161 std::string aContext;
165 oFStream << "<TS version=\"2.0\">" << std::endl;
166 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
167 oFStream << "<context>" << std::endl;
169 aContext = aContIt->first;
170 oFStream << " <name>" << aContext << "</name>" << std::endl;
172 aDict = aContIt->second;
173 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
174 oFStream << " <message>" << std::endl;
175 aSrc = aDictIt->first;
177 oFStream << " <source>" << aSrc << "</source>" << std::endl;
178 oFStream << " <translation>" << "</translation>" << std::endl;
180 oFStream << " </message>" << std::endl;
182 oFStream << "</context>" << std::endl;
185 oFStream << "</TS>" << std::endl;