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
34 * \class Config_TSReader
36 * \brief Class for reading translations from TS files (an XML format).
38 class Config_TSReader : public Config_XMLReader
42 /// \param theTSFile name of TS file
43 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
45 /// Returns content of TS file
46 const Config_Translator::Translator& translator() const { return myTranslator; }
48 /// Returns codecs defined in TS files
49 const Config_Translator::Dictionary& codecs() const { return myCodecs; }
52 /// Overloaded method. Defines how to process each node
53 virtual void processNode(xmlNodePtr theNode);
55 Config_Translator::Translator myTranslator;
56 Config_Translator::Dictionary myCodecs;
59 void Config_TSReader::processNode(xmlNodePtr theNode)
61 static std::string aName;
62 static std::string aSource;
63 std::string aTranslat;
65 if (isNode(theNode, "context", NULL)) {
67 } else if (isNode(theNode, "name", NULL)) {
68 aName = getContent(theNode);
69 myCodecs[aName] = encoding();
70 } else if (isNode(theNode, "message", NULL)) {
72 } else if (isNode(theNode, "source", NULL)) {
73 aSource = getContent(theNode);
74 } else if (isNode(theNode, "translation", NULL)) {
75 aTranslat = getContent(theNode);
76 if ((aName.size() > 0) && (aSource.size() > 0))
77 myTranslator[aName][aSource] = aTranslat;
82 //******************************************************************************
83 //******************************************************************************
84 //******************************************************************************
85 Config_Translator::Translator Config_Translator::myTranslator;
86 Config_Translator::Dictionary Config_Translator::myCodecs;
89 #ifdef MISSED_TRANSLATION
90 Config_Translator::Translator Config_Translator::myMissed;
94 bool Config_Translator::load(const std::string& theFileName)
96 Config_TSReader aReader(theFileName);
99 const Translator& aTranslator = aReader.translator();
100 Translator::const_iterator aIt;
101 std::string aContext;
102 Dictionary aDictionary;
103 for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
104 aContext = (*aIt).first;
105 aDictionary = (*aIt).second;
106 if (myTranslator.count(aContext) == 0) {
107 myTranslator[aContext] = aDictionary;
109 Dictionary::const_iterator aDictIt;
110 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
111 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
116 const Dictionary aCodecs = aReader.codecs();
117 Dictionary::const_iterator aDictIt;
118 for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) {
119 myCodecs[aDictIt->first] = aDictIt->second;
124 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
126 std::string aContext = theInfo.context();
127 std::string aMessage = theInfo.messageString();
128 std::list<std::string> aParameters = theInfo.parameters();
129 return translate(aContext, aMessage, aParameters);
132 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
134 std::string aResult = theString;
135 std::list<std::string>::const_iterator aIt;
139 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
141 sprintf(aBuf, "%d", i);
142 std::string aCode = std::string("%") + std::string(aBuf);
143 size_t aPos = aResult.find(aCode);
144 if (aPos != std::string::npos) {
145 std::string aFirst = aResult.substr(0, aPos);
146 std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
147 aResult = aFirst + aParam + aLast;
153 std::string Config_Translator::translate(const std::string& theContext,
154 const std::string& theMessage,
155 const std::list<std::string>& theParams)
157 if (myTranslator.count(theContext) > 0) {
158 if (myTranslator[theContext].count(theMessage) > 0) {
159 std::string aTranslation = myTranslator[theContext][theMessage];
160 if (theParams.size() > 0) {
161 aTranslation = insertParameters(aTranslation, theParams);
163 if (aTranslation.size() > 0)
167 std::string aMsg = theMessage;
168 if (theParams.size() > 0) {
169 aMsg = insertParameters(aMsg, theParams);
172 #ifdef MISSED_TRANSLATION
173 myMissed[theContext][theMessage] = "";
180 std::string Config_Translator::codec(const std::string& theContext)
182 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
185 std::string Config_Translator::codec(const Events_InfoMessage& theInfo)
187 return codec(theInfo.context());
191 #ifdef MISSED_TRANSLATION
192 void Config_Translator::saveMissedTranslations()
194 if (myMissed.size() == 0)
197 char* aPath = getenv("ROOT_DIR");
199 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
201 std::string aFile = aPath + std::string("/MissedTranslation.ts");
203 std::ofstream oFStream;
206 remove(aFile.c_str());
208 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
209 if (oFStream.is_open()) {
210 Translator::const_iterator aContIt;
211 Dictionary::const_iterator aDictIt;
212 std::string aContext;
216 oFStream << "<TS version=\"2.0\">" << std::endl;
217 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
218 oFStream << "<context>" << std::endl;
220 aContext = aContIt->first;
221 oFStream << " <name>" << aContext << "</name>" << std::endl;
223 aDict = aContIt->second;
224 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
225 oFStream << " <message>" << std::endl;
226 aSrc = aDictIt->first;
228 oFStream << " <source>" << aSrc << "</source>" << std::endl;
229 oFStream << " <translation>" << "</translation>" << std::endl;
231 oFStream << " </message>" << std::endl;
233 oFStream << "</context>" << std::endl;
236 oFStream << "</TS>" << std::endl;