Module documentation[view] [edit] [history] [purge]

For {{lang}}.

--[=[
Module description
]=]

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local language_name = require('Module:ISO_639').language_name

--[=[
Get the appropriate text direction for a language
]=]
function p._text_direction(args)
	local rtl_langs = {
		'Arabic', -- ar
		'Aramaic', -- arc
		'Dhivehi', -- dv
		'Persian', -- fa
		'Hausa', -- ha
		'Hebrew', -- he, hbo
		'Khowar', -- khw
		'Kashmiri', -- ks
		'Kurdish', -- ku
		'Moabite', -- obm-hebr
		'Pashto', -- ps
		'Syriac', -- syc, syr
		'Urdu', -- ur
		'Yiddish' -- yi
	}
	
	local lang = args.lang or ''
	local stripped_lang = mw.text.split(lang, '-')[1]
	
	local lang_name = language_name(lang) or language_name(stripped_lang)

	-- lang_name can be nil, which will make string.mach() blow up
	-- TODO: This should probably emit a tracking category.
	if lang_name ~= nil then
		for k, rtl_lang in pairs(rtl_langs) do
			if string.match(lang_name, rtl_lang) then
				return 'rtl'
			end
		end
	end
	return 'ltr'
end

function p.text_direction(frame)
	return p._text_direction(getArgs(frame))
end

--[=[
Implements [[Template:Lang]]
]=]
function p.lang(frame)
	local args = getArgs(frame)
	
	local lang = args.lang or "en"
	local text = args.text or ""
	local inline = yesno(args.inline or "yes")
	
	local font = args.font
	local style = args.style
	local class = args.class
	local attr = args.attr
	local noclose = yesno(args.noclose or "no")
	
	--[=[
	Define the text direction
	]=]
	local dir = p._text_direction({lang = args.lang})
	
	--[=[
	Span or div?
	]=]
	local tag
	if inline then
		tag = "span"
	else
		tag = "div"
	end
	
	--[=[
	Set the language attributes.
	]=]
	local lang_def = " lang = '" .. lang .. "' xml:lang='" .. lang .. "' dir = '" .. dir .. "'"
	
	--[=[
	Set the style.
	]=]
	local style_def = ""
	if font then
		style_def = "font-family:" .. font .. ";"
	end
	if style then
		style_def = style_def .. " " .. style
	end
	if style_def ~= "" then
		style_def = " style = '" .. style_def .. "'"
	end
	
	--[=[
	Set the class.
	]=]
	local class_def = "wst-lang"
	if class then
		class_def = class_def .. " " .. class
	end
	class_def = " class = '" .. class_def .. "'"
	
	--[=[
	Set other attributes.
	]=]
	local attr_def = ""
	if attr then
		attr_def = " " .. attr
	end
	
	--[=[
	Make the tagged content.
	]=]
	local content = "<" .. tag .. lang_def .. style_def .. class_def .. attr_def .. ">" .. text
	if not noclose then
		content = content .. "</" .. tag .. ">"
	end
	return content
end

return p