diff --git a/mods/GAME/adminCommands/init.lua b/mods/GAME/adminCommands/init.lua index eb105d6..92d2374 100644 --- a/mods/GAME/adminCommands/init.lua +++ b/mods/GAME/adminCommands/init.lua @@ -99,11 +99,11 @@ core.register_chatcommand('get_missing_locale', { local langCodes = params.lang_codes if shortModName ~= nil and type(shortModName) ~= 'string' then - return false + return false, 'Incorrect mod name' end if langCodes ~= nil and type(langCodes) ~= 'table' then - return false + return false, 'Incorrect lang code' end local items @@ -130,3 +130,60 @@ core.register_chatcommand('get_missing_locale', { return true, '' end }) + +core.register_chatcommand('create_locale', { + privs = { server = true }, + description = ' ', + func = function(name, params) + local shortModName, langCode = string.match(params, '(%S+) (%S+)') + + if shortModName == nil or shortModName == '' then + return false, 'Incorrect mod name!' + end + + if langCode == nil or langCode == '' or not table.contains(LANG_CODES, langCode)then + return false, 'Incorrect lang code!' + end + + local items = Smc.getRegisteredItemsListByMod(shortModName) + + if items == nil then + return false, 'There are no items registered for this mod (or the entire game)' + end + + local filePath = core.get_worldpath()..('/%s.%s.tr'):format(shortModName, langCode) + local file, err = io.open(filePath, 'w') + + if not file then + return false, 'Cannot open file: '..err + end + + file:write('# textdomain: '..shortModName..'\n\n') + + for _, item in ipairs(items) do + local itemSettings = item.itemDef.settings + local name = string.match(itemSettings.name, ('%S+:(%S+)')) + local title = core.strip_colors(core.get_translated_string(langCode, itemSettings._title)) + local desc = core.strip_colors(core.get_translated_string(langCode, itemSettings._description)) + + if title ~= name..'-title' then + title = '[locale already exists]' + else + title = '' + end + + if desc ~= name..'-desc' then + desc = '[locale already exists]' + else + desc = '' + end + + file:write(('%s-title=%s\n%s-desc=%s\n'):format(name, title, name, desc)) + end + + file:write('\n') + file:close() + + return true, 'File created: '..filePath + end +}) diff --git a/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua b/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua index dd262e9..6257bda 100644 --- a/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua +++ b/mods/SYSTEM/HelperLib/src/Lib/registeredItems.lua @@ -1,23 +1,30 @@ +local tableInsert = table.insert + ---@type table local registeredItems = {} ---@type table> local registeredItemsByMod = {} +---@type table +local registeredItemsListByMod = {} ---@param shortModName string ---@param item Item|Node function Smc.addRegisteredItem(shortModName, item) local itemName = item.itemDef.settings.name - local modItems = registeredItemsByMod[shortModName] - if modItems == nil then - registeredItemsByMod[shortModName] = {[itemName]=item} - registeredItems[itemName] = item - return + if registeredItemsByMod[shortModName] == nil then + registeredItemsByMod[shortModName] = {} end + if registeredItemsListByMod[shortModName] == nil then + registeredItemsListByMod[shortModName] = {} + end + + registeredItems[itemName] = item - modItems[itemName] = item + registeredItemsByMod[shortModName] = item + tableInsert(registeredItemsListByMod[shortModName], item) end ---@param shortModName string @@ -26,6 +33,12 @@ function Smc.getRegisteredItemsByMod(shortModName) return table.copy_with_metatables(registeredItemsByMod[shortModName]) end +---@param shortModName string +---@return table? +function Smc.getRegisteredItemsListByMod(shortModName) + return table.copy_with_metatables(registeredItemsListByMod[shortModName]) +end + ---@param itemName ItemStackString ---@return Item|Node function Smc.getRegisteredItemByName(itemName)