diff --git a/mods/CORE/Item/src/Item.lua b/mods/CORE/Item/src/Item.lua index c52a26f..5ee8af6 100644 --- a/mods/CORE/Item/src/Item.lua +++ b/mods/CORE/Item/src/Item.lua @@ -31,12 +31,6 @@ local RARITY_INDEX = { ['unique'] = 0 } --- Keys indicating the languages ​​for which localization presence is checked. -local LANG_CODES = {'en', 'ru'} - --- Number of missed localizations. -local missedLocaleCount = 0 - ---@class Item ---@field itemDef Item.ItemDefinition ---@field luantiDef ItemDefinition @@ -85,23 +79,12 @@ local defMerge = function(...) return tableMerge(...) end -local function checkLocale(str, strTranslate) - local modName = Mod.getInfo().name - for _, langCode in ipairs(LANG_CODES) do - if core.get_translated_string(langCode, strTranslate) == str then - missedLocaleCount = missedLocaleCount + 1 - Logger.warningLog('Localization missing[%s][%s]. String: `%s`. Mod: `%s`', missedLocaleCount, langCode, str, modName) - end - end -end - ---@param itemDef Item.ItemDefinition ---@return NodeDefinition|ItemDefinition local function itemDefToLuantiDef(itemDef) local modName = Mod.getInfo().shortName local S = core.get_translator(modName) - itemDef = tableCopy(itemDef) local s = itemDef.settings local c = itemDef.callbacks @@ -117,9 +100,6 @@ local function itemDefToLuantiDef(itemDef) local titleTranslate, descriptionTranslate = S(title), S(description) - checkLocale(title, titleTranslate) - checkLocale(description, descriptionTranslate) - if s._title_color ~= nil and s._description_color ~= nil then titleTranslate = colorize(s._title_color , titleTranslate) descriptionTranslate = colorize(s._description_color, descriptionTranslate) @@ -178,12 +158,16 @@ function Item:new(itemDef) 'Please note that the `description` field should not be used when registering an item. '.. 'Use `_title` and `_description` instead.') + itemDef = tableCopy(itemDef) + -- Adding default parameters itemDef = defMerge(itemDef, self.defaultDef, true) local s = itemDef.settings + local shortModName = Mod.getInfo().shortName + if s._add_mod_name then - s.name = Mod.getInfo().shortName .. ':' .. s.name + s.name = shortModName .. ':' .. s.name end ---@type Item @@ -215,6 +199,7 @@ function Item:new(itemDef) instance.itemDef = itemDef instance.luantiDef = luantiDef + Smc.addRegisteredItem(shortModName, instance) return instance end diff --git a/mods/GAME/adminCommands/init.lua b/mods/GAME/adminCommands/init.lua index df38b54..9ab7358 100644 --- a/mods/GAME/adminCommands/init.lua +++ b/mods/GAME/adminCommands/init.lua @@ -66,3 +66,66 @@ core.register_on_joinplayer(function(player, last_login) saturation = 1.0, }) end) + +-- Keys indicating the languages ​​for which localization presence is checked. +local LANG_CODES = {'en', 'ru'} + +-- Number of missed localizations. +local missedLocaleCount = 0 + +local function checkLocale(strTranslate, langCodes) + local str = core.strip_escapes(strTranslate) + + for _, langCode in ipairs(langCodes or LANG_CODES) do + if core.strip_escapes(core.get_translated_string(langCode, strTranslate)) == str then + missedLocaleCount = missedLocaleCount + 1 + Logger.warningLog('Localization missing[%s][%s]. String: `%s`.', missedLocaleCount, langCode, str) + end + end +end + +core.register_chatcommand('get_missing_locale', { + privs = {server = true}, + description = '{short_mod_name:string?, lang_codes:string[]?}', + func = function(name, paramsStr) + local params = core.deserialize('return '..paramsStr or '{}') + + if params == nil then + return false + end + + local shortModName = params.short_mod_name + local langCodes = params.lang_codes + + if shortModName ~= nil and type(shortModName) ~= 'string' then + return false + end + + if langCodes ~= nil and type(langCodes) ~= 'table' then + return false + end + + local items + if shortModName == '' or shortModName == nil then + items = Smc.getRegisteredItems() + else + items = Smc.getRegisteredItemsByMod(shortModName) + end + + if items == nil then + return false, 'There are no items registered for this mod (or the entire game)' + end + + ---@param name string + ---@param item Item + for name, item in pairs(items) do + local itemSettings = item.itemDef.settings + local title, desc = itemSettings._title, itemSettings._description + + checkLocale(title, langCodes) + checkLocale(desc, langCodes) + end + + return true, '' + end +}) diff --git a/mods/SYSTEM/HelperLib/src/Lib.lua b/mods/SYSTEM/HelperLib/src/Lib.lua index dd32ba8..b2414a6 100644 --- a/mods/SYSTEM/HelperLib/src/Lib.lua +++ b/mods/SYSTEM/HelperLib/src/Lib.lua @@ -8,4 +8,5 @@ require('Lib.color') require('Lib.immersiveCraftData') require('Lib.getObjectByGUID') require('Lib.receiveFields') +require('Lib.registeredItems') require('Lib.requires') \ No newline at end of file diff --git a/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua b/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua new file mode 100644 index 0000000..fead3cd --- /dev/null +++ b/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua @@ -0,0 +1,33 @@ +---@type table +local registeredItems = {} + +---@type table> +local registeredItemsByMod = {} + +---@param shortModName string +---@param item Item +function Smc.addRegisteredItem(shortModName, item) + local itemName = item.itemDef.settings.name + local modItems = registeredItemsByMod[shortModName] + + if modItems == nil then + registeredItemsByMod[shortModName] = {[itemName]=item} + return + end + + assert(modItems[itemName] == nil) + + registeredItems[itemName] = item + modItems[itemName] = item +end + +---@param shortModName string +---@return table? +function Smc.getRegisteredItemsByMod(shortModName) + return table.copy_with_metatables(registeredItemsByMod[shortModName]) +end + +---@return table? +function Smc.getRegisteredItems() + return table.copy_with_metatables(registeredItems) +end