Module:Autolang

From Meta, a Wikimedia project coordination wiki
Module documentation
local p = {}
---------------------------------------------------------
----------------- [[Template:AutolangSection]] -----------------
---------------------------------------------------------
--[=[
  Works like {{autotranslate}} for section transclusion (see [[:mw:Extension:Labeled Section Transclusion]])
  just allowing an unlimited number of arguments, even named arguments.
  It's doing Magic! No arguments should be passed to {{#invoke:}}
]=]
 
function p.section(frame)
    local args, pargs = frame.args, ( frame:getParent() or {} ).args or {}
    if not args.base then args = pargs end
    local base = args.base

    -- no longer working: mw.newRawMessage() has been deactivated:
    -- local userlang = mw.message.newRawMessage( '{{int:lang}}' ):parse()
    local userlang = frame:expandTemplate{title = 'Template:uselang'} -- workaround

    local tlb, fallback1, currenttemplate
    local fallback, contentlang = mw.text.split( userlang, '-', true )[1], mw.language.getContentLanguage():getCode()
 
    local createReturn = function(title) 
        local ret
        local tlargs = {}
        ret = '{{#lst:' .. title .. '|' .. args.section .. table.concat(tlargs) .. '}}'
        return frame:preprocess(ret)
    end
 
    if not base then
        return ("'section' in [[Module:Autolang]] was called but the 'base' parameter could not be found." .. 
            "The base parameter specifies the template that's subpages will be sought for a suitable translation.")
    end
    tlb = base .. '/'
 
    currenttemplate = tlb .. userlang
    if mw.title.new( currenttemplate ).exists then
        return createReturn(currenttemplate)
    end
 
    currenttemplate = tlb .. fallback
    if mw.title.new( currenttemplate ).exists then
        return createReturn(currenttemplate)
    end
 
    currenttemplate = tlb .. contentlang
    if mw.title.new( currenttemplate ).exists then
        return createReturn(currenttemplate)
    end
    return createReturn(tl .. base)
end


return p