CGeneralTextHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * CGeneralTextHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGeneralTextHandler.h"
  12. #include "filesystem/Filesystem.h"
  13. #include "CConfigHandler.h"
  14. #include "CModHandler.h"
  15. #include "mapObjects/CQuest.h"
  16. #include "VCMI_Lib.h"
  17. #include "Languages.h"
  18. #include "TextOperations.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. /// Detects language and encoding of H3 text files based on matching against pregenerated footprints of H3 file
  21. void CGeneralTextHandler::detectInstallParameters()
  22. {
  23. using LanguageFootprint = std::array<double, 16>;
  24. static const std::array<LanguageFootprint, 7> knownFootprints =
  25. { {
  26. { { 0.1602, 0.0000, 0.0357, 0.0054, 0.0038, 0.0017, 0.0077, 0.0214, 0.0000, 0.0000, 0.1264, 0.1947, 0.2012, 0.1406, 0.0480, 0.0532 } },
  27. { { 0.0559, 0.0000, 0.1983, 0.0051, 0.0222, 0.0183, 0.4596, 0.2405, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000 } },
  28. { { 0.0493, 0.0000, 0.1926, 0.0047, 0.0230, 0.0121, 0.4133, 0.2780, 0.0002, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0259, 0.0008 } },
  29. { { 0.0534, 0.0000, 0.1705, 0.0047, 0.0418, 0.0208, 0.4775, 0.2191, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0005, 0.0036, 0.0080 } },
  30. { { 0.0534, 0.0000, 0.1701, 0.0067, 0.0157, 0.0133, 0.4328, 0.2540, 0.0001, 0.0043, 0.0000, 0.0244, 0.0000, 0.0000, 0.0181, 0.0071 } },
  31. { { 0.0548, 0.0000, 0.1744, 0.0061, 0.0031, 0.0009, 0.0046, 0.0136, 0.0000, 0.0004, 0.0000, 0.0000, 0.0227, 0.0061, 0.4882, 0.2252 } },
  32. { { 0.0559, 0.0000, 0.1807, 0.0059, 0.0036, 0.0013, 0.0046, 0.0134, 0.0000, 0.0004, 0.0000, 0.0487, 0.0209, 0.0060, 0.4615, 0.1972 } },
  33. } };
  34. static const std::array<std::string, 7> knownLanguages =
  35. { {
  36. "chinese",
  37. "english",
  38. "french",
  39. "german",
  40. "polish",
  41. "russian",
  42. "ukrainian"
  43. } };
  44. // load file that will be used for footprint generation
  45. // this is one of the most text-heavy files in game and consists solely from translated texts
  46. auto resource = CResourceHandler::get("core")->load(ResourceID("DATA/GENRLTXT.TXT", EResType::TEXT));
  47. std::array<size_t, 256> charCount{};
  48. std::array<double, 16> footprint{};
  49. std::array<double, 6> deviations{};
  50. auto data = resource->readAll();
  51. // compute how often each character occurs in input file
  52. for (si64 i = 0; i < data.second; ++i)
  53. charCount[data.first[i]] += 1;
  54. // and convert computed data into weights
  55. // to reduce amount of data, group footprint data into 16-char blocks.
  56. // While this will reduce precision, it should not affect output
  57. // since we expect only tiny differences compared to reference footprints
  58. for (size_t i = 0; i < 256; ++i)
  59. footprint[i/16] += static_cast<double>(charCount[i]) / data.second;
  60. logGlobal->debug("Language footprint: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
  61. footprint[0], footprint[1], footprint[2], footprint[3], footprint[4], footprint[5], footprint[6], footprint[7],
  62. footprint[8], footprint[9], footprint[10], footprint[11], footprint[12], footprint[13], footprint[14], footprint[15]
  63. );
  64. for (size_t i = 0; i < deviations.size(); ++i)
  65. {
  66. for (size_t j = 0; j < footprint.size(); ++j)
  67. deviations[i] += std::abs((footprint[j] - knownFootprints[i][j]));
  68. }
  69. size_t bestIndex = boost::range::min_element(deviations) - deviations.begin();
  70. for (size_t i = 0; i < deviations.size(); ++i)
  71. logGlobal->debug("Comparing to %s: %f", knownLanguages[i], deviations[i]);
  72. Settings language = settings.write["session"]["language"];
  73. language->String() = knownLanguages[bestIndex];
  74. Settings confidence = settings.write["session"]["languageDeviation"];
  75. confidence->Float() = deviations[bestIndex];
  76. Settings encoding = settings.write["session"]["encoding"];
  77. encoding->String() = Languages::getLanguageOptions(knownLanguages[bestIndex]).encoding;
  78. }
  79. //Helper for string -> float conversion
  80. class LocaleWithComma: public std::numpunct<char>
  81. {
  82. protected:
  83. char do_decimal_point() const override
  84. {
  85. return ',';
  86. }
  87. };
  88. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  89. {
  90. ResourceID resource(URI, EResType::TEXT);
  91. auto input = CResourceHandler::get()->load(resource);
  92. std::string modName = VLC->modh->findResourceOrigin(resource);
  93. std::string language = VLC->modh->getModLanguage(modName);
  94. fileEncoding = Languages::getLanguageOptions(language).encoding;
  95. data.reset(new char[input->getSize()]);
  96. input->read(reinterpret_cast<uint8_t*>(data.get()), input->getSize());
  97. curr = data.get();
  98. end = curr + input->getSize();
  99. }
  100. std::string CLegacyConfigParser::extractQuotedPart()
  101. {
  102. assert(*curr == '\"');
  103. curr++; // skip quote
  104. char * begin = curr;
  105. while (curr != end && *curr != '\"' && *curr != '\t')
  106. curr++;
  107. return std::string(begin, curr++); //increment curr to close quote
  108. }
  109. std::string CLegacyConfigParser::extractQuotedString()
  110. {
  111. assert(*curr == '\"');
  112. std::string ret;
  113. while (true)
  114. {
  115. ret += extractQuotedPart();
  116. // double quote - add it to string and continue quoted part
  117. if (curr < end && *curr == '\"')
  118. {
  119. ret += '\"';
  120. }
  121. //extract normal part
  122. else if(curr < end && *curr != '\t' && *curr != '\r')
  123. {
  124. char * begin = curr;
  125. while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
  126. curr++;
  127. ret += std::string(begin, curr);
  128. if(curr>=end || *curr != '\"')
  129. return ret;
  130. }
  131. else // end of string
  132. return ret;
  133. }
  134. }
  135. std::string CLegacyConfigParser::extractNormalString()
  136. {
  137. char * begin = curr;
  138. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  139. curr++;
  140. return std::string(begin, curr);
  141. }
  142. std::string CLegacyConfigParser::readRawString()
  143. {
  144. if (curr >= end || *curr == '\n')
  145. return "";
  146. std::string ret;
  147. if (*curr == '\"')
  148. ret = extractQuotedString();// quoted text - find closing quote
  149. else
  150. ret = extractNormalString();//string without quotes - copy till \t or \r
  151. curr++;
  152. return ret;
  153. }
  154. std::string CLegacyConfigParser::readString()
  155. {
  156. // do not convert strings that are already in ASCII - this will only slow down loading process
  157. std::string str = readRawString();
  158. if (TextOperations::isValidASCII(str))
  159. return str;
  160. return TextOperations::toUnicode(str, fileEncoding);
  161. }
  162. float CLegacyConfigParser::readNumber()
  163. {
  164. std::string input = readRawString();
  165. std::istringstream stream(input);
  166. if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  167. stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
  168. float result;
  169. if ( !(stream >> result) )
  170. return 0;
  171. return result;
  172. }
  173. bool CLegacyConfigParser::isNextEntryEmpty() const
  174. {
  175. char * nextSymbol = curr;
  176. while (nextSymbol < end && *nextSymbol == ' ')
  177. nextSymbol++; //find next meaningfull symbol
  178. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  179. }
  180. bool CLegacyConfigParser::endLine()
  181. {
  182. while (curr < end && *curr != '\n')
  183. readString();
  184. curr++;
  185. return curr < end;
  186. }
  187. void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string const & sourceName)
  188. {
  189. CLegacyConfigParser parser(sourceName);
  190. size_t index = 0;
  191. do
  192. {
  193. registerString( "core", {sourceID, index}, parser.readString());
  194. index += 1;
  195. }
  196. while (parser.endLine());
  197. }
  198. const std::string & CGeneralTextHandler::deserialize(const TextIdentifier & identifier) const
  199. {
  200. if(stringsLocalizations.count(identifier.get()) == 0)
  201. {
  202. logGlobal->error("Unable to find localization for string '%s'", identifier.get());
  203. return identifier.get();
  204. }
  205. auto const & entry = stringsLocalizations.at(identifier.get());
  206. if (!entry.overrideValue.empty())
  207. return entry.overrideValue;
  208. return entry.baseValue;
  209. }
  210. void CGeneralTextHandler::registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized)
  211. {
  212. assert(!modContext.empty());
  213. assert(!getModLanguage(modContext).empty());
  214. assert(UID.get().find("..") == std::string::npos); // invalid identifier - there is section that was evaluated to empty string
  215. //assert(stringsLocalizations.count(UID.get()) == 0); // registering already registered string?
  216. if(stringsLocalizations.count(UID.get()) > 0)
  217. {
  218. auto & value = stringsLocalizations[UID.get()];
  219. if(value.baseLanguage.empty())
  220. {
  221. value.baseLanguage = getModLanguage(modContext);
  222. value.baseValue = localized;
  223. }
  224. else
  225. {
  226. if(value.baseValue != localized)
  227. logMod->warn("Duplicate registered string '%s' found! Old value: '%s', new value: '%s'", UID.get(), value.baseValue, localized);
  228. }
  229. }
  230. else
  231. {
  232. StringState result;
  233. result.baseLanguage = getModLanguage(modContext);
  234. result.baseValue = localized;
  235. result.modContext = modContext;
  236. stringsLocalizations[UID.get()] = result;
  237. }
  238. }
  239. void CGeneralTextHandler::registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized)
  240. {
  241. assert(!modContext.empty());
  242. assert(!language.empty());
  243. // NOTE: implicitly creates entry, intended - strings added by vcmi (and potential UI mods) are not registered anywhere at the moment
  244. auto & entry = stringsLocalizations[UID.get()];
  245. entry.overrideLanguage = language;
  246. entry.overrideValue = localized;
  247. if (entry.modContext.empty())
  248. entry.modContext = modContext;
  249. }
  250. bool CGeneralTextHandler::validateTranslation(const std::string & language, const std::string & modContext, const JsonNode & config) const
  251. {
  252. bool allPresent = true;
  253. for (auto const & string : stringsLocalizations)
  254. {
  255. if (string.second.modContext != modContext)
  256. continue; // Not our mod
  257. if (string.second.overrideLanguage == language)
  258. continue; // Already translated
  259. if (string.second.baseLanguage == language && !string.second.baseValue.empty())
  260. continue; // Base string already uses our language
  261. if (config.Struct().count(string.first) > 0)
  262. continue;
  263. if (allPresent)
  264. logMod->warn("Translation into language '%s' in mod '%s' is incomplete! Missing lines:", language, modContext);
  265. std::string currentText;
  266. if (string.second.overrideValue.empty())
  267. currentText = string.second.baseValue;
  268. else
  269. currentText = string.second.overrideValue;
  270. logMod->warn(R"( "%s" : "%s",)", string.first, TextOperations::escapeString(currentText));
  271. allPresent = false;
  272. }
  273. bool allFound = true;
  274. for (auto const & string : config.Struct())
  275. {
  276. if (stringsLocalizations.count(string.first) > 0)
  277. continue;
  278. if (allFound)
  279. logMod->warn("Translation into language '%s' in mod '%s' has unused lines:", language, modContext);
  280. logMod->warn(R"( "%s" : "%s",)", string.first, TextOperations::escapeString(string.second.String()));
  281. allFound = false;
  282. }
  283. return allPresent && allFound;
  284. }
  285. void CGeneralTextHandler::loadTranslationOverrides(const std::string & language, const std::string & modContext, const JsonNode & config)
  286. {
  287. for ( auto const & node : config.Struct())
  288. registerStringOverride(modContext, language, node.first, node.second.String());
  289. }
  290. CGeneralTextHandler::CGeneralTextHandler():
  291. victoryConditions(*this, "core.vcdesc" ),
  292. lossCondtions (*this, "core.lcdesc" ),
  293. colors (*this, "core.plcolors" ),
  294. tcommands (*this, "core.tcommand" ),
  295. hcommands (*this, "core.hallinfo" ),
  296. fcommands (*this, "core.castinfo" ),
  297. advobtxt (*this, "core.advevent" ),
  298. restypes (*this, "core.restypes" ),
  299. randsign (*this, "core.randsign" ),
  300. overview (*this, "core.overview" ),
  301. arraytxt (*this, "core.arraytxt" ),
  302. primarySkillNames(*this, "core.priskill" ),
  303. jktexts (*this, "core.jktext" ),
  304. tavernInfo (*this, "core.tvrninfo" ),
  305. tavernRumors (*this, "core.randtvrn" ),
  306. turnDurations (*this, "core.turndur" ),
  307. heroscrn (*this, "core.heroscrn" ),
  308. tentColors (*this, "core.tentcolr" ),
  309. levels (*this, "core.skilllev" ),
  310. zelp (*this, "core.help" ),
  311. allTexts (*this, "core.genrltxt" ),
  312. // pseudo-array, that don't have H3 file with same name
  313. seerEmpty (*this, "core.seerhut.empty" ),
  314. seerNames (*this, "core.seerhut.names" ),
  315. capColors (*this, "vcmi.capitalColors" ),
  316. znpc00 (*this, "vcmi.znpc00" ), // technically - wog
  317. qeModCommands (*this, "vcmi.quickExchange" )
  318. {
  319. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  320. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  321. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  322. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  323. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  324. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  325. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  326. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  327. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  328. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  329. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  330. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  331. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  332. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  333. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  334. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  335. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  336. readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
  337. readToVector("core.minename", "DATA/MINENAME.TXT" );
  338. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  339. static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
  340. if (CResourceHandler::get()->existsResource(ResourceID(QE_MOD_COMMANDS, EResType::TEXT)))
  341. readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
  342. {
  343. CLegacyConfigParser parser("DATA/RANDTVRN.TXT");
  344. parser.endLine();
  345. size_t index = 0;
  346. do
  347. {
  348. std::string line = parser.readString();
  349. if(!line.empty())
  350. {
  351. registerString("core", {"core.randtvrn", index}, line);
  352. index += 1;
  353. }
  354. }
  355. while (parser.endLine());
  356. }
  357. {
  358. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  359. parser.endLine();
  360. size_t index = 0;
  361. do
  362. {
  363. registerString("core", {"core.genrltxt", index}, parser.readString());
  364. index += 1;
  365. }
  366. while (parser.endLine());
  367. }
  368. {
  369. CLegacyConfigParser parser("DATA/HELP.TXT");
  370. size_t index = 0;
  371. do
  372. {
  373. std::string first = parser.readString();
  374. std::string second = parser.readString();
  375. registerString("core", "core.help." + std::to_string(index) + ".hover", first);
  376. registerString("core", "core.help." + std::to_string(index) + ".help", second);
  377. index += 1;
  378. }
  379. while (parser.endLine());
  380. }
  381. {
  382. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  383. size_t index = 0;
  384. do
  385. {
  386. std::string color = parser.readString();
  387. registerString("core", {"core.plcolors", index}, color);
  388. color[0] = toupper(color[0]);
  389. registerString("core", {"vcmi.capitalColors", index}, color);
  390. index += 1;
  391. }
  392. while (parser.endLine());
  393. }
  394. {
  395. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  396. //skip header
  397. parser.endLine();
  398. for (size_t i = 0; i < 6; ++i)
  399. {
  400. registerString("core", {"core.seerhut.empty", i}, parser.readString());
  401. }
  402. parser.endLine();
  403. for (size_t i = 0; i < 9; ++i) //9 types of quests
  404. {
  405. std::string questName = CQuest::missionName(static_cast<CQuest::Emission>(1+i));
  406. for (size_t j = 0; j < 5; ++j)
  407. {
  408. std::string questState = CQuest::missionState(j);
  409. parser.readString(); //front description
  410. for (size_t k = 0; k < 6; ++k)
  411. {
  412. registerString("core", {"core.seerhut.quest", questName, questState, k}, parser.readString());
  413. }
  414. parser.endLine();
  415. }
  416. }
  417. for (size_t k = 0; k < 6; ++k) //Time limit
  418. {
  419. registerString("core", {"core.seerhut.time", k}, parser.readString());
  420. }
  421. parser.endLine();
  422. parser.endLine(); // empty line
  423. parser.endLine(); // header
  424. for (size_t i = 0; i < 48; ++i)
  425. {
  426. registerString("core", {"core.seerhut.names", i}, parser.readString());
  427. parser.endLine();
  428. }
  429. }
  430. {
  431. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  432. //skip header
  433. parser.endLine();
  434. std::string text;
  435. size_t campaignsCount = 0;
  436. do
  437. {
  438. text = parser.readString();
  439. if (!text.empty())
  440. {
  441. registerString("core", {"core.camptext.names", campaignsCount}, text);
  442. campaignsCount += 1;
  443. }
  444. }
  445. while (parser.endLine() && !text.empty());
  446. for (size_t campaign=0; campaign<campaignsCount; campaign++)
  447. {
  448. size_t region = 0;
  449. do // skip empty space and header
  450. {
  451. text = parser.readString();
  452. }
  453. while (parser.endLine() && text.empty());
  454. do
  455. {
  456. text = parser.readString();
  457. if (!text.empty())
  458. {
  459. registerString("core", {"core.camptext.regions", std::to_string(campaign), region}, text);
  460. region += 1;
  461. }
  462. }
  463. while (parser.endLine() && !text.empty());
  464. scenariosCountPerCampaign.push_back(region);
  465. }
  466. }
  467. if (VLC->modh->modules.COMMANDERS)
  468. {
  469. if(CResourceHandler::get()->existsResource(ResourceID("DATA/ZNPC00.TXT", EResType::TEXT)))
  470. readToVector("vcmi.znpc00", "DATA/ZNPC00.TXT" );
  471. }
  472. }
  473. int32_t CGeneralTextHandler::pluralText(int32_t textIndex, int32_t count) const
  474. {
  475. if(textIndex == 0)
  476. return 0;
  477. if(textIndex < 0)
  478. return -textIndex;
  479. if(count == 1)
  480. return textIndex;
  481. return textIndex + 1;
  482. }
  483. void CGeneralTextHandler::dumpAllTexts()
  484. {
  485. logGlobal->info("BEGIN TEXT EXPORT");
  486. for ( auto const & entry : stringsLocalizations)
  487. {
  488. if (!entry.second.overrideValue.empty())
  489. logGlobal->info(R"("%s" : "%s",)", entry.first, TextOperations::escapeString(entry.second.overrideValue));
  490. else
  491. logGlobal->info(R"("%s" : "%s",)", entry.first, TextOperations::escapeString(entry.second.baseValue));
  492. }
  493. logGlobal->info("END TEXT EXPORT");
  494. }
  495. size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const
  496. {
  497. assert(campaignID < scenariosCountPerCampaign.size());
  498. if(campaignID < scenariosCountPerCampaign.size())
  499. return scenariosCountPerCampaign[campaignID];
  500. return 0;
  501. }
  502. std::string CGeneralTextHandler::getModLanguage(const std::string & modContext)
  503. {
  504. if (modContext == "core")
  505. return getInstalledLanguage();
  506. return VLC->modh->getModLanguage(modContext);
  507. }
  508. std::string CGeneralTextHandler::getPreferredLanguage()
  509. {
  510. assert(!settings["general"]["language"].String().empty());
  511. return settings["general"]["language"].String();
  512. }
  513. std::string CGeneralTextHandler::getInstalledLanguage()
  514. {
  515. assert(!settings["session"]["language"].String().empty());
  516. return settings["session"]["language"].String();
  517. }
  518. std::string CGeneralTextHandler::getInstalledEncoding()
  519. {
  520. assert(!settings["session"]["encoding"].String().empty());
  521. return settings["session"]["encoding"].String();
  522. }
  523. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(std::string const & prefix)
  524. {
  525. std::vector<std::string> result;
  526. for (auto const & entry : stringsLocalizations)
  527. {
  528. if(boost::algorithm::starts_with(entry.first, prefix))
  529. result.push_back(entry.first);
  530. }
  531. return result;
  532. }
  533. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath):
  534. owner(owner),
  535. basePath(basePath)
  536. {}
  537. std::string LegacyTextContainer::operator[](size_t index) const
  538. {
  539. return owner.translate(basePath, index);
  540. }
  541. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):
  542. owner(owner),
  543. basePath(basePath)
  544. {}
  545. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  546. {
  547. return {
  548. owner.translate(basePath + "." + std::to_string(index) + ".hover"),
  549. owner.translate(basePath + "." + std::to_string(index) + ".help")
  550. };
  551. }
  552. VCMI_LIB_NAMESPACE_END