1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Config_Translator.h"
22 #include "Config_XMLReader.h"
23 #include "Config_Common.h"
29 #pragma warning(disable : 4996) // for sprintf
33 * \class Config_TSReader
35 * \brief Class for reading translations from TS files (an XML format).
37 class Config_TSReader : public Config_XMLReader
41 /// \param theTSFile name of TS file
42 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
44 /// Returns content of TS file
45 const Config_Translator::Translator& translator() const { return myTranslator; }
47 /// Returns codecs defined in TS files
48 const Config_Translator::Dictionary& codecs() const { return myCodecs; }
51 /// Overloaded method. Defines how to process each node
52 virtual void processNode(xmlNodePtr theNode);
54 Config_Translator::Translator myTranslator;
55 Config_Translator::Dictionary myCodecs;
58 void Config_TSReader::processNode(xmlNodePtr theNode)
60 static std::string aName;
61 static std::string aSource;
62 std::string aTranslat;
64 if (isNode(theNode, "context", NULL)) {
66 } else if (isNode(theNode, "name", NULL)) {
67 aName = getContent(theNode);
68 myCodecs[aName] = encoding();
69 } else if (isNode(theNode, "message", NULL)) {
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;
80 //******************************************************************************
81 //******************************************************************************
82 //******************************************************************************
83 Config_Translator::Translator Config_Translator::myTranslator;
84 Config_Translator::Dictionary Config_Translator::myCodecs;
87 #ifdef MISSED_TRANSLATION
88 Config_Translator::Translator Config_Translator::myMissed;
92 bool Config_Translator::load(const std::string& theFileName)
94 Config_TSReader aReader(theFileName);
97 const Translator& aTranslator = aReader.translator();
98 Translator::const_iterator aIt;
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;
107 Dictionary::const_iterator aDictIt;
108 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
109 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
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;
122 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
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);
130 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
132 std::string aResult = theString;
133 std::list<std::string>::const_iterator aIt;
137 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
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;
151 std::string Config_Translator::translate(const std::string& theContext,
152 const std::string& theMessage,
153 const std::list<std::string>& theParams)
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);
161 if (aTranslation.size() > 0)
165 std::string aMsg = theMessage;
166 if (theParams.size() > 0) {
167 aMsg = insertParameters(aMsg, theParams);
170 #ifdef MISSED_TRANSLATION
171 myMissed[theContext][theMessage] = "";
178 std::string Config_Translator::codec(const std::string& theContext)
180 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
183 std::string Config_Translator::codec(const Events_InfoMessage& theInfo)
185 return codec(theInfo.context());
189 #ifdef MISSED_TRANSLATION
190 void Config_Translator::saveMissedTranslations()
192 if (myMissed.size() == 0)
195 char* aPath = getenv("ROOT_DIR");
197 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
199 std::string aFile = aPath + std::string("/MissedTranslation.ts");
201 std::ofstream oFStream;
204 int aa = remove(aFile.c_str());
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;
214 oFStream << "<TS version=\"2.0\">" << std::endl;
215 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
216 oFStream << "<context>" << std::endl;
218 aContext = aContIt->first;
219 oFStream << " <name>" << aContext << "</name>" << std::endl;
221 aDict = aContIt->second;
222 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
223 oFStream << " <message>" << std::endl;
224 aSrc = aDictIt->first;
226 oFStream << " <source>" << aSrc << "</source>" << std::endl;
227 oFStream << " <translation>" << "</translation>" << std::endl;
229 oFStream << " </message>" << std::endl;
231 oFStream << "</context>" << std::endl;
234 oFStream << "</TS>" << std::endl;