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 return translate(aContext, aMessage);
115 std::string Config_Translator::translate(const std::string& theContext,
116 const std::string& theMessage)
118 if (myTranslator.count(theContext) > 0) {
119 if (myTranslator[theContext].count(theMessage) > 0) {
120 std::string aTranslation = myTranslator[theContext][theMessage];
121 if (aTranslation.size() > 0)
126 #ifdef MISSED_TRANSLATION
127 myMissed[theContext][theMessage] = "";
134 std::string Config_Translator::codec(const std::string& theContext)
136 return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8";
141 #ifdef MISSED_TRANSLATION
142 void Config_Translator::saveMissedTranslations()
144 if (myMissed.size() == 0)
147 char* aPath = getenv("ROOT_DIR");
149 std::string aFile = aPath + std::string("\\MissedTranslation.ts");
151 std::string aFile = aPath + std::string("/MissedTranslation.ts");
153 std::ofstream oFStream;
156 int aa = remove(aFile.c_str());
158 oFStream.open(aFile, std::ofstream::out | std::ofstream::app);
159 if (oFStream.is_open()) {
160 Translator::const_iterator aContIt;
161 Dictionary::const_iterator aDictIt;
162 std::string aContext;
166 oFStream << "<TS version=\"2.0\">" << std::endl;
167 for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) {
168 oFStream << "<context>" << std::endl;
170 aContext = aContIt->first;
171 oFStream << " <name>" << aContext << "</name>" << std::endl;
173 aDict = aContIt->second;
174 for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) {
175 oFStream << " <message>" << std::endl;
176 aSrc = aDictIt->first;
178 oFStream << " <source>" << aSrc << "</source>" << std::endl;
179 oFStream << " <translation>" << "</translation>" << std::endl;
181 oFStream << " </message>" << std::endl;
183 oFStream << "</context>" << std::endl;
186 oFStream << "</TS>" << std::endl;