Jump to content

Module:TableTranslation

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Gommeh (talk | contribs) at 18:59, 18 June 2025. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

-- Optional RTL detection for future Langx/dir support
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

-- Lang cell using {{Lang}} to support italics and formatting
local function lang_cell(frame, code, rawtext)
	code = code or ""
	rawtext = rawtext or ""
	if code ~= "" then
		return "| " .. frame:preprocess(string.format("{{Langx|%s|%s|label=none|italic=unset}}", code, rawtext))
	else
		return "| " .. frame:preprocess(rawtext)
	end
end

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

	-- Detect substitution and render static content
	local isSubst = frame:getParent():getTitle() == frame:getTitle() or mw.isSubsting()
	
	-- Resolve language codes from various aliases
	local code1 = args["lang1code"] or args["lang1"] or args[1]
	local code2 = args["lang2code"] or args["lang2"] or args[2]

	if not code1 or not code2 then
		return "Error: Please provide both lang1code and lang2code."
	end

	-- Expand display names from ISO 639 codes
	local lang1 = frame:expandTemplate{ title = "ISO 639 name", args = { code1 } }
	local lang2 = frame:expandTemplate{ title = "ISO 639 name", args = { code2 } }

	local showRom = args["showromanization"] and mw.ustring.lower(args["showromanization"]) == "true"

	local output = {}
	local sortable = args["sortable"]
	local tableClass = "wikitable"
	if not (sortable and mw.ustring.lower(sortable) == "false") then
		tableClass = tableClass .. " sortable"
	end

	if showRom then
		table.insert(output, '{| class="' .. tableClass .. '"')
		table.insert(output, string.format("! %s !! Transliteration !! %s", lang1, lang2))
	else
		table.insert(output, '{| class="' .. tableClass .. '"')
		table.insert(output, string.format("! %s !! %s", lang1, lang2))
	end

	-- Named parameters
	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
		local roman = args["rom" .. i] or ""
		table.insert(output, "|-")
		table.insert(output, lang_cell(frame, code1, text))
		if showRom then
			table.insert(output, "| " .. frame:preprocess(roman))
		end
		table.insert(output, lang_cell(frame, code2, trans))
		i = i + 1
	end

	-- Positional fallback
	local index = 1
	while true do
		local text = args[index]
		local roman = showRom and args[index + 1] or nil
		local trans = showRom and args[index + 2] or args[index + 1]

		if not text and not trans then break end
		table.insert(output, "|-")
		table.insert(output, lang_cell(frame, code1, text))
		if showRom then
			table.insert(output, "| " .. frame:preprocess(roman or ""))
			index = index + 3
		else
			index = index + 2
		end
		table.insert(output, lang_cell(frame, code2, trans))
	end

	table.insert(output, "|}")

	-- When subst'd, output the raw table (static)
	if isSubst then
		return table.concat(output, "\n")
	end

	-- When not substituted, return normally
	return table.concat(output, "\n")
end