Mòdul:Llengua

De Viquidites

La documentació d'ús d'aquest mòdul es pot crear a Mòdul:Llengua/ús

local p = {}

local codis = mw.loadData("Module:llengua/taula") -- taula llengua - script
local scripts = mw.loadData("Module:llengua/script") -- taula script - style

-- Plantilla:lang
function p.lang(frame)
	local args = frame:getParent().args
	local codi = args[1]; if codi == "" then codi = nil end
	local text = args[2]; if tetx == "" then text = nil end
	local sc = args.sc; if sc == "" then sc = nil end
	local ret = text or ''
	
	if not (codi and text) then
		error("És necessari proporcionar dos paràmetres.")
	end
	
	if not sc then
		local sc_codi = codis[codi]
		if sc_codi then
			sc = sc_codi.script
		end
	end
	local style
	if sc and sc ~= "Latn" then
		local sc_style = scripts[sc]
		if sc_style then
			style = sc_style.style
		else
			error(sc .. ": no està definit")
		end
	end
	
	if codi ~= "ca" or style then
		ret = '<span'
		if codi ~= "ca" then
			ret = ret .. ' lang="' .. codi .. '"'
		end
		if style then
			ret = ret .. ' style="' .. style .. '"'
		end
		ret = ret .. '>' .. text .. '</span>'
	end
	
    return ret
end

-- Cerca el nom de llengua definit a /taula o en la llibreria de MediaWiki
function p.nom(codi)
    if type(codi) == "table" then codi = codi.args[1] end -- des de plantilles via invoke o des de mòduls via require
    if codi == nil then
        return
    end
    local llenguaTaula = codis[codi]
    local nom = nil
    if llenguaTaula then
        nom = llenguaTaula.nom
    end
    if nom == nil then
        nom = mw.language.fetchLanguageName(codi, 'ca') -- ca = nom en català
    end
    if nom == nil or nom == "" then
        return codi
    end
    return nom
end

return p