mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	All translatable strings now have language identifier
This commit is contained in:
		| @@ -346,9 +346,9 @@ CArtifact * CArtHandler::loadFromJson(const std::string & scope, const JsonNode | ||||
|  | ||||
| 	const JsonNode & text = node["text"]; | ||||
|  | ||||
| 	VLC->generaltexth->registerString(art->getNameTextID(), text["name"].String()); | ||||
| 	VLC->generaltexth->registerString(art->getDescriptionTextID(), text["description"].String()); | ||||
| 	VLC->generaltexth->registerString(art->getEventTextID(), text["event"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, art->getNameTextID(), text["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, art->getDescriptionTextID(), text["description"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, art->getEventTextID(), text["event"].String()); | ||||
|  | ||||
| 	const JsonNode & graphics = node["graphics"]; | ||||
| 	art->image = graphics["image"].String(); | ||||
|   | ||||
| @@ -240,8 +240,8 @@ void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, con | ||||
|  | ||||
| 	if (!dest.hidden) | ||||
| 	{ | ||||
| 		VLC->generaltexth->registerString(dest.getNameTextID(), source["name"].String()); | ||||
| 		VLC->generaltexth->registerString(dest.getDescriptionTextID(), source["description"].String()); | ||||
| 		VLC->generaltexth->registerString( "core", dest.getNameTextID(), source["name"].String()); | ||||
| 		VLC->generaltexth->registerString( "core", dest.getDescriptionTextID(), source["description"].String()); | ||||
| 	} | ||||
|  | ||||
| 	const JsonNode & graphics = source["graphics"]; | ||||
|   | ||||
| @@ -636,8 +636,8 @@ CCreature * CCreatureHandler::loadFromJson(const std::string & scope, const Json | ||||
|  | ||||
| 	cre->cost = Res::ResourceSet(node["cost"]); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(cre->getNameSingularTextID(), node["name"]["singular"].String()); | ||||
| 	VLC->generaltexth->registerString(cre->getNamePluralTextID(), node["name"]["plural"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, cre->getNameSingularTextID(), node["name"]["singular"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, cre->getNamePluralTextID(), node["name"]["plural"].String()); | ||||
|  | ||||
| 	cre->addBonus(node["hitPoints"].Integer(), Bonus::STACK_HEALTH); | ||||
| 	cre->addBonus(node["speed"].Integer(), Bonus::STACKS_SPEED); | ||||
|   | ||||
| @@ -104,7 +104,7 @@ bool Unicode::isValidString(const char * data, size_t size) | ||||
| } | ||||
|  | ||||
| /// Detects language and encoding of H3 text files based on matching against pregenerated footprints of H3 file | ||||
| void CGeneralTextHandler::detectInstallParameters() const | ||||
| void CGeneralTextHandler::detectInstallParameters() | ||||
| { | ||||
| 	struct LanguageFootprint | ||||
| 	{ | ||||
| @@ -115,25 +115,21 @@ void CGeneralTextHandler::detectInstallParameters() const | ||||
|  | ||||
| 	static const std::vector<LanguageFootprint> knownFootprints = | ||||
| 	{ | ||||
| 		{ "English",   "CP1252", { { 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 } } }, | ||||
| 		{ "French",    "CP1252", { { 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 } } }, | ||||
| 		{ "German",    "CP1252", { { 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 } } }, | ||||
| 		{ "Polish",    "CP1250", { { 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 } } }, | ||||
| 		{ "Russian",   "CP1251", { { 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 } } }, | ||||
| 		{ "Ukrainian", "CP1251", { { 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 } } }, | ||||
| 		{ "english",   "CP1252", { { 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 } } }, | ||||
| 		{ "french",    "CP1252", { { 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 } } }, | ||||
| 		{ "german",    "CP1252", { { 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 } } }, | ||||
| 		{ "polish",    "CP1250", { { 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 } } }, | ||||
| 		{ "russian",   "CP1251", { { 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 } } }, | ||||
| 		{ "ukrainian", "CP1251", { { 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 } } }, | ||||
| 	}; | ||||
|  | ||||
| 	// load file that will be used for footprint generation | ||||
| 	// this is one of the most text-heavy files in game and consists solely from translated texts | ||||
| 	auto resource = CResourceHandler::get()->load(ResourceID("DATA/GENRLTXT.TXT", EResType::TEXT)); | ||||
|  | ||||
| 	std::array<size_t, 256> charCount; | ||||
| 	std::array<double, 16> footprint; | ||||
| 	std::vector<double> deviations; | ||||
|  | ||||
| 	boost::range::fill(charCount, 0); | ||||
| 	boost::range::fill(footprint, 0.0); | ||||
| 	deviations.resize(knownFootprints.size(), 0.0); | ||||
| 	std::array<size_t, 256> charCount{}; | ||||
| 	std::array<double, 16> footprint{}; | ||||
| 	std::vector<double> deviations(knownFootprints.size(), 0.0); | ||||
|  | ||||
| 	auto data = resource->readAll(); | ||||
|  | ||||
| @@ -146,7 +142,7 @@ void CGeneralTextHandler::detectInstallParameters() const | ||||
| 	// While this will reduce precision, it should not affect output | ||||
| 	// since we expect only tiny differences compared to reference footprints | ||||
| 	for (size_t i = 0; i < 256; ++i) | ||||
| 		footprint[i/16] += double(charCount[i]) / data.second; | ||||
| 		footprint[i/16] += static_cast<double>(charCount[i]) / data.second; | ||||
|  | ||||
| 	logGlobal->debug("Language footprint: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f", | ||||
| 			footprint[0], footprint[1], footprint[2],  footprint[3],  footprint[4],  footprint[5],  footprint[6],  footprint[7], | ||||
| @@ -164,6 +160,9 @@ void CGeneralTextHandler::detectInstallParameters() const | ||||
| 	for (size_t i = 0; i < deviations.size(); ++i) | ||||
| 		logGlobal->debug("Comparing to %s: %f", knownFootprints[i].language, deviations[i]); | ||||
|  | ||||
| 	Settings language = settings.write["session"]["language"]; | ||||
| 	language->String() = knownFootprints[bestIndex].language; | ||||
|  | ||||
| 	Settings encoding = settings.write["session"]["encoding"]; | ||||
| 	encoding->String() = knownFootprints[bestIndex].encoding; | ||||
| } | ||||
| @@ -227,20 +226,15 @@ protected: | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| CLegacyConfigParser::CLegacyConfigParser(std::string URI) | ||||
| CLegacyConfigParser::CLegacyConfigParser(std::string URI): | ||||
| 	CLegacyConfigParser(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT))) | ||||
| { | ||||
| 	init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT))); | ||||
| } | ||||
|  | ||||
| CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input) | ||||
| { | ||||
| 	init(input); | ||||
| } | ||||
|  | ||||
| void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input) | ||||
| { | ||||
| 	data.reset(new char[input->getSize()]); | ||||
| 	input->read((ui8*)data.get(), input->getSize()); | ||||
| 	input->read(reinterpret_cast<uint8_t*>(data.get()), input->getSize()); | ||||
|  | ||||
| 	curr = data.get(); | ||||
| 	end = curr + input->getSize(); | ||||
| @@ -366,7 +360,7 @@ void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string | ||||
| 	size_t index = 0; | ||||
| 	do | ||||
| 	{ | ||||
| 		registerString({sourceID, index}, parser.readString()); | ||||
| 		registerString( "core", {sourceID, index}, parser.readString()); | ||||
| 		index += 1; | ||||
| 	} | ||||
| 	while (parser.endLine()); | ||||
| @@ -374,31 +368,52 @@ void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string | ||||
|  | ||||
| const std::string & CGeneralTextHandler::deserialize(const TextIdentifier & identifier) const | ||||
| { | ||||
| 	if(stringsOverrides.count(identifier.get())) | ||||
| 		return stringsOverrides.at(identifier.get()); | ||||
| 	if(stringsLocalizations.count(identifier.get()) == 0) | ||||
| 	{ | ||||
| 		logGlobal->error("Unable to find localization for string '%s'", identifier.get()); | ||||
| 		return identifier.get(); | ||||
| 	} | ||||
|  | ||||
| 	if(stringsLocalizations.count(identifier.get())) | ||||
| 		return stringsLocalizations.at(identifier.get()); | ||||
| 	auto const & entry = stringsLocalizations.at(identifier.get()); | ||||
|  | ||||
| 	logGlobal->error("Unable to find localization for string '%s'", identifier.get()); | ||||
| 	return identifier.get(); | ||||
| 	if (!entry.overrideValue.empty()) | ||||
| 		return entry.overrideValue; | ||||
| 	return entry.baseValue; | ||||
| } | ||||
|  | ||||
| void CGeneralTextHandler::registerString(const TextIdentifier & UID, const std::string & localized) | ||||
| void CGeneralTextHandler::registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized) | ||||
| { | ||||
| 	assert(UID.get().find("..") == std::string::npos); | ||||
| 	stringsLocalizations[UID.get()] = localized; | ||||
| 	assert(UID.get().find("..") == std::string::npos); // invalid identifier - there is section that was evaluated to empty string | ||||
| 	assert(stringsLocalizations.count(UID.get()) == 0); // registering already registered string? | ||||
| 	assert(!modContext.empty()); | ||||
| 	assert(!getModLanguage(modContext).empty()); | ||||
|  | ||||
| 	StringState result; | ||||
| 	result.baseLanguage = getModLanguage(modContext); | ||||
| 	result.baseValue = localized; | ||||
| 	result.modContext = modContext; | ||||
|  | ||||
| 	stringsLocalizations[UID.get()] = result; | ||||
| } | ||||
|  | ||||
| void CGeneralTextHandler::registerStringOverride(const TextIdentifier & UID, const std::string & localized) | ||||
| void CGeneralTextHandler::registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized) | ||||
| { | ||||
| 	stringsOverrides[UID.get()] = localized; | ||||
| 	assert(!modContext.empty()); | ||||
| 	assert(!language.empty()); | ||||
|  | ||||
| 	// NOTE: implicitly creates entry, intended - strings added by vcmi (and potential UI mods) are not registered anywhere at the moment | ||||
| 	auto & entry = stringsLocalizations[UID.get()]; | ||||
|  | ||||
| 	entry.overrideLanguage = language; | ||||
| 	entry.overrideValue = localized; | ||||
| 	if (entry.modContext.empty()) | ||||
| 		entry.modContext = modContext; | ||||
| } | ||||
|  | ||||
| void CGeneralTextHandler::loadTranslationOverrides(const JsonNode & config) | ||||
| void CGeneralTextHandler::loadTranslationOverrides(const std::string & language, const JsonNode & config) | ||||
| { | ||||
| 	for ( auto const & node : config.Struct()) | ||||
| 		registerStringOverride(node.first, node.second.String()); | ||||
| 		registerStringOverride(node.second.meta, language, node.first, node.second.String()); | ||||
| } | ||||
|  | ||||
| CGeneralTextHandler::CGeneralTextHandler(): | ||||
| @@ -466,7 +481,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 			std::string line = parser.readString(); | ||||
| 			if(!line.empty()) | ||||
| 			{ | ||||
| 				registerString({"core.randtvrn", index}, line); | ||||
| 				registerString("core", {"core.randtvrn", index}, line); | ||||
| 				index += 1; | ||||
| 			} | ||||
| 		} | ||||
| @@ -478,7 +493,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 		size_t index = 0; | ||||
| 		do | ||||
| 		{ | ||||
| 			registerString({"core.genrltxt", index}, parser.readString()); | ||||
| 			registerString("core", {"core.genrltxt", index}, parser.readString()); | ||||
| 			index += 1; | ||||
| 		} | ||||
| 		while (parser.endLine()); | ||||
| @@ -490,8 +505,8 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 		{ | ||||
| 			std::string first = parser.readString(); | ||||
| 			std::string second = parser.readString(); | ||||
| 			registerString("core.help." + std::to_string(index) + ".hover", first); | ||||
| 			registerString("core.help." + std::to_string(index) + ".help",  second); | ||||
| 			registerString("core", "core.help." + std::to_string(index) + ".hover", first); | ||||
| 			registerString("core", "core.help." + std::to_string(index) + ".help",  second); | ||||
| 			index += 1; | ||||
| 		} | ||||
| 		while (parser.endLine()); | ||||
| @@ -503,9 +518,9 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 		{ | ||||
| 			std::string color = parser.readString(); | ||||
|  | ||||
| 			registerString({"core.plcolors", index}, color); | ||||
| 			registerString("core", {"core.plcolors", index}, color); | ||||
| 			color[0] = toupper(color[0]); | ||||
| 			registerString({"vcmi.capitalColors", index}, color); | ||||
| 			registerString("core", {"vcmi.capitalColors", index}, color); | ||||
| 			index += 1; | ||||
| 		} | ||||
| 		while (parser.endLine()); | ||||
| @@ -518,13 +533,13 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
|  | ||||
| 		for (size_t i = 0; i < 6; ++i) | ||||
| 		{ | ||||
| 			registerString({"core.seerhut.empty", i}, parser.readString()); | ||||
| 			registerString("core", {"core.seerhut.empty", i}, parser.readString()); | ||||
| 		} | ||||
| 		parser.endLine(); | ||||
|  | ||||
| 		for (size_t i = 0; i < 9; ++i) //9 types of quests | ||||
| 		{ | ||||
| 			std::string questName = CQuest::missionName(CQuest::Emission(1+i)); | ||||
| 			std::string questName = CQuest::missionName(static_cast<CQuest::Emission>(1+i)); | ||||
|  | ||||
| 			for (size_t j = 0; j < 5; ++j) | ||||
| 			{ | ||||
| @@ -533,7 +548,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 				parser.readString(); //front description | ||||
| 				for (size_t k = 0; k < 6; ++k) | ||||
| 				{ | ||||
| 					registerString({"core.seerhut.quest", questName, questState, k}, parser.readString()); | ||||
| 					registerString("core", {"core.seerhut.quest", questName, questState, k}, parser.readString()); | ||||
| 				} | ||||
| 				parser.endLine(); | ||||
| 			} | ||||
| @@ -541,7 +556,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
|  | ||||
| 		for (size_t k = 0; k < 6; ++k) //Time limit | ||||
| 		{ | ||||
| 			registerString({"core.seerhut.time", k}, parser.readString()); | ||||
| 			registerString("core", {"core.seerhut.time", k}, parser.readString()); | ||||
| 		} | ||||
| 		parser.endLine(); | ||||
|  | ||||
| @@ -550,7 +565,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
|  | ||||
| 		for (size_t i = 0; i < 48; ++i) | ||||
| 		{ | ||||
| 			registerString({"core.seerhut.names", i}, parser.readString()); | ||||
| 			registerString("core", {"core.seerhut.names", i}, parser.readString()); | ||||
| 			parser.endLine(); | ||||
| 		} | ||||
| 	} | ||||
| @@ -567,7 +582,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 			text = parser.readString(); | ||||
| 			if (!text.empty()) | ||||
| 			{ | ||||
| 				registerString({"core.camptext.names", campaignsCount}, text); | ||||
| 				registerString("core", {"core.camptext.names", campaignsCount}, text); | ||||
| 				campaignsCount += 1; | ||||
| 			} | ||||
| 		} | ||||
| @@ -588,7 +603,7 @@ CGeneralTextHandler::CGeneralTextHandler(): | ||||
| 				text = parser.readString(); | ||||
| 				if (!text.empty()) | ||||
| 				{ | ||||
| 					registerString({"core.camptext.regions", std::to_string(campaign), region}, text); | ||||
| 					registerString("core", {"core.camptext.regions", std::to_string(campaign), region}, text); | ||||
| 					region += 1; | ||||
| 				} | ||||
| 			} | ||||
| @@ -608,12 +623,12 @@ int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t c | ||||
| { | ||||
| 	if(textIndex == 0) | ||||
| 		return 0; | ||||
| 	else if(textIndex < 0) | ||||
| 	if(textIndex < 0) | ||||
| 		return -textIndex; | ||||
| 	else if(count == 1) | ||||
| 	if(count == 1) | ||||
| 		return textIndex; | ||||
| 	else | ||||
| 		return textIndex + 1; | ||||
|  | ||||
| 	return textIndex + 1; | ||||
| } | ||||
|  | ||||
| void CGeneralTextHandler::dumpAllTexts() | ||||
| @@ -631,11 +646,12 @@ void CGeneralTextHandler::dumpAllTexts() | ||||
|  | ||||
| 	logGlobal->info("BEGIN TEXT EXPORT"); | ||||
| 	for ( auto const & entry : stringsLocalizations) | ||||
| 		if (stringsOverrides.count(entry.first) == 0) | ||||
| 			logGlobal->info("\"%s\" : \"%s\",", entry.first, escapeString(entry.second)); | ||||
|  | ||||
| 	for ( auto const & entry : stringsOverrides) | ||||
| 		logGlobal->info("\"%s\" : \"%s\",", entry.first, escapeString(entry.second)); | ||||
| 	{ | ||||
| 		if (!entry.second.overrideValue.empty()) | ||||
| 			logGlobal->info(R"("%s" : "%s", // %s / %s)", entry.first, escapeString(entry.second.overrideValue), entry.second.modContext, entry.second.overrideLanguage); | ||||
| 		else | ||||
| 			logGlobal->info(R"("%s" : "%s", // %s / %s)", entry.first, escapeString(entry.second.baseValue), entry.second.modContext, entry.second.baseLanguage); | ||||
| 	} | ||||
|  | ||||
| 	logGlobal->info("END TEXT EXPORT"); | ||||
| } | ||||
| @@ -649,11 +665,23 @@ size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| std::string CGeneralTextHandler::getInstalledLanguage() | ||||
| std::string CGeneralTextHandler::getModLanguage(const std::string & modContext) | ||||
| { | ||||
| 	if (modContext == "core") | ||||
| 		return getInstalledLanguage(); | ||||
| 	return VLC->modh->getModLanguage(modContext); | ||||
| } | ||||
|  | ||||
| std::string CGeneralTextHandler::getPreferredLanguage() | ||||
| { | ||||
| 	return settings["general"]["language"].String(); | ||||
| } | ||||
|  | ||||
| std::string CGeneralTextHandler::getInstalledLanguage() | ||||
| { | ||||
| 	return settings["session"]["language"].String(); | ||||
| } | ||||
|  | ||||
| std::string CGeneralTextHandler::getInstalledEncoding() | ||||
| { | ||||
| 	auto explicitSetting = settings["general"]["encoding"].String(); | ||||
|   | ||||
| @@ -146,30 +146,44 @@ public: | ||||
| /// Handles all text-related data in game | ||||
| class DLL_LINKAGE CGeneralTextHandler | ||||
| { | ||||
| 	/// map identifier -> localization | ||||
| 	std::unordered_map<std::string, std::string> stringsLocalizations; | ||||
| 	struct StringState | ||||
| 	{ | ||||
| 		/// Human-readable string that was added on registration | ||||
| 		std::string baseValue; | ||||
|  | ||||
| 	/// map identifier -> localization, high-priority strings from translation json | ||||
| 	std::unordered_map<std::string, std::string> stringsOverrides; | ||||
| 		/// Language of base string | ||||
| 		std::string baseLanguage; | ||||
|  | ||||
| 		/// Translated human-readable string | ||||
| 		std::string overrideValue; | ||||
|  | ||||
| 		/// Language of the override string | ||||
| 		std::string overrideLanguage; | ||||
|  | ||||
| 		/// ID of mod that created this string | ||||
| 		std::string modContext; | ||||
| 	}; | ||||
|  | ||||
| 	/// map identifier -> localization | ||||
| 	std::unordered_map<std::string, StringState> stringsLocalizations; | ||||
|  | ||||
| 	void readToVector(const std::string & sourceID, const std::string & sourceName); | ||||
|  | ||||
| 	/// number of scenarios in specific campaign. TODO: move to a better location | ||||
| 	std::vector<size_t> scenariosCountPerCampaign; | ||||
|  | ||||
| 	/// Attempts to detect encoding & language of H3 files | ||||
| 	void detectInstallParameters() const; | ||||
| 	std::string getModLanguage(const std::string & modContext); | ||||
| public: | ||||
|  | ||||
| 	/// Loads translation from provided json | ||||
| 	/// Any entries loaded by this will have priority over texts registered normally | ||||
| 	void loadTranslationOverrides(JsonNode const & file); | ||||
| 	void loadTranslationOverrides(const std::string & language, JsonNode const & file); | ||||
|  | ||||
| 	/// add selected string to internal storage | ||||
| 	void registerString(const TextIdentifier & UID, const std::string & localized); | ||||
| 	void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized); | ||||
|  | ||||
| 	/// add selected string to internal storage as high-priority strings | ||||
| 	void registerStringOverride(const TextIdentifier & UID, const std::string & localized); | ||||
| 	void registerStringOverride(const std::string & modContext, const std::string & language, const TextIdentifier & UID, const std::string & localized); | ||||
|  | ||||
| 	// returns true if identifier with such name was registered, even if not translated to current language | ||||
| 	// not required right now, can be added if necessary | ||||
| @@ -234,7 +248,13 @@ public: | ||||
| 	CGeneralTextHandler(const CGeneralTextHandler&) = delete; | ||||
| 	CGeneralTextHandler operator=(const CGeneralTextHandler&) = delete; | ||||
|  | ||||
| 	/// Attempts to detect encoding & language of H3 files | ||||
| 	static void detectInstallParameters(); | ||||
|  | ||||
| 	/// Returns name of language preferred by user | ||||
| 	static std::string getPreferredLanguage(); | ||||
|  | ||||
| 	/// Returns name of language of Heroes III text files | ||||
| 	static std::string getInstalledLanguage(); | ||||
|  | ||||
| 	/// Returns name of encoding of Heroes III text files | ||||
|   | ||||
| @@ -246,7 +246,7 @@ CHeroClass * CHeroClassHandler::loadFromJson(const std::string & scope, const Js | ||||
| 	heroClass->imageMapFemale    = node["animation"]["map"]["female"].String(); | ||||
| 	heroClass->imageMapMale      = node["animation"]["map"]["male"].String(); | ||||
|  | ||||
| 	VLC->generaltexth->registerString( heroClass->getNameTextID(), node["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, heroClass->getNameTextID(), node["name"].String()); | ||||
|  | ||||
| 	heroClass->affinity = vstd::find_pos(affinityStr, node["affinity"].String()); | ||||
|  | ||||
| @@ -421,11 +421,11 @@ CHero * CHeroHandler::loadFromJson(const std::string & scope, const JsonNode & n | ||||
| 	hero->sex = node["female"].Bool(); | ||||
| 	hero->special = node["special"].Bool(); | ||||
|  | ||||
| 	VLC->generaltexth->registerString( hero->getNameTextID(), node["texts"]["name"].String()); | ||||
| 	VLC->generaltexth->registerString( hero->getBiographyTextID(), node["texts"]["biography"].String()); | ||||
| 	VLC->generaltexth->registerString( hero->getSpecialtyNameTextID(), node["texts"]["specialty"]["name"].String()); | ||||
| 	VLC->generaltexth->registerString( hero->getSpecialtyTooltipTextID(), node["texts"]["specialty"]["tooltip"].String()); | ||||
| 	VLC->generaltexth->registerString( hero->getSpecialtyDescriptionTextID(), node["texts"]["specialty"]["description"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, hero->getNameTextID(), node["texts"]["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, hero->getBiographyTextID(), node["texts"]["biography"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, hero->getSpecialtyNameTextID(), node["texts"]["specialty"]["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, hero->getSpecialtyTooltipTextID(), node["texts"]["specialty"]["tooltip"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, hero->getSpecialtyDescriptionTextID(), node["texts"]["specialty"]["description"].String()); | ||||
|  | ||||
| 	hero->iconSpecSmall = node["images"]["specialtySmall"].String(); | ||||
| 	hero->iconSpecLarge = node["images"]["specialtyLarge"].String(); | ||||
|   | ||||
| @@ -638,6 +638,12 @@ CModInfo::CModInfo(std::string identifier,const JsonNode & local, const JsonNode | ||||
| 		vcmiCompatibleMin = Version::fromString(config["compatibility"]["min"].String()); | ||||
| 		vcmiCompatibleMax = Version::fromString(config["compatibility"]["max"].String()); | ||||
| 	} | ||||
|  | ||||
| 	if (!config["language"].isNull()) | ||||
| 		baseLanguage = config["language"].String(); | ||||
| 	else | ||||
| 		baseLanguage = "english"; | ||||
|  | ||||
| 	loadLocalData(local); | ||||
| } | ||||
|  | ||||
| @@ -1090,7 +1096,12 @@ void CModHandler::loadModFilesystems() | ||||
| 	} | ||||
| } | ||||
|  | ||||
| std::set<TModID> CModHandler::getModDependencies(TModID modId, bool & isModFound) | ||||
| std::string CModHandler::getModLanguage(TModID modId) const | ||||
| { | ||||
| 	return allMods.at(modId).baseLanguage; | ||||
| } | ||||
|  | ||||
| std::set<TModID> CModHandler::getModDependencies(TModID modId, bool & isModFound) const | ||||
| { | ||||
| 	auto it = allMods.find(modId); | ||||
| 	isModFound = (it != allMods.end()); | ||||
| @@ -1099,7 +1110,7 @@ std::set<TModID> CModHandler::getModDependencies(TModID modId, bool & isModFound | ||||
| 		return it->second.dependencies; | ||||
|  | ||||
| 	logMod->error("Mod not found: '%s'", modId); | ||||
| 	return std::set<TModID>(); | ||||
| 	return {}; | ||||
| } | ||||
|  | ||||
| void CModHandler::initializeConfig() | ||||
| @@ -1110,13 +1121,24 @@ void CModHandler::initializeConfig() | ||||
| void CModHandler::loadTranslation(TModID modName) | ||||
| { | ||||
| 	auto const & mod = allMods[modName]; | ||||
| 	std::string language = VLC->generaltexth->getInstalledLanguage(); | ||||
| 	std::string preferredLanguage = VLC->generaltexth->getPreferredLanguage(); | ||||
| 	std::string modBaseLanguage = allMods[modName].baseLanguage; | ||||
|  | ||||
| 	for (auto const & config : mod.config["translations"].Vector()) | ||||
| 		VLC->generaltexth->loadTranslationOverrides(JsonNode(ResourceID(config.String(), EResType::TEXT))); | ||||
| 	{ | ||||
| 		JsonNode json(ResourceID(config.String(), EResType::TEXT)); | ||||
| 		json.setMeta(modName); | ||||
|  | ||||
| 	for (auto const & config : mod.config[language]["translations"].Vector()) | ||||
| 		VLC->generaltexth->loadTranslationOverrides(JsonNode(ResourceID(config.String(), EResType::TEXT))); | ||||
| 		VLC->generaltexth->loadTranslationOverrides(modBaseLanguage, json); | ||||
| 	} | ||||
|  | ||||
| 	for (auto const & config : mod.config[preferredLanguage]["translations"].Vector()) | ||||
| 	{ | ||||
| 		JsonNode json(ResourceID(config.String(), EResType::TEXT)); | ||||
| 		json.setMeta(modName); | ||||
|  | ||||
| 		VLC->generaltexth->loadTranslationOverrides(preferredLanguage, json); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| void CModHandler::load() | ||||
|   | ||||
| @@ -219,6 +219,9 @@ public: | ||||
| 	 | ||||
| 	/// version of the mod | ||||
| 	Version version; | ||||
|  | ||||
| 	/// Base language of mod, all mod strings are assumed to be in this language | ||||
| 	std::string baseLanguage; | ||||
| 	 | ||||
| 	/// vcmi versions compatible with the mod | ||||
|  | ||||
| @@ -334,7 +337,9 @@ public: | ||||
| 	void loadMods(bool onlyEssential = false); | ||||
| 	void loadModFilesystems(); | ||||
|  | ||||
| 	std::set<TModID> getModDependencies(TModID modId, bool & isModFound); | ||||
| 	std::string getModLanguage(TModID modId) const; | ||||
|  | ||||
| 	std::set<TModID> getModDependencies(TModID modId, bool & isModFound) const; | ||||
|  | ||||
| 	/// returns list of all (active) mods | ||||
| 	std::vector<std::string> getAllMods(); | ||||
|   | ||||
| @@ -211,7 +211,7 @@ CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & | ||||
| 	CSkill * skill = new CSkill(SecondarySkill((si32)index), identifier); | ||||
| 	skill->modScope = scope; | ||||
|  | ||||
| 	VLC->generaltexth->registerString(skill->getNameTextID(), json["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, skill->getNameTextID(), json["name"].String()); | ||||
| 	switch(json["gainChance"].getType()) | ||||
| 	{ | ||||
| 	case JsonNode::JsonType::DATA_INTEGER: | ||||
| @@ -236,7 +236,7 @@ CSkill * CSkillHandler::loadFromJson(const std::string & scope, const JsonNode & | ||||
| 			skill->addNewBonus(bonus, level); | ||||
| 		} | ||||
| 		CSkill::LevelInfo & skillAtLevel = skill->at(level); | ||||
| 		VLC->generaltexth->registerString(skill->getDescriptionTextID(level), levelNode["description"].String()); | ||||
| 		VLC->generaltexth->registerString(scope, skill->getDescriptionTextID(level), levelNode["description"].String()); | ||||
| 		skillAtLevel.iconSmall = levelNode["images"]["small"].String(); | ||||
| 		skillAtLevel.iconMedium = levelNode["images"]["medium"].String(); | ||||
| 		skillAtLevel.iconLarge = levelNode["images"]["large"].String(); | ||||
|   | ||||
| @@ -604,8 +604,8 @@ void CTownHandler::loadBuilding(CTown * town, const std::string & stringID, cons | ||||
| 	ret->modScope = source.meta; | ||||
| 	ret->town = town; | ||||
|  | ||||
| 	VLC->generaltexth->registerString(ret->getNameTextID(), source["name"].String()); | ||||
| 	VLC->generaltexth->registerString(ret->getDescriptionTextID(), source["description"].String()); | ||||
| 	VLC->generaltexth->registerString(source.meta, ret->getNameTextID(), source["name"].String()); | ||||
| 	VLC->generaltexth->registerString(source.meta, ret->getDescriptionTextID(), source["description"].String()); | ||||
|  | ||||
| 	ret->resources = TResources(source["cost"]); | ||||
| 	ret->produce =   TResources(source["produce"]); | ||||
| @@ -890,7 +890,7 @@ void CTownHandler::loadTown(CTown * town, const JsonNode & source) | ||||
| 	town->namesCount = 0; | ||||
| 	for (auto const & name : source["names"].Vector()) | ||||
| 	{ | ||||
| 		VLC->generaltexth->registerString(town->getRandomNameTextID(town->namesCount), name.String()); | ||||
| 		VLC->generaltexth->registerString(town->faction->modScope, town->getRandomNameTextID(town->namesCount), name.String()); | ||||
| 		town->namesCount += 1; | ||||
| 	} | ||||
|  | ||||
| @@ -990,7 +990,7 @@ CFaction * CTownHandler::loadFromJson(const std::string & scope, const JsonNode | ||||
| 	faction->modScope = scope; | ||||
| 	faction->identifier = identifier; | ||||
|  | ||||
| 	VLC->generaltexth->registerString(faction->getNameTextID(), source["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, faction->getNameTextID(), source["name"].String()); | ||||
|  | ||||
| 	faction->creatureBg120 = source["creatureBackground"]["120px"].String(); | ||||
| 	faction->creatureBg130 = source["creatureBackground"]["130px"].String(); | ||||
|   | ||||
| @@ -40,7 +40,7 @@ RiverType * RiverTypeHandler::loadFromJson( | ||||
| 	info->shortIdentifier = json["shortIdentifier"].String(); | ||||
| 	info->deltaName       = json["delta"].String(); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(info->getNameTextID(), json["text"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, info->getNameTextID(), json["text"].String()); | ||||
|  | ||||
| 	return info; | ||||
| } | ||||
|   | ||||
| @@ -40,7 +40,7 @@ RoadType * RoadTypeHandler::loadFromJson( | ||||
| 	info->shortIdentifier = json["shortIdentifier"].String(); | ||||
| 	info->movementCost    = json["moveCost"].Integer(); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(info->getNameTextID(), json["text"].String()); | ||||
| 	VLC->generaltexth->registerString(scope,info->getNameTextID(), json["text"].String()); | ||||
|  | ||||
| 	return info; | ||||
| } | ||||
|   | ||||
| @@ -36,7 +36,7 @@ TerrainType * TerrainTypeHandler::loadFromJson( const std::string & scope, const | ||||
| 	info->transitionRequired = json["transitionRequired"].Bool(); | ||||
| 	info->terrainViewPatterns = json["terrainViewPatterns"].String(); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(info->getNameTextID(), json["text"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, info->getNameTextID(), json["text"].String()); | ||||
|  | ||||
| 	const JsonVector & unblockedVec = json["minimapUnblocked"].Vector(); | ||||
| 	info->minimapUnblocked = | ||||
|   | ||||
| @@ -233,7 +233,7 @@ ObjectClass * CObjectClassesHandler::loadFromJson(const std::string & scope, con | ||||
| 	obj->base = json["base"]; | ||||
| 	obj->id = index; | ||||
|  | ||||
| 	VLC->generaltexth->registerString(obj->getNameTextID(), json["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, obj->getNameTextID(), json["name"].String()); | ||||
|  | ||||
| 	obj->objects.resize(json["lastReservedIndex"].Float() + 1); | ||||
|  | ||||
|   | ||||
| @@ -141,7 +141,7 @@ void CDwellingInstanceConstructor::initTypeData(const JsonNode & input) | ||||
| 	if (input.Struct().count("name") == 0) | ||||
| 		logMod->warn("Dwelling %s missing name!", getJsonKey()); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(getNameTextID(), input["name"].String()); | ||||
| 	VLC->generaltexth->registerString( input.meta, getNameTextID(), input["name"].String()); | ||||
|  | ||||
| 	const JsonVector & levels = input["creatures"].Vector(); | ||||
| 	const auto totalLevels = levels.size(); | ||||
| @@ -267,7 +267,7 @@ void CBankInstanceConstructor::initTypeData(const JsonNode & input) | ||||
| 	if (input.Struct().count("name") == 0) | ||||
| 		logMod->warn("Bank %s missing name!", getJsonKey()); | ||||
|  | ||||
| 	VLC->generaltexth->registerString(getNameTextID(), input["name"].String()); | ||||
| 	VLC->generaltexth->registerString(input.meta, getNameTextID(), input["name"].String()); | ||||
|  | ||||
| 	levels = input["levels"].Vector(); | ||||
| 	bankResetDuration = static_cast<si32>(input["resetDuration"].Float()); | ||||
|   | ||||
| @@ -694,7 +694,7 @@ CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & | ||||
| 		spell->combat = type == "combat"; | ||||
| 	} | ||||
|  | ||||
| 	VLC->generaltexth->registerString(spell->getNameTextID(), json["name"].String()); | ||||
| 	VLC->generaltexth->registerString(scope, spell->getNameTextID(), json["name"].String()); | ||||
|  | ||||
| 	logMod->trace("%s: loading spell %s", __FUNCTION__, spell->getNameTranslated()); | ||||
|  | ||||
| @@ -909,7 +909,7 @@ CSpell * CSpellHandler::loadFromJson(const std::string & scope, const JsonNode & | ||||
| 		const si32 levelPower     = levelObject.power = static_cast<si32>(levelNode["power"].Integer()); | ||||
|  | ||||
| 		if (!spell->isCreatureAbility()) | ||||
| 			VLC->generaltexth->registerString(spell->getDescriptionTextID(levelIndex), levelNode["description"].String()); | ||||
| 			VLC->generaltexth->registerString(scope, spell->getDescriptionTextID(levelIndex), levelNode["description"].String()); | ||||
|  | ||||
| 		levelObject.cost          = static_cast<si32>(levelNode["cost"].Integer()); | ||||
| 		levelObject.AIValue       = static_cast<si32>(levelNode["aiValue"].Integer()); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user