Module:MyTranslationModule/sandbox
Appearance
![]() | This is the module sandbox page for Module:MyTranslationModule (diff). |
local p = {}
local rtl_langs = {
ar = true, he = true, fa = true, ur = true,
ps = true, prs = true, syr = true, ckb = true,
}
local function is_rtl(code)
if not code then return false end
code = mw.ustring.lower(mw.text.trim(code))
return rtl_langs[code] or false
end
local function build_cell(text, code)
if not text then text = "" end
if not code then return string.format("| %s", text) end
local dir = is_rtl(code) and "rtl" or "ltr"
return string.format('| style="direction:%s;" lang="%s" | %s', dir, code, text)
end
function p.render(frame)
local args = frame:getParent().args
local code1 = args["lang1code"]
local code2 = args["lang2code"]
-- Get names from lang1/lang2 or fall back to code conversion
local lang1 = args["lang1"]
local lang2 = args["lang2"]
if not lang1 and code1 then
lang1 = frame:expandTemplate{title = "ISO 639 name", args = {code1}}
end
if not lang2 and code2 then
lang2 = frame:expandTemplate{title = "ISO 639 name", args = {code2}}
end
if not lang1 or not lang2 then
return "Error: You must specify either language names or codes."
end
local output = {}
table.insert(output, '{| class="wikitable sortable"')
table.insert(output, string.format("! %s !! %s", lang1, lang2))
-- Named parameters: text1/trans1...
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, "|-")
table.insert(output, build_cell(text, code1))
table.insert(output, build_cell(trans, code2))
i = i + 1
end
-- Positional args: from 3 onwards
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, "|-")
table.insert(output, build_cell(text, code1))
table.insert(output, build_cell(trans, code2))
index = index + 2
end
table.insert(output, "|}")
return table.concat(output, "\n")
end
return p