X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2FConfig%2FConfig_Translator.cpp;h=643681be20917c3e2225c807f683f2429abce1a0;hb=798aa75abfe79ef25c9b4d3be4d48244ecb2fa7c;hp=38bbf8bdda45b657cfe0cd9f64e9f9056241d2fd;hpb=3aa818c0a5abf4c46afa2d91cad46340efc4bd5f;p=modules%2Fshaper.git diff --git a/src/Config/Config_Translator.cpp b/src/Config/Config_Translator.cpp index 38bbf8bdd..643681be2 100644 --- a/src/Config/Config_Translator.cpp +++ b/src/Config/Config_Translator.cpp @@ -8,18 +8,23 @@ #include "Config_XMLReader.h" #include "Config_Common.h" +#include +#include + class Config_TSReader : public Config_XMLReader { public: Config_TSReader(const std::string& theTSFile) : Config_XMLReader(theTSFile) {} const Config_Translator::Translator& translator() const { return myTranslator; } + const Config_Translator::Dictionary& codecs() const { return myCodecs; } protected: /// Overloaded method. Defines how to process each node virtual void processNode(xmlNodePtr theNode); private: Config_Translator::Translator myTranslator; + Config_Translator::Dictionary myCodecs; }; void Config_TSReader::processNode(xmlNodePtr theNode) @@ -32,6 +37,7 @@ void Config_TSReader::processNode(xmlNodePtr theNode) aName = ""; } else if (isNode(theNode, "name", NULL)) { aName = getContent(theNode); + myCodecs[aName] = encoding(); } else if (isNode(theNode, "message", NULL)) { aSource = ""; } else if (isNode(theNode, "source", NULL)) { @@ -47,6 +53,13 @@ void Config_TSReader::processNode(xmlNodePtr theNode) //****************************************************************************** //****************************************************************************** Config_Translator::Translator Config_Translator::myTranslator; +Config_Translator::Dictionary Config_Translator::myCodecs; + +#ifdef _DEBUG +#ifdef MISSED_TRANSLATION +Config_Translator::Translator Config_Translator::myMissed; +#endif +#endif bool Config_Translator::load(const std::string& theFileName) { @@ -70,36 +83,125 @@ bool Config_Translator::load(const std::string& theFileName) } } + const Dictionary aCodecs = aReader.codecs(); + Dictionary::const_iterator aDictIt; + for (aDictIt = aCodecs.cbegin(); aDictIt != aCodecs.cend(); aDictIt++) { + myCodecs[aDictIt->first] = aDictIt->second; + } return true; } -std::string Config_Translator::translate(const Events_InfoMessage& theInfo) +std::string Config_Translator::translate(std::shared_ptr theInfo) +{ + std::string aContext = theInfo->context(); + std::string aMessage = theInfo->messageString(); + std::list aParameters = theInfo->parameters(); + return translate(aContext, aMessage, aParameters); +} + + +std::string insertParameters(const std::string& theString, const std::list& theParams) +{ + std::string aResult = theString; + std::list::const_iterator aIt; + int i; + char aBuf[20]; + std::string aParam; + for (i=1, aIt = theParams.cbegin(); aIt != theParams.cend(); aIt++, i++) { + aParam = (*aIt); + sprintf(aBuf, "%d", i); + std::string aCode = std::string("%") + std::string(aBuf); + size_t aPos = aResult.find(aCode); + if (aPos != std::string::npos) { + std::string aFirst = aResult.substr(0, aPos); + std::string aLast = aResult.substr(aPos + aCode.length(), std::string::npos); + aResult = aFirst + aParam + aLast; + } + } + return aResult; +} + +std::string Config_Translator::translate(const std::string& theContext, + const std::string& theMessage, + const std::list& theParams) { - std::string aContext = theInfo.context(); - std::string aMessage = theInfo.message(); - std::list aParameters = theInfo.parameters(); - if (myTranslator.count(aContext) > 0) { - if (myTranslator[aContext].count(aMessage) > 0) { - std::string aTranslation = myTranslator[aContext][aMessage]; - if (aParameters.size() > 0) { - std::list::const_iterator aIt; - int i; - char aBuf[20]; - std::string aParam; - for (i=1, aIt = aParameters.cbegin(); aIt != aParameters.cend(); aIt++, i++) { - aParam = (*aIt); - sprintf(aBuf, "%d ", i); - std::string aCode = std::string("%") + std::string(aBuf); - size_t aPos = aTranslation.find(aCode); - if (aPos != std::string::npos) { - std::string aFirst = aTranslation.substr(0, aPos); - std::string aLast = aTranslation.substr(aPos + aCode.length(), std::string::npos); - aTranslation = aFirst + aParam + aLast; - } - } + if (myTranslator.count(theContext) > 0) { + if (myTranslator[theContext].count(theMessage) > 0) { + std::string aTranslation = myTranslator[theContext][theMessage]; + if (theParams.size() > 0) { + aTranslation = insertParameters(aTranslation, theParams); } return aTranslation; } } - return ""; + std::string aMsg = theMessage; + if (theParams.size() > 0) { + aMsg = insertParameters(aMsg, theParams); + } +#ifdef _DEBUG +#ifdef MISSED_TRANSLATION + myMissed[theContext][theMessage] = ""; +#endif +#endif + return aMsg; +} + + +std::string Config_Translator::codec(const std::string& theContext) +{ + return (myCodecs.count(theContext) > 0)? myCodecs[theContext] : "UTF-8"; +} + + +#ifdef _DEBUG +#ifdef MISSED_TRANSLATION +void Config_Translator::saveMissedTranslations() +{ + if (myMissed.size() == 0) + return; + + char* aPath = getenv("ROOT_DIR"); +#ifdef WIN32 + std::string aFile = aPath + std::string("\\MissedTranslation.ts"); +#else + std::string aFile = aPath + std::string("/MissedTranslation.ts"); +#endif + std::ofstream oFStream; + + // Delete old file + int aa = remove(aFile.c_str()); + + oFStream.open(aFile, std::ofstream::out | std::ofstream::app); + if (oFStream.is_open()) { + Translator::const_iterator aContIt; + Dictionary::const_iterator aDictIt; + std::string aContext; + std::string aSrc; + Dictionary aDict; + + oFStream << "" << std::endl; + for(aContIt = myMissed.cbegin(); aContIt != myMissed.cend(); aContIt++) { + oFStream << "" << std::endl; + + aContext = aContIt->first; + oFStream << " " << aContext << "" << std::endl; + + aDict = aContIt->second; + for(aDictIt = aDict.cbegin(); aDictIt != aDict.cend(); aDictIt++) { + oFStream << " " << std::endl; + aSrc = aDictIt->first; + + oFStream << " " << aSrc << "" << std::endl; + oFStream << " " << "" << std::endl; + + oFStream << " " << std::endl; + } + oFStream << "" << std::endl; + } + + oFStream << "" << std::endl; + oFStream.close(); + } } +#endif +#endif