Browse Source

Names for object supertypes are passed through translator

Ivan Savenko 2 years ago
parent
commit
c5f72ccdc7

+ 1 - 0
lib/CGeneralTextHandler.h

@@ -160,6 +160,7 @@ class DLL_LINKAGE CGeneralTextHandler
 	/// Attempts to detect encoding & language of H3 files
 	void detectInstallParameters() const;
 public:
+
 	/// Loads translation from provided json
 	/// Any entries loaded by this will have priority over texts registered normally
 	void loadTranslationOverrides(JsonNode const & file);

+ 23 - 8
lib/mapObjects/CObjectClassesHandler.cpp

@@ -177,7 +177,7 @@ void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, cons
 
 	auto handler = handlerConstructors.at(obj->handlerName)();
 	handler->setType(obj->id, id);
-	handler->setTypeName(obj->identifier, convertedId);
+	handler->setTypeName(obj->getIdentifier(), convertedId);
 
 	if (customNames.count(obj->id) && customNames.at(obj->id).size() > id)
 		handler->init(entry, customNames.at(obj->id).at(id));
@@ -194,7 +194,7 @@ void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, cons
 		legacyTemplates.erase(range.first, range.second);
 	}
 
-	logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->identifier, obj->id, convertedId, id);
+	logGlobal->debug("Loaded object %s(%d)::%s(%d)", obj->getIdentifier(), obj->id, convertedId, id);
 
 	//some mods redefine content handlers in the decoration.json in such way:
 	//"core:sign" : { "types" : { "forgeSign" : { ...
@@ -219,22 +219,37 @@ void CObjectClassesHandler::loadObjectEntry(const std::string & identifier, cons
 	else if(isExistingKey) //It's supposed that fan mods handlers are not overridden by default handlers
 	{
 		logGlobal->trace("Handler '%s' has not been overridden with handler '%s' in object %s(%d)::%s(%d)",
-			obj->subObjects[id]->subTypeName, obj->handlerName, obj->identifier, obj->id, convertedId, id);
+			obj->subObjects[id]->subTypeName, obj->handlerName, obj->getIdentifier(), obj->id, convertedId, id);
 	}
 	else
 	{
 		logGlobal->warn("Handler '%s' for object %s(%d)::%s(%d) has not been activated as RMG breaker",
-			obj->handlerName, obj->identifier, obj->id, convertedId, id);
+			obj->handlerName, obj->getIdentifier(), obj->id, convertedId, id);
 	}
 }
 
+std::string CObjectClassesHandler::ObjectContainter::getIdentifier() const
+{
+	return modScope + ":" + identifier;
+}
+
+std::string CObjectClassesHandler::ObjectContainter::getNameTextID() const
+{
+	return TextIdentifier ("object", modScope, identifier, "name").get();
+}
+
+std::string CObjectClassesHandler::ObjectContainter::getNameTranslated() const
+{
+	return VLC->generaltexth->translate(getNameTextID());
+}
+
 CObjectClassesHandler::ObjectContainter * CObjectClassesHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & name)
 {
-	auto obj = new ObjectContainter();
+	auto obj = new ObjectContainter(scope, name);
 	static const si32 fixedObjectsBound = 256; //Legacy value for backward compatibility
 
-	obj->identifier = name;
-	obj->name = json["name"].String();
+	VLC->generaltexth->registerString( obj->getNameTextID(), json["name"].String());
+
 	obj->handlerName = json["handler"].String();
 	obj->base = json["base"];
 	obj->id = selectNextID(json["index"], objects, fixedObjectsBound);
@@ -387,7 +402,7 @@ void CObjectClassesHandler::afterLoadFinalization()
 std::string CObjectClassesHandler::getObjectName(si32 type) const
 {
 	if (objects.count(type))
-		return objects.at(type)->name;
+		return  objects.at(type)->getNameTranslated();
 	logGlobal->error("Access to non existing object of type %d", type);
 	return "";
 }

+ 16 - 4
lib/mapObjects/CObjectClassesHandler.h

@@ -234,11 +234,19 @@ class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
 {
 	/// Small internal structure that contains information on specific group of objects
 	/// (creating separate entity is overcomplicating at least at this point)
-	struct ObjectContainter
+	class DLL_LINKAGE ObjectContainter
 	{
-		si32 id;
 		std::string identifier;
-		std::string name; // human-readable name
+		std::string modScope;
+
+	public:
+		ObjectContainter() = default;
+		ObjectContainter(const std::string & modScope, const std::string & identifier):
+			identifier(identifier),
+			modScope(modScope)
+		{}
+
+		si32 id;
 		std::string handlerName; // ID of handler that controls this object, should be determined using handlerConstructor map
 
 		JsonNode base;
@@ -249,13 +257,17 @@ class DLL_LINKAGE CObjectClassesHandler : public IHandlerBase
 
 		boost::optional<si32> groupDefaultAiValue;
 
+		std::string getIdentifier() const;
+		std::string getNameTextID() const; // {scope, "objects", name, "name"}
+		std::string getNameTranslated() const;
+
 		template <typename Handler> void serialize(Handler &h, const int version)
 		{
-			h & name;
 			h & handlerName;
 			h & base;
 			h & subObjects;
 			h & identifier;
+			h & modScope;
 			h & subIds;
 			h & sounds;
 			h & groupDefaultAiValue;