Module:alternative forms

Definition from Wiktionary, the free dictionary
Jump to: navigation, search

This module is called by {{alter}}, the template that is used in Alternative forms sections to link the alternative forms of a term and to show which dialect or spelling system they belong to.

Dialect labels are found in data modules (see Category:Dialectal data modules). The modules are titled with the language code and :Dialects: for instance, Module:en:Dialects. Each label has link text and can give a Wikipedia article that the label should link to. If you would like to create a dialectal data module but do not know how, post on the talk page.

Testcases[edit]

ὑμεῖς(humeîs)
honor

local export = {}
local m_link = require('Module:links')
local m_languages = require("Module:languages")

-- See if the language's dialectal data module has a label corresponding to the dialect argument.
local function getLabel(dialect, dialect_info)
	local label = dialect_info[dialect]
	if label then
		local target = label.link
		local display = label.display or dialect
		dialect = target and ('[[w:'.. target ..'|'..display..']]') or display
	end
	return dialect
end

local function make_dialects(raw, lang)
	local dialect_page = 'Module:'.. lang:getCode() ..':Dialects'
	local dialect_info = mw.title.new(dialect_page).exists and require(dialect_page) or false
		
	local dialects = {}
	
	for _, dialect in pairs(raw) do
		table.insert(dialects, dialect_info and getLabel(dialect, dialect_info) or dialect)
	end
	
	return dialects
end

local function langError(lang)
	if not lang then
		-- This error message will likely never appear, because [[Module:parameters]]
		-- replaces an empty required parameter with nil.
		error("The first parameter (language code) is missing.", 2)
	elseif lang:match("^%l%l$") or lang:match("^%l%l%l$") or lang:match("^%l%l%l%-%l%l%l$") or lang:match("^%l%l%l%-%l%l%l%-%l%l%l$") then
		error("The language code \"" .. lang .. "\" is not valid.", 2)
	else
		error("Please enter a language code in the first parameter", 2)
	end
end

function export.create(frame)
	NAMESPACE = mw.title.getCurrentTitle().nsText
	
	params = {
		[1] = { required = true },
		[2] = { required = true, list = true, allow_holes = true },
		["alt"] = { list = true, allow_holes = true },
		["id"] = { list = true, allow_holes = true },
		["tr"] = { list = true, allow_holes = true },
		["sc"] = {},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	local lang = args[1] or (NAMESPACE == "Template" and "und") or langError(args[1])
	local sc = require("Module:scripts").getByCode(args["sc"])
	
	lang = m_languages.getByCode(lang) or langError(lang)
	
	
	local rawDialects = {}
	local links = {}

	for i = 1, args[2].maxindex do
		-- If the previous parameter was empty and we're not on the first parameter,
		-- this parameter and any others contain dialect or other labels.
		if i > 1 and not args[2][i - 1] then
			rawDialects = {unpack(args[2], i, args[2].maxindex)}
			break
		-- If there's a term, link it and add it to the list.
		elseif args[2][i] then
			term = m_link.full_link( { lang = lang, sc = sc, term = args[2][i], alt = args.alt[i], id = args.id[i], tr = args.tr[i] } )
			table.insert(links, term)
		end
	end

	local dialects  = make_dialects(rawDialects, lang)
	
	local output = { table.concat(links, ', ') }
	if #dialects > 0 then
		if lang:hasTranslit() then
			table.insert(output, " – ''"..table.concat(dialects, ", ").."''")
		else
			table.insert(output, " (''"..table.concat(dialects, ", ").."'')")
		end
	end
	
	return table.concat(output)
end

return export