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:42, 11 May 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

-- List of RTL language codes
local rtl_langs = {
    ar = true, he = true, fa = true, ur = true,
    ps = true, prs = true, syr = true, ckb = true,
}

-- Detect RTL by lang code (e.g. "ar", "he", etc.)
local function is_rtl(code)
    if not code then return false end
    code = code:lower():gsub("^%s*(.-)%s*$", "%1")
    return rtl_langs[code] or false
end

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

    -- Language names and codes
    local lang1 = args["lang1"] or args[1]
    local lang2 = args["lang2"] or args[2]
    local code1 = args["lang1code"]
    local code2 = args["lang2code"]

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

    -- Determine directionality
    local dir1 = is_rtl(code1) and ' dir="rtl"' or ' dir="ltr"'
    local dir2 = is_rtl(code2) and ' dir="rtl"' or ' dir="ltr"'

    -- Add lang attribute if code is present
    local langAttr1 = code1 and (' lang="' .. code1 .. '"') or ""
    local langAttr2 = code2 and (' lang="' .. code2 .. '"') or ""

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

    -- Named parameters: text1/trans1 etc.
    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 %s ||%s%s %s',
            dir1, langAttr1, text or "", dir2, langAttr2, trans or ""))
        i = i + 1
    end

    -- Positional parameters: 3,4,5,...
    local index = 3
    while true do
        local text = args[index]
        local trans = args[index + 1]
        if not text and not trans then break end
        table.insert(output, string.format('|-\n|%s%s %s ||%s%s %s',
            dir1, langAttr1, text or "", dir2, langAttr2, trans or ""))
        index = index + 2
    end

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

return p