Module:MyTranslationModule/sandbox
Appearance
![]() | This is the module sandbox page for Module:MyTranslationModule (diff). |
local p = {}
-- Languages written right-to-left (used if you want Langx in future)
local rtl_langs = {
ar = true, he = true, fa = true, ur = true,
ps = true, prs = true, syr = true, ckb = true,
}
-- Determine if language code is RTL
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
-- Build one cell using {{Lang|code|text}}
local function lang_cell(code, text)
code = code or ""
text = text or ""
if code ~= "" then
return "| " .. frame:preprocess("{{Lang|" .. code .. "|" .. text .. "}}")
else
return "| " .. text
end
end
function p.render(frame)
local args = frame:getParent().args
-- Language codes are required
local code1 = args["lang1code"]
local code2 = args["lang2code"]
if not code1 or not code2 then
return "Error: Please provide both lang1code and lang2code."
end
-- Get human-readable names using {{ISO 639 name|code}}
local lang1 = frame:expandTemplate{ title = "ISO 639 name", args = { code1 } }
local lang2 = frame:expandTemplate{ title = "ISO 639 name", args = { code2 } }
local output = {}
table.insert(output, '{| class="wikitable sortable"')
table.insert(output, string.format("! %s !! %s", lang1, lang2))
-- Named pairs first
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, lang_cell(frame, code1, text))
table.insert(output, lang_cell(frame, code2, trans))
i = i + 1
end
-- Then positional fallback (start at param 1 if lang1/lang2 are omitted)
local index = 1
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, lang_cell(frame, code1, text))
table.insert(output, lang_cell(frame, code2, trans))
index = index + 2
end
table.insert(output, "|}")
return table.concat(output, "\n")
end
return p