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"
15 * \class Config_TSReader
17 * \brief Class for reading translations from TS files (an XML format).
19 class Config_TSReader : public Config_XMLReader
23 /// \param theTSFile name of TS file
24 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
26 /// Returns content of TS file
27 const Config_Translator::Translator& translator() const { return myTranslator; }
29 /// Returns codecs defined in TS files
30 const Config_Translator::Dictionary& codecs() const { return myCodecs; }
33 /// Overloaded method. Defines how to process each node
34 virtual void processNode(xmlNodePtr theNode);
36 Config_Translator::Translator myTranslator;
37 Config_Translator::Dictionary myCodecs;
40 void Config_TSReader::processNode(xmlNodePtr theNode)
42 static std::string aName;
43 static std::string aSource;
44 std::string aTranslat;
46 if (isNode(theNode, "context", NULL)) {
48 } else if (isNode(theNode, "name", NULL)) {
49 aName = getContent(theNode);
50 myCodecs[aName] = encoding();
51 } else if (isNode(theNode, "message", NULL)) {
53 } else if (isNode(theNode, "source", NULL)) {
54 aSource = getContent(theNode);
55 } else if (isNode(theNode, "translation", NULL)) {
56 aTranslat = getContent(theNode);
57 if ((aName.size() > 0) && (aSource.size() > 0))
58 myTranslator[aName][aSource] = aTranslat;
62 //******************************************************************************
63 //******************************************************************************
64 //******************************************************************************
65 Config_Translator::Translator Config_Translator::myTranslator;
66 Config_Translator::Dictionary Config_Translator::myCodecs;
69 #ifdef MISSED_TRANSLATION
70 Config_Translator::Translator Config_Translator::myMissed;
74 bool Config_Translator::load(const std::string& theFileName)
76 Config_TSReader aReader(theFileName);
79 const Translator& aTranslator = aReader.translator();
80 Translator::const_iterator aIt;
82 Dictionary aDictionary;
83 for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
84 aContext = (*aIt).first;
85 aDictionary = (*aIt).second;
86 if (myTranslator.count(aContext) == 0) {
87 myTranslator[aContext] = aDictionary;
89 Dictionary::const_iterator aDictIt;
90 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
91 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
96 const Dictionary aCodecs = aReader.codecs();
97 Dictionary::const_iterator aDictIt;
98 for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) {
99 myCodecs[aDictIt->first] = aDictIt->second;
104 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
106 std::string aContext = theInfo.context();
107 std::string aMessage = theInfo.messageString();
108 std::list<std::string> aParameters = theInfo.parameters();
109 return translate(aContext, aMessage, aParameters);
112 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
114 std::string aResult = theString;
115 std::list<std::string>::const_iterator aIt;
119 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
121 sprintf(aBuf, "%d", i);
122 std::string aCode = std::string("%") + std::string(aBuf);
123 size_t aPos = aResult.find(aCode);
124 if (aPos != std::string::npos) {
125 std::string aFirst = aResult.substr(0, aPos);
126 std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
127 aResult = aFirst + aParam + aLast;
133 std::string Config_Translator::translate(const std::string& theContext,
134 const std::string& theMessage,
135 const std::list<std::string>& theParams)
137 if (myTranslator.count(theContext) > 0) {
138 if (myTranslator[theContext].count(theMessage) > 0) {
139 std::string aTranslation = myTranslator[theContext][theMessage];
140 if (theParams.size() > 0) {
141 aTranslation = insertParameters(aTranslation, theParams);
143 if (aTranslation.size() > 0)
147 std::string aMsg = theMessage;
148 if (theParams.size() > 0) {
149 aMsg = insertParameters(aMsg, theParams);
152 #ifdef MISSED_TRANSLATION
153 myMissed[theContext][theMessage] = "";
160 std::string Config_Translator::codec(const std::string& theContext)
162 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
167 #ifdef MISSED_TRANSLATION
168 void Config_Translator::saveMissedTranslations()
170 if (myMissed.size() == 0)
173 char* aPath = getenv("ROOT_DIR");
175 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
177 std::string aFile = aPath + std::string("/MissedTranslation.ts");
179 std::ofstream oFStream;
182 int aa = remove(aFile.c_str());
184 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
185 if (oFStream.is_open()) {
186 Translator::const_iterator aContIt;
187 Dictionary::const_iterator aDictIt;
188 std::string aContext;
192 oFStream << "<TS version=\"2.0\">" << std::endl;
193 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
194 oFStream << "<context>" << std::endl;
196 aContext = aContIt->first;
197 oFStream << " <name>" << aContext << "</name>" << std::endl;
199 aDict = aContIt->second;
200 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
201 oFStream << " <message>" << std::endl;
202 aSrc = aDictIt->first;
204 oFStream << " <source>" << aSrc << "</source>" << std::endl;
205 oFStream << " <translation>" << "</translation>" << std::endl;
207 oFStream << " </message>" << std::endl;
209 oFStream << "</context>" << std::endl;
212 oFStream << "</TS>" << std::endl;