Add new admin command /create_locale
This commit is contained in:
@@ -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 = '<short_mod_name> <lang_code>',
|
||||
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
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user