diff --git a/lib/mapping/MapFormatJson.cpp b/lib/mapping/MapFormatJson.cpp index 5ab9f6297..beb31cf7a 100644 --- a/lib/mapping/MapFormatJson.cpp +++ b/lib/mapping/MapFormatJson.cpp @@ -1275,6 +1275,7 @@ void CMapLoaderJson::readTranslations() if(isExistArchive(language.identifier + ".json")) mapHeader->translations.Struct()[language.identifier] = getFromArchive(language.identifier + ".json"); } + mapHeader->registerMapStrings(); } diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index 5a18d1fc3..f4048c8e3 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -400,6 +400,8 @@ void MainWindow::saveMap() else QMessageBox::information(this, "Map validation", "Map has some errors. Open Validator from the Map menu to see issues found"); } + + Translations::cleanupRemovedItems(*controller.map()); CMapService mapService; try diff --git a/mapeditor/mapsettings/translations.cpp b/mapeditor/mapsettings/translations.cpp index 0b77b4cfc..fd2ae9130 100644 --- a/mapeditor/mapsettings/translations.cpp +++ b/mapeditor/mapsettings/translations.cpp @@ -15,6 +15,44 @@ #include "../../lib/CGeneralTextHandler.h" #include "../../lib/VCMI_Lib.h" +void Translations::cleanupRemovedItems(CMap & map) +{ + std::set existingObjects; + for(auto object : map.objects) + existingObjects.insert(object->instanceName); + + for(auto & translations : map.translations.Struct()) + { + auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT); + for(auto & s : translations.second.Struct()) + { + for(auto part : QString::fromStdString(s.first).split('.')) + { + if(existingObjects.count(part.toStdString())) + { + updateTranslations.Struct()[s.first] = s.second; + break; + } + } + } + translations.second = updateTranslations; + } +} + +void Translations::cleanupRemovedItems(CMap & map, const std::string & pattern) +{ + for(auto & translations : map.translations.Struct()) + { + auto updateTranslations = JsonNode(JsonNode::JsonType::DATA_STRUCT); + for(auto & s : translations.second.Struct()) + { + if(s.first.find(pattern) == std::string::npos) + updateTranslations.Struct()[s.first] = s.second; + } + translations.second = updateTranslations; + } +} + Translations::Translations(CMapHeader & mh, QWidget *parent) : QDialog(parent), ui(new Ui::Translations), @@ -41,6 +79,7 @@ Translations::~Translations() void Translations::fillTranslationsTable(const std::string & language) { + Translations::cleanupRemovedItems(dynamic_cast(mapHeader)); auto & translation = mapHeader.translations[language]; ui->translationsTable->blockSignals(true); ui->translationsTable->setRowCount(0); diff --git a/mapeditor/mapsettings/translations.h b/mapeditor/mapsettings/translations.h index 27b9c8218..af63600c8 100644 --- a/mapeditor/mapsettings/translations.h +++ b/mapeditor/mapsettings/translations.h @@ -11,7 +11,7 @@ #pragma once #include -#include "../lib/mapping/CMapHeader.h" +#include "../lib/mapping/CMap.h" namespace Ui { class Translations; @@ -26,6 +26,10 @@ class Translations : public QDialog public: explicit Translations(CMapHeader & mapHeader, QWidget *parent = nullptr); ~Translations(); + + //removes unused string IDs from map translations + static void cleanupRemovedItems(CMap & map); + static void cleanupRemovedItems(CMap & map, const std::string & pattern); private slots: void on_languageSelect_currentIndexChanged(int index);