CGeneralTextHandler.cpp 21 KB

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