Salome HOME
Send information message for translation
[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 class Config_TSReader : public Config_XMLReader
12 {
13 public:
14   Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
15
16   const Config_Translator::Translator& translator() const { return myTranslator; }
17
18 protected:
19   /// Overloaded method. Defines how to process each node
20   virtual void processNode(xmlNodePtr theNode);
21 private:
22   Config_Translator::Translator myTranslator;
23 };
24
25 void Config_TSReader::processNode(xmlNodePtr theNode)
26 {
27   static std::string aName;
28   static std::string aSource;
29   std::string aTranslat;
30
31   if (isNode(theNode, "context", NULL)) {
32     aName = "";
33   } else if (isNode(theNode, "name", NULL)) {
34     aName = getContent(theNode);
35   } else if (isNode(theNode, "message", NULL)) {
36     aSource = "";
37   } else if (isNode(theNode, "source", NULL)) {
38     aSource = getContent(theNode);
39   } else if (isNode(theNode, "translation", NULL)) {
40     aTranslat = getContent(theNode);
41     if ((aName.size() > 0) && (aSource.size() > 0))
42       myTranslator[aName][aSource] = aTranslat;
43   }
44 }
45
46 //******************************************************************************
47 //******************************************************************************
48 //******************************************************************************
49 Config_Translator::Translator Config_Translator::myTranslator;
50
51 bool Config_Translator::load(const std::string& theFileName)
52 {
53   Config_TSReader aReader(theFileName);
54   aReader.readAll();
55
56   const Translator& aTranslator = aReader.translator();
57   Translator::const_iterator aIt;
58   std::string aContext;
59   Dictionary aDictionary;
60   for (aIt = aTranslator.cbegin(); aIt != aTranslator.cend(); aIt++) {
61     aContext = (*aIt).first;
62     aDictionary = (*aIt).second;
63     if (myTranslator.count(aContext) == 0) {
64       myTranslator[aContext] = aDictionary;
65     } else {
66       Dictionary::const_iterator aDictIt;
67       for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
68         myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
69       }
70     }
71   }
72
73   return true;
74 }
75
76 std::string Config_Translator::translate(std::shared_ptr<Events_InfoMessage> theInfo)
77 {
78   std::string aContext = theInfo->context();
79   std::string aMessage = theInfo->message();
80   std::list<std::string> aParameters = theInfo->parameters();
81   return translate(aContext, aMessage, aParameters);
82 }
83
84
85 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
86 {
87   std::string aResult = theString;
88   std::list<std::string>::const_iterator aIt;
89   int i;
90   char aBuf[20];
91   std::string aParam;
92   for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
93     aParam = (*aIt);
94     sprintf_s(aBuf, "%d", i);
95     std::string aCode = std::string("%") + std::string(aBuf);
96     size_t aPos = aResult.find(aCode);
97     if (aPos != std::string::npos) {
98       std::string aFirst = aResult.substr(0, aPos);
99       std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos);
100       aResult = aFirst + aParam + aLast;
101     }
102   }
103   return aResult;
104 }
105
106 std::string Config_Translator::translate(const std::string& theContext,
107                                          const std::string& theMessage, 
108                                          const std::list<std::string>& theParams)
109 {
110   if (myTranslator.count(theContext) > 0) {
111     if (myTranslator[theContext].count(theMessage) > 0) {
112       std::string aTranslation = myTranslator[theContext][theMessage];
113       if (theParams.size() > 0) {
114         aTranslation = insertParameters(aTranslation, theParams);
115       }
116       return aTranslation;
117     }
118   }
119   std::string aMsg = theMessage;
120   if (theParams.size() > 0) {
121     aMsg = insertParameters(aMsg, theParams);
122   }
123   return aMsg;
124 }