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"
11 class Config_TSReader : public Config_XMLReader
14 Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {}
16 const Config_Translator::Translator& translator() const { return myTranslator; }
19 /// Overloaded method. Defines how to process each node
20 virtual void processNode(xmlNodePtr theNode);
22 Config_Translator::Translator myTranslator;
25 void Config_TSReader::processNode(xmlNodePtr theNode)
27 static std::string aName;
28 static std::string aSource;
29 std::string aTranslat;
31 if (isNode(theNode, "context", NULL)) {
33 } else if (isNode(theNode, "name", NULL)) {
34 aName = getContent(theNode);
35 } else if (isNode(theNode, "message", NULL)) {
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;
46 //******************************************************************************
47 //******************************************************************************
48 //******************************************************************************
49 Config_Translator::Translator Config_Translator::myTranslator;
51 bool Config_Translator::load(const std::string& theFileName)
53 Config_TSReader aReader(theFileName);
56 const Translator& aTranslator = aReader.translator();
57 Translator::const_iterator aIt;
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;
66 Dictionary::const_iterator aDictIt;
67 for (aDictIt = aDictionary.cbegin(); aDictIt != aDictionary.cend(); aDictIt++) {
68 myTranslator[aContext][(*aDictIt).first] = (*aDictIt).second;
76 std::string Config_Translator::translate(const Events_InfoMessage& theInfo)
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;
89 for (i=1, aIt = aParameters.cbegin(); aIt != aParameters.cend(); aIt++, i++) {
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;