Module:Lang

--[=[
Module description
]=]

local p = {} -- p stands for package

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

--[=[
Get the appropriate text direction for a language
]=]

p.rtl_langs = {
	['ae'] = true,				-- Avestan
	['ar'] = true,				-- Arabic
	['arc'] = true,				-- Aramaic/Imperial 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
	['mid'] = true,				-- Mandaic
	['myz'] = true,				-- Classical Mandaic
	['obm-hebr'] = true,		-- Moabite
	['phn'] = true,				-- Phoenician
	['ps'] = true,				-- Pashto
	['syc'] = true,				-- Syriac
	['syr'] = true,				-- Syriac
	['ur'] = true,				-- Urdu
	['yi'] = true,				-- Yiddish
	
	-- scripts
	['Adlm'] = true,			-- Adlam
	['Arab'] = true,			-- Arabic
	['Armi'] = true,			-- Imperial Aramaic
	['Avst'] = true,			-- Avestan
	['Cprt'] = true,			-- Cypriot syllabary
	['Elym'] = true,			-- Elymaic
	['Gara'] = true,			-- Garay
	['Hatr'] = true,			-- Hatran
	['Hebr'] = true,			-- Hebrew
	['Hung'] = true,			-- Old Hungarian (Hungarian Runic)
	['Khar'] = true,			-- Kharoshthi
	['Lydi'] = true,			-- Lydian
	['Mand'] = true,			-- Mandaic, Mandaean
	['Mani'] = true,			-- Manichaean
	['Mend'] = true,			-- Mende Kikakui
	['Merc'] = true,			-- Meroitic Cursive
	['Mero'] = true,			-- Meroitic Hieroglyphs
	['Narb'] = true,			-- Old North Arabian (Ancient North Arabian)
	['Nbat'] = true,			-- Nabataean
	['Nkoo'] = true,			-- N’Ko
	['Orkh'] = true,			-- Old Turkic, Orkhon Runic
	['Palm'] = true,			-- Palmyrene
	['Phli'] = true,			-- Inscriptional Pahlavi
	['Phlp'] = true,			-- Psalter Pahlavi
	['Phnx'] = true,			-- Phoenician
	['Prti'] = true,			-- Inscriptional Parthian
	['Rohg'] = true,			-- Hanifi Rohingya
	['Sarb'] = true,			-- Old South Arabian
	['Sidt'] = true,			-- Sidetic
	['Sogo'] = true,			-- Old Sogdian
	['Syrc'] = true,			-- Syriac
	['Thaa'] = true,			-- Thaana
	['Todr'] = true,			-- Todhri
	['Yezi'] = true				-- Yezidi
}

function p._text_direction(args)
	local lang = args.lang
	
	if lang and p.rtl_langs[lang] then
		return 'rtl'
	elseif type(lang) == 'string' then
		local stripped_lang = mw.text.split(lang, '-')[1]
		if p.rtl_langs[stripped_lang] then
			return 'rtl'
		end
	end
	
	return 'ltr'
end

--[=[
Implements [[Template:Lang]] and [[Template:Lang block]]
]=]
function p._lang(args)
	local lang = args.language or args.lang or args[1] or 'en'
	local dir = args.direction or args.dir or p._text_direction({['lang'] = lang})
	
	local text = 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.fonts or args.font, ['style'] = 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
Category:Wikisource protected modules