diff options
author | Andrei Karas <akaras@inbox.ru> | 2012-03-18 23:05:10 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2012-03-19 00:03:33 +0300 |
commit | f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f (patch) | |
tree | b325f2fa7063581f3110184aa449d08c9fafbb02 /src/utils/translation | |
parent | ac9594ec37a52c436b87a2d431e7df126592e4a5 (diff) | |
download | manaverse-f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f.tar.gz manaverse-f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f.tar.bz2 manaverse-f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f.tar.xz manaverse-f99ee017f2ac6b03a6bc6122cb78923a9b4f6a5f.zip |
Add help po translation.
Combine help and client data translations.
Diffstat (limited to 'src/utils/translation')
-rw-r--r-- | src/utils/translation/podict.cpp | 2 | ||||
-rw-r--r-- | src/utils/translation/podict.h | 2 | ||||
-rw-r--r-- | src/utils/translation/poparser.cpp | 32 | ||||
-rw-r--r-- | src/utils/translation/poparser.h | 10 | ||||
-rw-r--r-- | src/utils/translation/translationmanager.cpp | 68 | ||||
-rw-r--r-- | src/utils/translation/translationmanager.h | 10 |
6 files changed, 108 insertions, 16 deletions
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 <string.h> @@ -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<char*>(resman->loadFile(getFileName(mLang), size)); + char *buf = static_cast<char*>(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 <sstream> #include <string> @@ -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 <stdlib.h> #include <string.h> -#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<std::string> &lines) +{ + if (!dict || fileName.empty()) + return false; + + int contentsLength; + ResourceManager *resman = ResourceManager::getInstance(); + char *fileContents = static_cast<char*>( + 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 <string> #include <vector> @@ -29,13 +31,19 @@ class PoDict; class TranslationManager { public: - static PoDict *loadLang(std::vector<std::string> lang); + static PoDict *loadLang(std::vector<std::string> 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<std::string> &lines); }; #endif // UTILS_TRANSLATION_MANAGER_H |