Salome HOME
Adjust unit tests for Boolean Fill
[modules/shaper.git] / src / Config / Config_Translator.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "Config_Translator.h"
22 #include "Config_XMLReader.h"
23 #include "Config_Common.h"
24
25 #include <fstream>
26 #include <ostream>
27
28 #ifdef WIN32
29 #pragma warning(disable : 4996) // for sprintf
30 #endif
31
32 /**
33  * \class Config_TSReader
34  * \ingroup Config
35  * \brief Class for reading translations from TS files (an XML format).
36  */
37 class Config_TSReader : public Config_XMLReader
38 {
39 public:
40   /// Constructor
41   /// \param theTSFile name of TS file
42   Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
43
44   /// Returns content of TS file
45   const Config_Translator::Translator& translator() const { return myTranslator; }
46
47   /// Returns codecs defined in TS files
48   const Config_Translator::Dictionary& codecs() const { return myCodecs; }
49
50 protected:
51   /// Overloaded method. Defines how to process each node
52   virtual void processNode(xmlNodePtr theNode);
53 private:
54   Config_Translator::Translator myTranslator;
55   Config_Translator::Dictionary myCodecs;
56 };
57
58 void Config_TSReader::processNode(xmlNodePtr theNode)
59 {
60   static std::string aName;
61   static std::string aSource;
62   std::string aTranslat;
63
64   if (isNode(theNode, "context", NULL)) {
65     aName = "";
66   } else if (isNode(theNode, "name", NULL)) {
67     aName = getContent(theNode);
68     myCodecs[aName] = encoding();
69   } else if (isNode(theNode, "message", NULL)) {
70     aSource = "";
71   } else if (isNode(theNode, "source", NULL)) {
72     aSource = getContent(theNode);
73   } else if (isNode(theNode, "translation", NULL)) {
74     aTranslat = getContent(theNode);
75     if ((aName.size() > 0) && (aSource.size() > 0))
76       myTranslator[aName][aSource] = aTranslat;
77   }
78 }
79
80 //******************************************************************************
81 //******************************************************************************
82 //******************************************************************************
83 Config_Translator::Translator Config_Translator::myTranslator;
84 Config_Translator::Dictionary Config_Translator::myCodecs;
85
86 #ifdef _DEBUG
87 #ifdef MISSED_TRANSLATION
88 Config_Translator::Translator Config_Translator::myMissed;
89 #endif
90 #endif
91
92 bool Config_Translator::load(const std::string& theFileName)
93 {
94   Config_TSReader aReader(theFileName);
95   aReader.readAll();
96
97   const Translator& aTranslator = aReader.translator();
98   Translator::const_iterator aIt;
99   std::string aContext;
100   Dictionary aDictionary;
101   for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
102     aContext = (*aIt).first;
103     aDictionary = (*aIt).second;
104     if (myTranslator.count(aContext) == 0) {
105       myTranslator[aContext] = aDictionary;
106     } else {
107       Dictionary::const_iterator aDictIt;
108       for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
109         myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
110       }
111     }
112   }
113
114   const Dictionary aCodecs = aReader.codecs();
115   Dictionary::const_iterator aDictIt;
116   for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) {
117     myCodecs[aDictIt->first] = aDictIt->second;
118   }
119   return true;
120 }
121
122 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
123 {
124   std::string aContext = theInfo.context();
125   std::string aMessage = theInfo.messageString();
126   std::list<std::string> aParameters = theInfo.parameters();
127   return translate(aContext, aMessage, aParameters);
128 }
129
130 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
131 {
132   std::string aResult = theString;
133   std::list<std::string>::const_iterator aIt;
134   int i;
135   char aBuf[20];
136   std::string aParam;
137   for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
138     aParam = (*aIt);
139     sprintf(aBuf, "%d", i);
140     std::string aCode = std::string("%") + std::string(aBuf);
141     size_t aPos = aResult.find(aCode);
142     if (aPos != std::string::npos) {
143       std::string aFirst = aResult.substr(0, aPos);
144       std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
145       aResult = aFirst + aParam + aLast;
146     }
147   }
148   return aResult;
149 }
150
151 std::string Config_Translator::translate(const std::string& theContext,
152                                          const std::string& theMessage,
153                                          const std::list<std::string>& theParams)
154 {
155   if (myTranslator.count(theContext) > 0) {
156     if (myTranslator[theContext].count(theMessage) > 0) {
157       std::string aTranslation = myTranslator[theContext][theMessage];
158       if (theParams.size() > 0) {
159         aTranslation = insertParameters(aTranslation, theParams);
160       }
161       if (aTranslation.size() > 0)
162         return aTranslation;
163     }
164   }
165   std::string aMsg = theMessage;
166   if (theParams.size() > 0) {
167     aMsg = insertParameters(aMsg, theParams);
168   }
169 #ifdef _DEBUG
170 #ifdef MISSED_TRANSLATION
171   myMissed[theContext][theMessage] = "";
172 #endif
173 #endif
174   return aMsg;
175 }
176
177
178 std::string Config_Translator::codec(const std::string& theContext)
179 {
180   return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
181 }
182
183 std::string Config_Translator::codec(const Events_InfoMessage& theInfo)
184 {
185   return codec(theInfo.context());
186 }
187
188 #ifdef _DEBUG
189 #ifdef MISSED_TRANSLATION
190 void Config_Translator::saveMissedTranslations()
191 {
192   if (myMissed.size() == 0)
193     return;
194
195   char* aPath = getenv("ROOT_DIR");
196 #ifdef WIN32
197   std::string aFile = aPath + std::string("\\MissedTranslation.ts");
198 #else
199   std::string aFile = aPath + std::string("/MissedTranslation.ts");
200 #endif
201   std::ofstream oFStream;
202
203   // Delete old file
204   int aa = remove(aFile.c_str());
205
206   oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
207   if (oFStream.is_open()) {
208     Translator::const_iterator aContIt;
209     Dictionary::const_iterator aDictIt;
210     std::string aContext;
211     std::string aSrc;
212     Dictionary aDict;
213
214     oFStream << "<TS version=\"2.0\">" << std::endl;
215     for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
216       oFStream << "<context>" << std::endl;
217
218       aContext = aContIt->first;
219       oFStream << "  <name>" << aContext << "</name>" << std::endl;
220
221       aDict = aContIt->second;
222       for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
223         oFStream << "  <message>" << std::endl;
224         aSrc = aDictIt->first;
225
226         oFStream << "    <source>" << aSrc << "</source>" << std::endl;
227         oFStream << "    <translation>" << "</translation>" << std::endl;
228
229         oFStream << "  </message>" << std::endl;
230       }
231       oFStream << "</context>" << std::endl;
232     }
233
234     oFStream << "</TS>" << std::endl;
235     oFStream.close();
236   }
237 }
238 #endif
239 #endif