Quellcode durchsuchen

Refactoring of GeneralTextHandler to reduce boilerplate code in callers

Ivan Savenko vor 2 Jahren
Ursprung
Commit
9a620a9091

+ 2 - 2
client/gui/InterfaceObjectConfigurable.cpp

@@ -180,8 +180,8 @@ std::pair<std::string, std::string> InterfaceObjectConfigurable::readHintText(co
 		if(config.getType() == JsonNode::JsonType::DATA_STRING)
 		{
 			logGlobal->debug("Reading hint text (help) from generaltext handler:%sd", config.String());
-			result.first  = CGI->generaltexth->translate( config.String() + ".hover");
-			result.second = CGI->generaltexth->translate( config.String() + ".help");
+			result.first  = CGI->generaltexth->translate( config.String(), "hover");
+			result.second = CGI->generaltexth->translate( config.String(), "help");
 		}
 	}
 	return result;

+ 3 - 3
client/widgets/Buttons.cpp

@@ -289,8 +289,8 @@ std::pair<std::string, std::string> CButton::tooltip()
 std::pair<std::string, std::string> CButton::tooltipLocalized(const std::string & key)
 {
 	return std::make_pair(
-		CGI->generaltexth->translate(key + ".hover"),
-		CGI->generaltexth->translate(key + ".help")
+		CGI->generaltexth->translate(key, "hover"),
+		CGI->generaltexth->translate(key, "help")
 	);
 }
 
@@ -523,7 +523,7 @@ void CVolumeSlider::clickRight(tribool down, bool previousState)
 		int index = static_cast<int>(px / static_cast<double>(pos.w) * animImage->size());
 
 		size_t helpIndex = index + (mode == MUSIC ? 326 : 336);
-		std::string helpBox = CGI->generaltexth->translate("core.help." + std::to_string(helpIndex) + ".help" );
+		std::string helpBox = CGI->generaltexth->translate("core.help", helpIndex, "help" );
 
 		if(!helpBox.empty())
 			CRClickPopup::createAndPush(helpBox);

+ 27 - 42
lib/CGeneralTextHandler.cpp

@@ -301,51 +301,36 @@ bool CLegacyConfigParser::endLine()
 	return curr < end;
 }
 
-void CGeneralTextHandler::readToVector(std::string sourceID, std::string sourceName)
+void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string const & sourceName)
 {
 	CLegacyConfigParser parser(sourceName);
 	size_t index = 0;
 	do
 	{
-		registerH3String(sourceID, index, parser.readString());
+		registerString({sourceID, index}, parser.readString());
 		index += 1;
 	}
 	while (parser.endLine());
 }
 
-const std::string & CGeneralTextHandler::translate(const std::string & identifier, size_t index) const
-{
-	return translate(identifier + std::to_string(index));
-}
-
-const std::string & CGeneralTextHandler::translate(const std::string & identifier) const
-{
-	return deserialize(identifier);
-}
-
 const std::string & CGeneralTextHandler::serialize(const std::string & identifier) const
 {
 	assert(stringsIdentifiers.count(identifier));
 	return stringsIdentifiers.at(identifier);
 }
 
-const std::string & CGeneralTextHandler::deserialize(const std::string & identifier) const
-{
-	if (stringsLocalizations.count(identifier))
-		return stringsLocalizations.at(identifier);
-	logGlobal->error("Unable to find localization for string '%s'", identifier);
-	return identifier;
-}
-
-void CGeneralTextHandler::registerH3String(const std::string & file, size_t index, const std::string & localized)
+const std::string & CGeneralTextHandler::deserialize(const TextIdentifier & identifier) const
 {
-	registerString(file + '.' + std::to_string(index), localized);
+	if (stringsLocalizations.count(identifier.get()))
+		return stringsLocalizations.at(identifier.get());
+	logGlobal->error("Unable to find localization for string '%s'", identifier.get());
+	return identifier.get();
 }
 
-void CGeneralTextHandler::registerString(const std::string & UID, const std::string & localized)
+void CGeneralTextHandler::registerString(const TextIdentifier & UID, const std::string & localized)
 {
-	stringsIdentifiers[localized] = UID;
-	stringsLocalizations[UID] = localized;
+	stringsIdentifiers[localized] = UID.get();
+	stringsLocalizations[UID.get()] = localized;
 }
 
 CGeneralTextHandler::CGeneralTextHandler():
@@ -424,7 +409,7 @@ CGeneralTextHandler::CGeneralTextHandler():
 			std::string line = parser.readString();
 			if (!line.empty())
 			{
-				registerH3String("core.randtvrn", index, line);
+				registerString({"core.randtvrn", index}, line);
 				index += 1;
 			}
 		}
@@ -436,7 +421,7 @@ CGeneralTextHandler::CGeneralTextHandler():
 		size_t index = 0;
 		do
 		{
-			registerH3String("core.genrltxt", index, parser.readString());
+			registerString({"core.genrltxt", index}, parser.readString());
 			index += 1;
 		}
 		while (parser.endLine());
@@ -461,9 +446,9 @@ CGeneralTextHandler::CGeneralTextHandler():
 		{
 			std::string color = parser.readString();
 
-			registerH3String("core.plcolors", index, color);
+			registerString({"core.plcolors", index}, color);
 			color[0] = toupper(color[0]);
-			registerH3String("vcmi.capitalColors", index, color);
+			registerString({"vcmi.capitalColors", index}, color);
 			index += 1;
 		}
 		while (parser.endLine());
@@ -474,41 +459,41 @@ CGeneralTextHandler::CGeneralTextHandler():
 		//skip header
 		parser.endLine();
 
-		for (int i = 0; i < 6; ++i)
+		for (size_t i = 0; i < 6; ++i)
 		{
-			registerH3String("core.seerhut.empty", i, parser.readString());
+			registerString({"core.seerhut.empty", i}, parser.readString());
 		}
 		parser.endLine();
 
-		for (int i = 0; i < 9; ++i) //9 types of quests
+		for (size_t i = 0; i < 9; ++i) //9 types of quests
 		{
 			std::string questName = CQuest::missionName(CQuest::Emission(1+i));
 
-			for (int j = 0; j < 5; ++j)
+			for (size_t j = 0; j < 5; ++j)
 			{
 				std::string questState = CQuest::missionState(j);
 
 				parser.readString(); //front description
-				for (int k = 0; k < 6; ++k)
+				for (size_t k = 0; k < 6; ++k)
 				{
-					registerH3String("core.seerhut.quest." + questName + "." + questState, k, parser.readString());
+					registerString({"core.seerhut.quest", questName, questState, k}, parser.readString());
 				}
 				parser.endLine();
 			}
 		}
 
-		for (int k = 0; k < 6; ++k) //Time limit
+		for (size_t k = 0; k < 6; ++k) //Time limit
 		{
-			registerH3String("core.seerhut.time", k, parser.readString());
+			registerString({"core.seerhut.time", k}, parser.readString());
 		}
 		parser.endLine();
 
 		parser.endLine(); // empty line
 		parser.endLine(); // header
 
-		for (int i = 0; i < 48; ++i)
+		for (size_t i = 0; i < 48; ++i)
 		{
-			registerH3String("core.seerhut.names", i, parser.readString());
+			registerString({"core.seerhut.names", i}, parser.readString());
 			parser.endLine();
 		}
 	}
@@ -525,7 +510,7 @@ CGeneralTextHandler::CGeneralTextHandler():
 			text = parser.readString();
 			if (!text.empty())
 			{
-				registerH3String("core.camptext.names", campaignsCount, text);
+				registerString({"core.camptext.names", campaignsCount}, text);
 				campaignsCount += 1;
 			}
 		}
@@ -546,7 +531,7 @@ CGeneralTextHandler::CGeneralTextHandler():
 				text = parser.readString();
 				if (!text.empty())
 				{
-					registerH3String("core.camptext.regions." + std::to_string(campaign), region, text);
+					registerString({"core.camptext.regions", std::to_string(campaign), region}, text);
 					region += 1;
 				}
 			}
@@ -620,7 +605,7 @@ LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::strin
 
 const std::string & LegacyTextContainer::operator[](size_t index) const
 {
-	return owner.translate(basePath + "." + std::to_string(index));
+	return owner.translate(basePath, index);
 }
 
 LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):

+ 40 - 11
lib/CGeneralTextHandler.h

@@ -115,6 +115,34 @@ public:
 	std::pair<std::string, std::string> operator[](size_t index) const;
 };
 
+class TextIdentifier
+{
+	std::string identifier;
+public:
+	std::string const & get() const
+	{
+		return identifier;
+	}
+
+	TextIdentifier(const char * id):
+		identifier(id)
+	{}
+
+	TextIdentifier(std::string const & id):
+		identifier(id)
+	{}
+
+	template<typename ... T>
+	TextIdentifier(std::string const & id, size_t index, T ... rest):
+		TextIdentifier(id + '.' + std::to_string(index), rest ... )
+	{}
+
+	template<typename ... T>
+	TextIdentifier(std::string const & id, std::string const & id2, T ... rest):
+		TextIdentifier(id + '.' + id2, rest ... )
+	{}
+};
+
 /// Handles all text-related data in game
 class DLL_LINKAGE CGeneralTextHandler
 {
@@ -124,30 +152,31 @@ class DLL_LINKAGE CGeneralTextHandler
 	/// map localization -> identifier
 	std::unordered_map<std::string, std::string> stringsIdentifiers;
 
-	/// add selected string to internal storage
-	void registerString(const std::string & UID, const std::string & localized);
-	void registerH3String(const std::string & file, size_t index, const std::string & localized);
-
-	void readToVector(std::string sourceID, std::string sourceName);
+	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;
 public:
+	/// add selected string to internal storage
+	void registerString(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
 	// bool identifierExists( const std::string identifier) const;
 
 	/// returns translated version of a string that can be displayed to user
-	const std::string & translate(const std::string & identifier) const;
-
-	/// returns translated version of a string that can be displayed to user, H3-array compatibility version
-	const std::string & translate(const std::string & identifier, size_t index) const;
+	template<typename  ... Args>
+	const std::string & translate(std::string arg1, Args ... args) const
+	{
+		TextIdentifier id(arg1, args ...);
+		return deserialize(id);
+	}
 
 	/// converts translated string into locale-independent text that can be sent to another client
 	const std::string & serialize(const std::string & identifier) const;
 
-	/// converts identifier into user-readable string, may be identical to 'translate' but reserved for serialization calls
-	const std::string & deserialize(const std::string & identifier) const;
+	/// converts identifier into user-readable string
+	const std::string & deserialize(const TextIdentifier & identifier) const;
 
 	/// Debug method, dumps all currently known texts into console using Json-like format
 	void dumpAllTexts();

+ 1 - 1
lib/mapObjects/CQuest.cpp

@@ -303,7 +303,7 @@ void CQuest::getRolloverText(MetaString &ms, bool onHover) const
 	std::string questName  = missionName(Emission(missionType-1));
 	std::string questState = missionState(onHover ? 3 : 4);
 
-	ms << VLC->generaltexth->translate("core.seerhut.quest." + questName + "." + questState,textOption);
+	ms << VLC->generaltexth->translate("core.seerhut.quest", questName, questState,textOption);
 
 	switch(missionType)
 	{