Jump to content

Module:MyTranslationModule/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gommeh (talk | contribs) at 15:29, 11 May 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

function p.render(frame)
    local args = frame:getParent().args

    -- Support lang1/lang2 or positional args 1/2
    local lang1 = args["lang1"] or args[1]
    local lang2 = args["lang2"] or args[2]

    if not lang1 or not lang2 then
        return "Error: You must specify two language names."
    end

    local output = {}
    table.insert(output, '{| class="wikitable"')
    table.insert(output, string.format("! %s !! %s", lang1, lang2))

    -- First, process named args like text1/trans1, text2/trans2
    local i = 1
    while true do
        local text = args["text" .. i]
        local trans = args["trans" .. i]
        if not text and not trans then break end
        table.insert(output, string.format("|-\n| %s || %s", text or "", trans or ""))
        i = i + 1
    end

    -- Then add any *remaining* unnamed args starting from 3 onward
    for j = 3, #args, 2 do
        local text = args[j]
        local trans = args[j + 1]
        if text or trans then
            table.insert(output, string.format("|-\n| %s || %s", text or "", trans or ""))
        end
    end

    table.insert(output, "|}")
    return table.concat(output, "\n")
end

return p