Module:Po

来自缺氧 Wiki
跳转到导航 跳转到搜索

可在Module:Po/doc创建此模块的帮助文档

-- Module:Po
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local getArgs = require('Dev:Arguments').getArgs
local i18n = require([[Module:I18n]])

function p.po(args)
    local key = args
    if type(args) == 'table' then key = args[1]:upper() end
    local subModule = mw.text.split(key, '.', true)[2]
    local msgModule = 'Module:i18n/' ..
                          subModule:lower():gsub("^%l", string.upper)
                              :gsub("_", "")
    local __ = i18n.loadMessages(msgModule)
    local msg = __:msg(key)
    if type(args) == 'table' and args.format ~= nil then
        for k, v in pairs(args) do
            if type(k) == "string" and k ~= 'format' then
            	if (k:sub(1,1) == '$') then k = k:sub(2) end
                msg = mw.ustring.gsub(msg, fstr("{%s}", k), v)
            end
        end
    end
    return msg
end

-- test by: = p.po("STRINGS.DUPLICANTS.TRAITS.CANTRESEARCH.NAME")
-- test by: = p.po({"STRINGS.DUPLICANTS.trAITS.CANTRESEARCH.NAME"})
-- test by: = p.po({"STRINGS.UI.BUILDCATEGORIES.EQUIPMENT.TOOLTIP"})
-- test by: = p.po({"STRINGS.UI.BUILDCATEGORIES.EQUIPMENT.TOOLTIP", format=1, Hotkey='[E]'})
-- test by: = p.po({"STRINGS.COLONY_ACHIEVEMENTS.MISC_REQUIREMENTS.BUILD_NATURE_RESERVES_DESCRIPTION", format=1, ['$0']='000', ['$1']='111'})
function p.main(frame) return p.po(getArgs(frame)) end

return p