Jump to content

Module:TableTranslation: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
adding support for romanization
Line 1: Line 1:
local p = {}
local p = {}


-- Optional: RTL language codes for future Langx handling
-- Optional RTL detection for future Langx/dir support
local rtl_langs = {
local rtl_langs = {
ar = true, he = true, fa = true, ur = true,
ar = true, he = true, fa = true, ur = true,
ps = true, prs = true, syr = true, ckb = true,
ps = true, prs = true, syr = true, ckb = true,
}
}


local function is_rtl(code)
local function is_rtl(code)
if not code then return false end
if not code then return false end
code = mw.ustring.lower(mw.text.trim(code))
code = mw.ustring.lower(mw.text.trim(code))
return rtl_langs[code] or false
return rtl_langs[code] or false
end
end


-- Lang cell using {{Langx}} to support italics and formatting
-- Helper: Build a rendered Lang template cell using frame:preprocess()
local function lang_cell(frame, code, text)
local function lang_cell(frame, code, text)
code = code or ""
code = code or ""
text = text or ""
text = text or ""
if code ~= "" then
if code ~= "" then
return "| " .. frame:preprocess("{{Lang|" .. code .. "|" .. text .. "|italic=unset}}")
return "| " .. frame:preprocess("{{Langx|" .. code .. "|" .. text .. "}}")
else
else
return "| " .. text
return "| " .. text
end
end
end
end


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


-- Resolve language codes from various aliases
local code1 = args["lang1code"] or args["lang1"]
local code2 = args["lang2code"] or args["lang2"]
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
if not code1 or not code2 then
return "Error: Please provide an ISO code for both languages."
return "Error: Please provide both lang1code and lang2code."
end
end


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


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

local useClass = not (classParam and mw.ustring.lower(classParam) == "false")
-- Begin table
if useClass then
local output = {}
table.insert(output, '{| class="wikitable sortable"')
if showRom then
table.insert(output, '{| class="wikitable sortable"')
table.insert(output, string.format("! %s !! Romanization !! %s", lang1, lang2))
else
else
table.insert(output, '{|')
table.insert(output, '{| class="wikitable sortable"')
table.insert(output, string.format("! %s !! %s", lang1, lang2))
end
end
table.insert(output, string.format("! %s !! %s", lang1, lang2))


-- Named parameter loop: text1/trans1, text2/trans2, etc.
-- Named parameters (text1, rom1, trans1)
local i = 1
local i = 1
while true do
while true do
local text = args["text" .. i]
local text = args["text" .. i]
local trans = args["trans" .. i]
local trans = args["trans" .. i]
if not text and not trans then break end
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))
table.insert(output, "|-")
table.insert(output, lang_cell(frame, code1, text))
if showRom then
table.insert(output, lang_cell(frame, code2, trans))
table.insert(output, "| " .. roman)
i = i + 1
end
end
table.insert(output, lang_cell(frame, code2, trans))
i = i + 1
end


-- Positional fallback: |text|trans|text|trans|...
-- Positional fallback (|text|roman|trans) or (|text|trans)
local index = 1
local index = 1
while true do
while true do
local text = args[index]
local text = args[index]
local trans = args[index + 1]
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, "|-")
if not text and not trans then break end
table.insert(output, lang_cell(frame, code1, text))
table.insert(output, lang_cell(frame, code2, trans))
table.insert(output, "|-")
table.insert(output, lang_cell(frame, code1, text))
index = index + 2
if showRom then
end
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, "|}")
table.insert(output, "|}")
return table.concat(output, "\n")
return table.concat(output, "\n")
end
end



Revision as of 18:30, 11 May 2025

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 {{Langx}} 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 .. "}}")
	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 = {}
	if showRom then
		table.insert(output, '{| class="wikitable sortable"')
		table.insert(output, string.format("! %s !! Romanization !! %s", lang1, lang2))
	else
		table.insert(output, '{| class="wikitable sortable"')
		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