]> SALOME platform Git repositories - modules/shaper.git/blob - src/Config/Config_Translator.cpp
Salome HOME
38bbf8bdda45b657cfe0cd9f64e9f9056241d2fd
[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(const 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   if (myTranslator.count(aContext) > 0) {
82     if (myTranslator[aContext].count(aMessage) > 0) {
83       std::string aTranslation = myTranslator[aContext][aMessage];
84       if (aParameters.size() > 0) {
85         std::list<std::string>::const_iterator aIt;
86         int i;
87         char aBuf[20];
88         std::string aParam;
89         for (i=1, aIt = aParameters.cbegin(); aIt != aParameters.cend(); aIt++, i++) {
90           aParam = (*aIt);
91           sprintf(aBuf, "%d ", i);
92           std::string aCode = std::string("%") + std::string(aBuf);
93           size_t aPos = aTranslation.find(aCode);
94           if (aPos != std::string::npos) {
95             std::string aFirst = aTranslation.substr(0, aPos);
96             std::string aLast = aTranslation.substr(aPos + aCode.length(), std::string::npos);
97             aTranslation = aFirst + aParam + aLast;
98           }
99         }
100       }
101       return aTranslation;
102     }
103   }
104   return "";
105 }