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; }
20 const Config_Translator::Dictionary& codecs() const { return myCodecs; }
23 /// Overloaded method. Defines how to process each node
24 virtual void processNode(xmlNodePtr theNode);
26 Config_Translator::Translator myTranslator;
27 Config_Translator::Dictionary myCodecs;
30 void Config_TSReader::processNode(xmlNodePtr theNode)
32 static std::string aName;
33 static std::string aSource;
34 std::string aTranslat;
36 if (isNode(theNode, "context", NULL)) {
38 } else if (isNode(theNode, "name", NULL)) {
39 aName = getContent(theNode);
40 myCodecs[aName] = encoding();
41 } else if (isNode(theNode, "message", NULL)) {
43 } else if (isNode(theNode, "source", NULL)) {
44 aSource = getContent(theNode);
45 } else if (isNode(theNode, "translation", NULL)) {
46 aTranslat = getContent(theNode);
47 if ((aName.size() > 0) && (aSource.size() > 0))
48 myTranslator[aName][aSource] = aTranslat;
52 //******************************************************************************
53 //******************************************************************************
54 //******************************************************************************
55 Config_Translator::Translator Config_Translator::myTranslator;
56 Config_Translator::Dictionary Config_Translator::myCodecs;
59 #ifdef MISSED_TRANSLATION
60 Config_Translator::Translator Config_Translator::myMissed;
64 bool Config_Translator::load(const std::string& theFileName)
66 Config_TSReader aReader(theFileName);
69 const Translator& aTranslator = aReader.translator();
70 Translator::const_iterator aIt;
72 Dictionary aDictionary;
73 for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
74 aContext = (*aIt).first;
75 aDictionary = (*aIt).second;
76 if (myTranslator.count(aContext) == 0) {
77 myTranslator[aContext] = aDictionary;
79 Dictionary::const_iterator aDictIt;
80 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
81 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
86 const Dictionary aCodecs = aReader.codecs();
87 Dictionary::const_iterator aDictIt;
88 for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) {
89 myCodecs[aDictIt->first] = aDictIt->second;
94 std::string Config_Translator::translate(std::shared_ptr<Events_InfoMessage> theInfo)
96 std::string aContext = theInfo->context();
97 std::string aMessage = theInfo->message();
98 std::list<std::string> aParameters = theInfo->parameters();
99 return translate(aContext, aMessage, aParameters);
103 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
105 std::string aResult = theString;
106 std::list<std::string>::const_iterator aIt;
110 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
112 sprintf(aBuf, "%d", i);
113 std::string aCode = std::string("%") + std::string(aBuf);
114 size_t aPos = aResult.find(aCode);
115 if (aPos != std::string::npos) {
116 std::string aFirst = aResult.substr(0, aPos);
117 std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
118 aResult = aFirst + aParam + aLast;
124 std::string Config_Translator::translate(const std::string& theContext,
125 const std::string& theMessage,
126 const std::list<std::string>& theParams)
128 if (myTranslator.count(theContext) > 0) {
129 if (myTranslator[theContext].count(theMessage) > 0) {
130 std::string aTranslation = myTranslator[theContext][theMessage];
131 if (theParams.size() > 0) {
132 aTranslation = insertParameters(aTranslation, theParams);
137 std::string aMsg = theMessage;
138 if (theParams.size() > 0) {
139 aMsg = insertParameters(aMsg, theParams);
142 #ifdef MISSED_TRANSLATION
143 myMissed[theContext][theMessage] = "";
150 std::string Config_Translator::codec(const std::string& theContext)
152 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
157 #ifdef MISSED_TRANSLATION
158 void Config_Translator::saveMissedTranslations()
160 if (myMissed.size() == 0)
163 char* aPath = getenv("ROOT_DIR");
165 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
167 std::string aFile = aPath + std::string("/MissedTranslation.ts");
169 std::ofstream oFStream;
172 int aa = remove(aFile.c_str());
174 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
175 if (oFStream.is_open()) {
176 Translator::const_iterator aContIt;
177 Dictionary::const_iterator aDictIt;
178 std::string aContext;
182 oFStream << "<TS version=\"2.0\">" << std::endl;
183 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
184 oFStream << "<context>" << std::endl;
186 aContext = aContIt->first;
187 oFStream << " <name>" << aContext << "</name>" << std::endl;
189 aDict = aContIt->second;
190 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
191 oFStream << " <message>" << std::endl;
192 aSrc = aDictIt->first;
194 oFStream << " <source>" << aSrc << "</source>" << std::endl;
195 oFStream << " <translation>" << "</translation>" << std::endl;
197 oFStream << " </message>" << std::endl;
199 oFStream << "</context>" << std::endl;
202 oFStream << "</TS>" << std::endl;