From f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 18 Mar 2012 23:05:10 +0300 Subject: Add help po translation. Combine help and client data translations. --- src/gui/didyouknowwindow.cpp | 20 ++------ src/gui/helpwindow.cpp | 22 +++------ src/utils/translation/podict.cpp | 2 +- src/utils/translation/podict.h | 2 +- src/utils/translation/poparser.cpp | 32 ++++++++++--- src/utils/translation/poparser.h | 10 +++- src/utils/translation/translationmanager.cpp | 68 ++++++++++++++++++++++++++-- src/utils/translation/translationmanager.h | 10 +++- 8 files changed, 119 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/gui/didyouknowwindow.cpp b/src/gui/didyouknowwindow.cpp index a6b89923e..a071e1ffc 100644 --- a/src/gui/didyouknowwindow.cpp +++ b/src/gui/didyouknowwindow.cpp @@ -39,6 +39,9 @@ #include "utils/gettext.h" #include "utils/langs.h" +#include "utils/translation/podict.h" +#include "utils/translation/translationmanager.h" + #include "debug.h" static const int minTip = 1; @@ -129,26 +132,13 @@ void DidYouKnowWindow::loadFile(int num) { const std::string file = strprintf("tips/%d", num); const std::vector langs = getLang(); - ResourceManager *resman = ResourceManager::getInstance(); std::string helpPath = branding.getStringValue("helpPath"); if (helpPath.empty()) helpPath = paths.getStringValue("help"); std::vector lines; - if (!langs.empty()) - { - std::string name = helpPath + langs[0] + "/" + file + ".txt"; - if (resman->exists(name)) - resman->loadTextFile(name, lines); - if (lines.empty() && langs.size() > 1) - { - name = helpPath + langs[1] + "/" + file + ".txt"; - resman->loadTextFile(name, lines); - } - } - - if (lines.empty()) - resman->loadTextFile(helpPath + file + ".txt", lines); + TranslationManager::translateFile(helpPath + file + ".txt", + translator, lines); for (unsigned int i = 0; i < lines.size(); ++i) mBrowserBox->addRow(lines[i]); diff --git a/src/gui/helpwindow.cpp b/src/gui/helpwindow.cpp index 5876667da..0a28855b9 100644 --- a/src/gui/helpwindow.cpp +++ b/src/gui/helpwindow.cpp @@ -22,6 +22,7 @@ #include "gui/helpwindow.h" +#include "configuration.h" #include "logger.h" #include "gui/gui.h" @@ -33,11 +34,13 @@ #include "gui/widgets/scrollarea.h" #include "resources/resourcemanager.h" -#include "configuration.h" #include "utils/gettext.h" #include "utils/langs.h" +#include "utils/translation/podict.h" +#include "utils/translation/translationmanager.h" + #include "debug.h" HelpWindow::HelpWindow(): @@ -105,26 +108,13 @@ void HelpWindow::loadHelp(const std::string &helpFile) void HelpWindow::loadFile(const std::string &file) { const std::vector langs = getLang(); - ResourceManager *resman = ResourceManager::getInstance(); std::string helpPath = branding.getStringValue("helpPath"); if (helpPath.empty()) helpPath = paths.getStringValue("help"); std::vector lines; - if (!langs.empty()) - { - std::string name = helpPath + langs[0] + "/" + file + ".txt"; - if (resman->exists(name)) - resman->loadTextFile(name, lines); - if (lines.empty() && langs.size() > 1) - { - name = helpPath + langs[1] + "/" + file + ".txt"; - resman->loadTextFile(name, lines); - } - } - - if (lines.empty()) - resman->loadTextFile(helpPath + file + ".txt", lines); + TranslationManager::translateFile(helpPath + file + ".txt", + translator, lines); for (unsigned int i = 0; i < lines.size(); ++i) mBrowserBox->addRow(lines[i]); diff --git a/src/utils/translation/podict.cpp b/src/utils/translation/podict.cpp index 0be0b279f..5b1e2a2f2 100644 --- a/src/utils/translation/podict.cpp +++ b/src/utils/translation/podict.cpp @@ -40,7 +40,7 @@ PoDict::~PoDict() { } -const std::string PoDict::getStr(std::string &str) +const std::string PoDict::getStr(const std::string &str) { if (mPoLines.find(str) == mPoLines.end()) return str; diff --git a/src/utils/translation/podict.h b/src/utils/translation/podict.h index 0b7dd726c..4c41aadcc 100644 --- a/src/utils/translation/podict.h +++ b/src/utils/translation/podict.h @@ -33,7 +33,7 @@ class PoDict ~PoDict(); - const std::string getStr(std::string &str); + const std::string getStr(const std::string &str); const char *getChar(const char *str); diff --git a/src/utils/translation/poparser.cpp b/src/utils/translation/poparser.cpp index 521774592..d0f79a28c 100644 --- a/src/utils/translation/poparser.cpp +++ b/src/utils/translation/poparser.cpp @@ -24,7 +24,6 @@ #include "utils/stringutils.h" -#include "localconsts.h" #include "logger.h" #include @@ -39,22 +38,30 @@ PoParser::PoParser() : { } -void PoParser::openFile() +void PoParser::openFile(std::string name) { ResourceManager *resman = ResourceManager::getInstance(); int size; - char *buf = static_cast(resman->loadFile(getFileName(mLang), size)); + char *buf = static_cast(resman->loadFile(getFileName(name), size)); mFile.str(std::string(buf, size)); free(buf); } -PoDict *PoParser::load(std::string lang) +PoDict *PoParser::load(std::string lang, std::string fileName, PoDict *dict) { + logger->log("loading lang: %s, file: %s", lang.c_str(), fileName.c_str()); + setLang(lang); - mDict = getDict(); + if (!dict) + mDict = getDict(); + else + mDict = dict; - openFile(); + if (fileName.empty()) + openFile(mLang); + else + openFile(fileName); mMsgId = ""; mMsgStr = ""; @@ -84,6 +91,8 @@ PoDict *PoParser::load(std::string lang) // logger->log("add key: " + mMsgId); // logger->log("add value: " + mMsgStr); + convertStr(mMsgId); + convertStr(mMsgStr); // store key and value mDict->set(mMsgId, mMsgStr); } @@ -212,6 +221,7 @@ bool PoParser::checkLang(std::string lang) const std::string PoParser::getFileName(std::string lang) const { // get po file name from lang name + logger->log("getFileName: translations/%s.po", lang.c_str()); return strprintf("translations/%s.po", lang.c_str()); } @@ -219,3 +229,13 @@ PoDict *PoParser::getDict() { return new PoDict(mLang); } + +void PoParser::convertStr(std::string &str) +{ + if (str.empty()) + return; + + replaceAll(str, "\\n", "\n"); + replaceAll(str, "\\\"", "\""); + replaceAll(str, "\\\\", "\\"); +} diff --git a/src/utils/translation/poparser.h b/src/utils/translation/poparser.h index 799cd2fe7..35a9cd772 100644 --- a/src/utils/translation/poparser.h +++ b/src/utils/translation/poparser.h @@ -23,6 +23,8 @@ #include "utils/translation/podict.h" +#include "localconsts.h" + #include #include @@ -31,7 +33,9 @@ class PoParser public: PoParser(); - PoDict *load(std::string fileName); + PoDict *load(std::string lang, + std::string fileName = "", + PoDict *dict = nullptr); bool checkLang(std::string lang) const; @@ -41,7 +45,7 @@ class PoParser void setLang(std::string lang) { mLang = lang; } - void openFile(); + void openFile(std::string name); bool readLine(); @@ -55,6 +59,8 @@ class PoParser PoDict *getDict(); + void convertStr(std::string &str); + // current lang std::string mLang; diff --git a/src/utils/translation/translationmanager.cpp b/src/utils/translation/translationmanager.cpp index 1b68cc91d..03dd6d100 100644 --- a/src/utils/translation/translationmanager.cpp +++ b/src/utils/translation/translationmanager.cpp @@ -21,13 +21,16 @@ #include "utils/translation/translationmanager.h" #include "utils/langs.h" +#include "utils/stringutils.h" #include "utils/translation/podict.h" #include "utils/translation/poparser.h" +#include "resources/resourcemanager.h" + +#include #include -#include "localconsts.h" #include "logger.h" #include "debug.h" @@ -43,7 +46,8 @@ void TranslationManager::loadCurrentLang() { if (translator) delete translator; - translator = loadLang(getLang()); + translator = loadLang(getLang(), ""); + translator = loadLang(getLang(), "help/", translator); } void TranslationManager::close() @@ -52,7 +56,9 @@ void TranslationManager::close() translator = nullptr; } -PoDict *TranslationManager::loadLang(LangVect lang) +PoDict *TranslationManager::loadLang(LangVect lang, + std::string subName, + PoDict *dict) { std::string name = ""; PoParser parser; @@ -65,14 +71,66 @@ PoDict *TranslationManager::loadLang(LangVect lang) if (*it == "C") continue; - if (parser.checkLang(*it)) + logger->log("check file: " + subName + *it); + if (parser.checkLang(subName + *it)) { name = *it; break; } } if (!name.empty()) - return parser.load(name); + return parser.load(name, subName + name, dict); logger->log("can't find client data translation"); + if (dict) + return dict; return PoParser::getEmptyDict(); } + +bool TranslationManager::translateFile(const std::string &fileName, + PoDict *dict, + std::vector &lines) +{ + if (!dict || fileName.empty()) + return false; + + int contentsLength; + ResourceManager *resman = ResourceManager::getInstance(); + char *fileContents = static_cast( + resman->loadFile(fileName, contentsLength)); + + if (!fileContents) + { + logger->log("Couldn't load file: %s", fileName.c_str()); + return false; + } + std::string str = std::string(fileContents, contentsLength); + + std::string::size_type oldPos1 = 0; + std::string::size_type pos1; + while ((pos1 = str.find("<<")) != std::string::npos) + { + if (pos1 == oldPos1) + break; // detected infinite loop + std::string::size_type pos2 = str.find(">>", pos1 + 2); + if (pos2 == std::string::npos) + break; + const std::string key = str.substr(pos1 + 2, pos2 - pos1 - 2); + const std::string key2 = "<<" + str.substr( + pos1 + 2, pos2 - pos1 - 2) + ">>"; + const std::string val = dict->getStr(key); + +// logger->log("key:" + key); + replaceAll(str, key2, val); + + oldPos1 = pos1; + } + + std::istringstream iss(str); + std::string line; + + while (getline(iss, line)) + lines.push_back(line); + + free(fileContents); + return true; +} diff --git a/src/utils/translation/translationmanager.h b/src/utils/translation/translationmanager.h index 39702f4f4..842c5620b 100644 --- a/src/utils/translation/translationmanager.h +++ b/src/utils/translation/translationmanager.h @@ -21,6 +21,8 @@ #ifndef UTILS_TRANSLATION_MANAGER_H #define UTILS_TRANSLATION_MANAGER_H +#include "localconsts.h" + #include #include @@ -29,13 +31,19 @@ class PoDict; class TranslationManager { public: - static PoDict *loadLang(std::vector lang); + static PoDict *loadLang(std::vector lang, + std::string subName, + PoDict *dict = nullptr); static void init(); static void close(); static void loadCurrentLang(); + + static bool translateFile(const std::string &fileName, + PoDict *dict, + std::vector &lines); }; #endif // UTILS_TRANSLATION_MANAGER_H -- cgit v1.2.3-70-g09d2