CGeneralTextHandler.cpp 20 KB

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