Ir al contenido

Módulo:Lang

De Wikisource, la biblioteca libre.
Documentación del módulo


Uso

Lógica detrás de {{Lang}}
Esta documentación está transcluida desde Módulo:Lang/doc.
Los editores pueden experimentar en la zona de pruebas
Por favor, añade las categorías a la subpágina de documentación.
(subpáginas - enlaces)

local p = {}

local getArgs = require('Módulo:Arguments').getArgs
local yesno = require('Módulo:Yesno')

--[=[
Get the appropriate text direction for a language
]=]
function p._text_direction(args)
	local rtl_langs = {
		ae = true,				-- Avestan
		ar = true,				-- Arabic
		arc = true,				-- Aramaic
		dv = true,				-- Dhivehi
		fa = true,				-- Persian
		ha = true,				-- Hausa
		he = true,				-- Hebrew
		hbo = true,				-- Hebrew
		ira = true,				-- Iranian
		khw = true,				-- Khowar
		ks = true,				-- Kashmiri
		ku = true,				-- Kurdish
		['obm-hebr'] = true,	-- Moabite
		ps = true,				-- Pashto
		syc = true,				-- Syriac
		syr = true,				-- Syriac
		ur = true,				-- Urdu
		yi = true				-- Yiddish
	}
	
	local lang = args.lang
	
	if lang and rtl_langs[lang] then
		return 'rtl'
	elseif type(lang) == 'string' then
		local stripped_lang = mw.text.split(lang, '-')[1]
		if rtl_langs[stripped_lang] then
			return 'rtl'
		end
	end
	
	return 'ltr'
end

--[=[
Implementa [[Plantilla:Lang]] y [[Plantilla:Bloque lang]]
]=]
function p._lang(args)
	local lang = args.idioma or args.language or args.lang or args[1] or 'es'
	local dir = args.direction or args.dir or p._text_direction({['lang'] = lang})
	
	local text = args.texto or args.text or args[2]
	
	local inline = yesno(args.inline) ~= false -- default is true
	local noclose = yesno(args.noclose) or false -- default is false
	
	-- Span or div?
	local tag = (inline and 'span') or 'div'
	local content = mw.html.create(tag)
	
	-- Set the attributes
	content
		:addClass(table.concat({'wst-lang', args.class}, ' '))
		:attr('lang', lang)
		:attr('xml:lang', lang)
		:attr('dir', dir)
		:css({
			['font-family'] = args.fuente or args.fonts or args.font,
			['style'] = args.estilo or args.style
		})
		:allDone()
	
	if text and inline then
		content:wikitext(text)
	elseif text then
		content:newline():wikitext(text)
	end
	
	if not inline and not noclose then
		content:newline()
	end
	
	content = tostring(content)
	
	if noclose then
		content = string.gsub(content, '</' .. tag .. '>$', '')
	end
	
	return content
end

function p.lang(frame)
	local args = getArgs(frame)
	return p._lang(args)
end

return p