ManaPlus
translationmanager.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2012-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "fs/virtfs/fs.h"
25 
26 #include "utils/delete2.h"
27 #include "utils/stringutils.h"
28 
29 #include "utils/foreach.h"
30 
33 
34 #include "logger.h"
35 
36 #include "debug.h"
37 
39 {
40  delete translator;
42 }
43 
45 {
46  delete translator;
47  const StringVect lang = getLang();
48  translator = loadLang(lang, "", nullptr);
49  translator = loadLang(lang, "help/", translator);
50 }
51 
53 {
54  delete dictionary;
55  delete reverseDictionary;
56  dictionary = loadLang(getServerLang(), "dict/", nullptr);
58 }
59 
60 #ifdef ENABLE_CUSTOMNLS
61 void TranslationManager::loadGettextLang()
62 {
63  delete mainTranslator;
64  mainTranslator = loadLang(getLang(), "manaplus/", nullptr);
65 }
66 #endif // ENABLE_CUSTOMNLS
67 
69 {
73 }
74 
76  const std::string &subName,
77  PoDict *const dict)
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 }
101 
103  PoDict *const dict,
104  StringVect &lines)
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 }
147 
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 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
Definition: podict.h:33
const std::string getStr(const std::string &str)
Definition: podict.cpp:45
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
PoDict * load(const std::string &lang, const std::string &fileName, PoDict *const dict)
Definition: poparser.cpp:63
static PoDict * getEmptyDict()
Definition: poparser.cpp:256
static bool checkLang(const std::string &lang)
Definition: poparser.cpp:261
static void loadCurrentLang()
static PoDict * reverseLang(const PoDict *const dict)
static PoDict * loadLang(const LangVect &lang, const std::string &subName, PoDict *const dict)
static bool translateFile(const std::string &fileName, PoDict *const dict, StringVect &lines)
static void loadDictionaryLang()
#define delete2(var)
Definition: delete2.h:25
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
LangVect getServerLang()
Definition: langs.cpp:68
LangVect getLang()
Definition: langs.cpp:42
LangVect::const_iterator LangIter
Definition: langs.h:32
std::vector< std::string > LangVect
Definition: langs.h:31
Logger * logger
Definition: logger.cpp:89
const char * loadFile(std::string filename, int &fileSize)
Definition: fs.cpp:859
PoDict * dictionary
Definition: podict.cpp:29
PoDict * translator
Definition: podict.cpp:28
PoDict * reverseDictionary
Definition: podict.cpp:30
std::string & replaceAll(std::string &context, const std::string &from, const std::string &to)
std::vector< std::string > StringVect
Definition: stringvector.h:29
std::string fileName
Definition: testmain.cpp:39