Jump to content

Module:GommehTranslationModule

From Wikipedia, the free encyclopedia

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, text)
	code = code or ""
	text = text or ""
	if code ~= "" then
		return "| " .. frame:preprocess("{{Langx|" .. code .. "|" .. text .. "|label=none|italic=unset}}")
	else
		return "| " .. text
	end
end

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

	-- 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 } }

	-- Check if romanization is enabled
	local showRom = args["showromanization"] and mw.ustring.lower(args["showromanization"]) == "true"

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

	-- Start table with caption (optional future upgrade)
	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 (text1, rom1, 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
		local roman = args["rom" .. i] or ""
		table.insert(output, "|-")
		table.insert(output, lang_cell(frame, code1, text))
		if showRom then
			table.insert(output, "| " .. roman)
		end
		table.insert(output, lang_cell(frame, code2, trans))
		i = i + 1
	end

	-- Positional fallback (|text|roman|trans) or (|text|trans)
	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, "| " .. (roman or ""))
			index = index + 3
		else
			index = index + 2
		end
		table.insert(output, lang_cell(frame, code2, trans))
	end

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

return p