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(std::shared_ptr<Events_InfoMessage> theInfo)
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);
85 std::string insertParameters(const std::string& theString, const std::list<std::string>& theParams)
87 std::string aResult = theString;
88 std::list<std::string>::const_iterator aIt;
92 for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) {
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;
106 std::string Config_Translator::translate(const std::string& theContext,
107 const std::string& theMessage,
108 const std::list<std::string>& theParams)
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);
119 std::string aMsg = theMessage;
120 if (theParams.size() > 0) {
121 aMsg = insertParameters(aMsg, theParams);