ManaPlus
Static Public Member Functions
TranslationManager Class Reference

#include <translationmanager.h>

Static Public Member Functions

static PoDictloadLang (const LangVect &lang, const std::string &subName, PoDict *const dict)
 
static void init ()
 
static void close ()
 
static void loadCurrentLang ()
 
static void loadDictionaryLang ()
 
static bool translateFile (const std::string &fileName, PoDict *const dict, StringVect &lines)
 
static PoDictreverseLang (const PoDict *const dict)
 

Detailed Description

Definition at line 32 of file translationmanager.h.

Member Function Documentation

◆ close()

void TranslationManager::close ( )
static

Definition at line 68 of file translationmanager.cpp.

69 {
73 }
#define delete2(var)
Definition: delete2.h:25
PoDict * dictionary
Definition: podict.cpp:29
PoDict * translator
Definition: podict.cpp:28
PoDict * reverseDictionary
Definition: podict.cpp:30

References delete2, dictionary, reverseDictionary, and translator.

Referenced by Client::gameClear().

◆ init()

void TranslationManager::init ( )
static

Definition at line 38 of file translationmanager.cpp.

39 {
40  delete translator;
42 }
static PoDict * getEmptyDict()
Definition: poparser.cpp:256

References PoParser::getEmptyDict(), and translator.

◆ loadCurrentLang()

void TranslationManager::loadCurrentLang ( )
static

Definition at line 44 of file translationmanager.cpp.

45 {
46  delete translator;
47  const StringVect lang = getLang();
48  translator = loadLang(lang, "", nullptr);
49  translator = loadLang(lang, "help/", translator);
50 }
static PoDict * loadLang(const LangVect &lang, const std::string &subName, PoDict *const dict)
LangVect getLang()
Definition: langs.cpp:42
std::vector< std::string > StringVect
Definition: stringvector.h:29

References getLang(), loadLang(), and translator.

Referenced by Client::gameExec(), Client::gameInit(), and Client::loadData().

◆ loadDictionaryLang()

void TranslationManager::loadDictionaryLang ( )
static

Definition at line 52 of file translationmanager.cpp.

53 {
54  delete dictionary;
55  delete reverseDictionary;
56  dictionary = loadLang(getServerLang(), "dict/", nullptr);
58 }
static PoDict * reverseLang(const PoDict *const dict)
LangVect getServerLang()
Definition: langs.cpp:68

References dictionary, getServerLang(), loadLang(), reverseDictionary, and reverseLang().

Referenced by Client::gameExec(), Client::gameInit(), Client::loadData(), and PlayerInfo::setServerLanguage().

◆ loadLang()

PoDict * TranslationManager::loadLang ( const LangVect lang,
const std::string &  subName,
PoDict *const  dict 
)
static

Definition at line 75 of file translationmanager.cpp.

78 {
79  std::string name;
80  PoParser parser;
81 
82  FOR_EACH (LangIter, it, lang)
83  {
84  if (*it == "C")
85  continue;
86 
87 // logger->log("check file: " + subName + *it);
88  if (PoParser::checkLang(subName + *it))
89  {
90  name = *it;
91  break;
92  }
93  }
94  if (!name.empty())
95  return parser.load(name, subName + name, dict);
96  logger->log("can't find client data translation");
97  if (dict != nullptr)
98  return dict;
99  return PoParser::getEmptyDict();
100 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
PoDict * load(const std::string &lang, const std::string &fileName, PoDict *const dict)
Definition: poparser.cpp:63
static bool checkLang(const std::string &lang)
Definition: poparser.cpp:261
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
LangVect::const_iterator LangIter
Definition: langs.h:32
Logger * logger
Definition: logger.cpp:89

References PoParser::checkLang(), FOR_EACH, PoParser::getEmptyDict(), PoParser::load(), Logger::log(), and logger.

Referenced by loadCurrentLang(), and loadDictionaryLang().

◆ reverseLang()

PoDict * TranslationManager::reverseLang ( const PoDict *const  dict)
static

Definition at line 148 of file translationmanager.cpp.

149 {
150  PoDict *const revDict = new PoDict(dict->mLang);
151  FOR_EACH (PoMap::const_iterator, it, dict->mPoLines)
152  {
153  revDict->set((*it).second, (*it).first);
154  }
155  return revDict;
156 }
Definition: podict.h:33
std::string mLang
Definition: podict.h:64
void set(const std::string &key, const std::string &value)
Definition: podict.h:56
PoMap mPoLines
Definition: podict.h:63

References FOR_EACH, PoDict::mLang, PoDict::mPoLines, and PoDict::set().

Referenced by loadDictionaryLang().

◆ translateFile()

bool TranslationManager::translateFile ( const std::string &  fileName,
PoDict *const  dict,
StringVect lines 
)
static

Definition at line 102 of file translationmanager.cpp.

105 {
106  if ((dict == nullptr) || fileName.empty())
107  return false;
108 
109  int contentsLength;
110  const char *fileContents = VirtFs::loadFile(fileName,
111  contentsLength);
112 
113  if (fileContents == nullptr)
114  {
115  logger->log("Couldn't load file: %s", fileName.c_str());
116  return false;
117  }
118  std::string str = std::string(fileContents, contentsLength);
119 
120  size_t oldPos1 = std::string::npos;
121  size_t pos1;
122 
123  while ((pos1 = str.find("<<")) != std::string::npos)
124  {
125  if (pos1 == oldPos1)
126  break; // detected infinite loop
127  const size_t pos2 = str.find(">>", pos1 + 2);
128  if (pos2 == std::string::npos)
129  break;
130  const std::string key(str.substr(pos1 + 2, pos2 - pos1 - 2));
131  const std::string key2("<<" + str.substr(
132  pos1 + 2, pos2 - pos1 - 2) + ">>");
133  const std::string val(dict->getStr(key));
134  replaceAll(str, key2, val);
135  oldPos1 = pos1;
136  }
137 
138  std::istringstream iss(str);
139  std::string line;
140 
141  while (getline(iss, line))
142  lines.push_back(line);
143 
144  delete [] fileContents;
145  return true;
146 }
const std::string getStr(const std::string &str)
Definition: podict.cpp:45
const char * loadFile(std::string filename, int &fileSize)
Definition: fs.cpp:859
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
std::string fileName
Definition: testmain.cpp:39

References fileName, PoDict::getStr(), VirtFs::loadFile(), Logger::log(), logger, and replaceAll().

Referenced by DidYouKnowWindow::loadFile(), and HelpWindow::loadFile().


The documentation for this class was generated from the following files: