Salome HOME
Replace of Events_Error by Events_InfoMessage. Provide storing of non translated...
[modules/shaper.git] / src / Config / Config_Translator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Config_Translator.cpp
4 // Created:     31 May 2016
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "Config_Translator.h"
8 #include "Config_XMLReader.h"
9 #include "Config_Common.h"
10
11 #include <fstream>
12 #include <ostream>
13
14 class Config_TSReader : public Config_XMLReader
15 {
16 public:
17   Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
18
19   const Config_Translator::Translator& translator() const { return myTranslator; }
20
21 protected:
22   /// Overloaded method. Defines how to process each node
23   virtual void processNode(xmlNodePtr theNode);
24 private:
25   Config_Translator::Translator myTranslator;
26 };
27
28 void Config_TSReader::processNode(xmlNodePtr theNode)
29 {
30   static std::string aName;
31   static std::string aSource;
32   std::string aTranslat;
33
34   if (isNode(theNode, "context", NULL)) {
35     aName = "";
36   } else if (isNode(theNode, "name", NULL)) {
37     aName = getContent(theNode);
38   } else if (isNode(theNode, "message", NULL)) {
39     aSource = "";
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;
46   }
47 }
48
49 //******************************************************************************
50 //******************************************************************************
51 //******************************************************************************
52 Config_Translator::Translator Config_Translator::myTranslator;
53 #ifdef _DEBUG
54 #ifdef MISSED_TRANSLATION
55 Config_Translator::Translator Config_Translator::myMissed;
56 #endif
57 #endif
58
59 bool Config_Translator::load(const std::string& theFileName)
60 {
61   Config_TSReader aReader(theFileName);
62   aReader.readAll();
63
64   const Translator& aTranslator = aReader.translator();
65   Translator::const_iterator aIt;
66   std::string aContext;
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;
73     } else {
74       Dictionary::const_iterator aDictIt;
75       for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
76         myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
77       }
78     }
79   }
80
81   return true;
82 }
83
84 std::string Config_Translator::translate(std::shared_ptr<Events_InfoMessage> theInfo)
85 {
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);
90 }
91
92
93 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
94 {
95   std::string aResult = theString;
96   std::list<std::string>::const_iterator aIt;
97   int i;
98   char aBuf[20];
99   std::string aParam;
100   for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
101     aParam = (*aIt);
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;
109     }
110   }
111   return aResult;
112 }
113
114 std::string Config_Translator::translate(const std::string& theContext,
115                                          const std::string& theMessage, 
116                                          const std::list<std::string>& theParams)
117 {
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);
123       }
124       return aTranslation;
125     }
126   }
127   std::string aMsg = theMessage;
128   if (theParams.size() > 0) {
129     aMsg = insertParameters(aMsg, theParams);
130   }
131 #ifdef _DEBUG
132 #ifdef MISSED_TRANSLATION
133   myMissed[theContext][theMessage] = "";
134 #endif
135 #endif
136   return aMsg;
137 }
138
139 #ifdef _DEBUG
140 #ifdef MISSED_TRANSLATION
141 void Config_Translator::saveMissedTranslations()
142 {
143   if (myMissed.size() == 0)
144     return;
145
146   char* aPath = getenv("ROOT_DIR");
147 #ifdef WIN32
148   std::string aFile = aPath + std::string("\\MissedTranslation.ts");
149 #else
150   std::string aFile = aPath + std::string("/MissedTranslation.ts");
151 #endif
152   std::ofstream oFStream;
153
154   // Delete old file
155   int aa = remove(aFile.c_str());
156
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;
162     std::string aSrc;
163     Dictionary aDict;
164
165     oFStream << "<TS version=\"2.0\">" << std::endl;
166     for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
167       oFStream << "<context>" << std::endl;
168
169       aContext = aContIt->first;
170       oFStream << "  <name>" << aContext << "</name>" << std::endl;
171
172       aDict = aContIt->second;
173       for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
174         oFStream << "  <message>" << std::endl;
175         aSrc = aDictIt->first;
176
177         oFStream << "    <source>" << aSrc << "</source>" << std::endl;
178         oFStream << "    <translation>" << "</translation>" << std::endl;
179
180         oFStream << "  </message>" << std::endl;
181       }
182       oFStream << "</context>" << std::endl;
183     }
184
185     oFStream << "</TS>" << std::endl;
186     oFStream.close();
187   }
188 }
189 #endif
190 #endif