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 #pragma warning(disable : 4996) // for sprintf
19 * \class Config_TSReader
21 * \brief Class for reading translations from TS files (an XML format).
23 class Config_TSReader : public Config_XMLReader
27 /// \param theTSFile name of TS file
28 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
30 /// Returns content of TS file
31 const Config_Translator::Translator& translator() const { return myTranslator; }
33 /// Returns codecs defined in TS files
34 const Config_Translator::Dictionary& codecs() const { return myCodecs; }
37 /// Overloaded method. Defines how to process each node
38 virtual void processNode(xmlNodePtr theNode);
40 Config_Translator::Translator myTranslator;
41 Config_Translator::Dictionary myCodecs;
44 void Config_TSReader::processNode(xmlNodePtr theNode)
46 static std::string aName;
47 static std::string aSource;
48 std::string aTranslat;
50 if (isNode(theNode, "context", NULL)) {
52 } else if (isNode(theNode, "name", NULL)) {
53 aName = getContent(theNode);
54 myCodecs[aName] = encoding();
55 } else if (isNode(theNode, "message", NULL)) {
57 } else if (isNode(theNode, "source", NULL)) {
58 aSource = getContent(theNode);
59 } else if (isNode(theNode, "translation", NULL)) {
60 aTranslat = getContent(theNode);
61 if ((aName.size() > 0) && (aSource.size() > 0))
62 myTranslator[aName][aSource] = aTranslat;
66 //******************************************************************************
67 //******************************************************************************
68 //******************************************************************************
69 Config_Translator::Translator Config_Translator::myTranslator;
70 Config_Translator::Dictionary Config_Translator::myCodecs;
73 #ifdef MISSED_TRANSLATION
74 Config_Translator::Translator Config_Translator::myMissed;
78 bool Config_Translator::load(const std::string& theFileName)
80 Config_TSReader aReader(theFileName);
83 const Translator& aTranslator = aReader.translator();
84 Translator::const_iterator aIt;
86 Dictionary aDictionary;
87 for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
88 aContext = (*aIt).first;
89 aDictionary = (*aIt).second;
90 if (myTranslator.count(aContext) == 0) {
91 myTranslator[aContext] = aDictionary;
93 Dictionary::const_iterator aDictIt;
94 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
95 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
100 const Dictionary aCodecs = aReader.codecs();
101 Dictionary::const_iterator aDictIt;
102 for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) {
103 myCodecs[aDictIt->first] = aDictIt->second;
108 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
110 std::string aContext = theInfo.context();
111 std::string aMessage = theInfo.messageString();
112 std::list<std::string> aParameters = theInfo.parameters();
113 return translate(aContext, aMessage, aParameters);
116 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
118 std::string aResult = theString;
119 std::list<std::string>::const_iterator aIt;
123 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
125 sprintf(aBuf, "%d", i);
126 std::string aCode = std::string("%") + std::string(aBuf);
127 size_t aPos = aResult.find(aCode);
128 if (aPos != std::string::npos) {
129 std::string aFirst = aResult.substr(0, aPos);
130 std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
131 aResult = aFirst + aParam + aLast;
137 std::string Config_Translator::translate(const std::string& theContext,
138 const std::string& theMessage,
139 const std::list<std::string>& theParams)
141 if (myTranslator.count(theContext) > 0) {
142 if (myTranslator[theContext].count(theMessage) > 0) {
143 std::string aTranslation = myTranslator[theContext][theMessage];
144 if (theParams.size() > 0) {
145 aTranslation = insertParameters(aTranslation, theParams);
147 if (aTranslation.size() > 0)
151 std::string aMsg = theMessage;
152 if (theParams.size() > 0) {
153 aMsg = insertParameters(aMsg, theParams);
156 #ifdef MISSED_TRANSLATION
157 myMissed[theContext][theMessage] = "";
164 std::string Config_Translator::codec(const std::string& theContext)
166 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
169 std::string Config_Translator::codec(const Events_InfoMessage& theInfo)
171 return codec(theInfo.context());
175 #ifdef MISSED_TRANSLATION
176 void Config_Translator::saveMissedTranslations()
178 if (myMissed.size() == 0)
181 char* aPath = getenv("ROOT_DIR");
183 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
185 std::string aFile = aPath + std::string("/MissedTranslation.ts");
187 std::ofstream oFStream;
190 int aa = remove(aFile.c_str());
192 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
193 if (oFStream.is_open()) {
194 Translator::const_iterator aContIt;
195 Dictionary::const_iterator aDictIt;
196 std::string aContext;
200 oFStream << "<TS version=\"2.0\">" << std::endl;
201 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
202 oFStream << "<context>" << std::endl;
204 aContext = aContIt->first;
205 oFStream << " <name>" << aContext << "</name>" << std::endl;
207 aDict = aContIt->second;
208 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
209 oFStream << " <message>" << std::endl;
210 aSrc = aDictIt->first;
212 oFStream << " <source>" << aSrc << "</source>" << std::endl;
213 oFStream << " <translation>" << "</translation>" << std::endl;
215 oFStream << " </message>" << std::endl;
217 oFStream << "</context>" << std::endl;
220 oFStream << "</TS>" << std::endl;