Documentation for this module may be created at ماڊيول:لغات/doc

local p = 	{}
local lang= require('Module:لغات/بيانات')

function gsubname(temp,the)
	if the and the ~= ''
		then
			s = string.gsub(temp, " ", " ")
			s = "".. s
			fi = s 
		else
			fi = temp 
	end
	return fi
end

function LatnCyrl(code,al,number, returnnil)
	code = code:lower()
	number = (number or 0) +1
	e = string.sub(code, -5)     -- 5 from the end until the end
	s = string.gsub(code, e, '')
	d = p.getname(s, '',number)
	if d and d ~='' then
		if (e == '-latn' or e == '-cyrl' or e == '-arab')
			then
				if e == '-latn' then f = ' لاطيني'
				elseif e == '-cyrl' then f = ' سيريلية'
				elseif e == '-arab' then f = ' عربي'
				end
				  er = d ..  f
				  
			else
		end
	end
	if returnnil and returnnil ~= '' then co = '' else co = code end
	if not er then return co else return gsubname(er,al) end
end

function p.getcode(code)
	s = code
	s = string.gsub(code, ' ', '')
--	s = s:lower()
	for k, v in pairs(lang) do
		for i, r in pairs(v) do
			if (s == r or s == k)
				then 
					return v.name
			end
		end
	end
end

function p.getname(co, al, number, returnnil)
	number = (number or 0) + 1
	if number and number > 3 then return nil end
	if not co or co =='' then return '' end
	code = co  --:lower()
	code = string.gsub(co , ' ', '')--:lower()
	if code and code~= '' then
		val = p.getcode(code) 
		if val and val~= '' 
			then 
				fi = gsubname(val,al)
			else
				fi = LatnCyrl(code,al,number, returnnil)
		end
		end
	return fi
end

function goo(name)
	for ss, v in pairs(lang) do
		if (name == p.getname(ss, 't') or name == p.getname(ss , ''))
		then 
			return ss--frame:preprocess(ss)
		else
			for q, codee in pairs(v) do
				if (name == p.getname(codee, 't') or name == p.getname(codee, ''))
					then return codee
				elseif (name == p.getname(ss .. '-cyrl', 't') or name == p.getname(ss .. '-cyrl', ''))
					then return ss .. '-cyrl'
				elseif (name == p.getname(ss.. '-latn', 't') or  name == p.getname(ss.. '-latn', ''))
					then return ss.. '-latn'
				elseif (name == p.getname(ss.. '-arab', 't') or  name == p.getname(ss.. '-arab', ''))
					then return ss.. '-arab'
				end
			end
		end
	end
	
end

p['اسم لغة'] = function ( frame )
	local na = frame.args[1]
	if not na or na =='' then return '' end
	local code = p.getname(frame.args[1], frame.args[2], 0, frame.args['nil'])
	return frame:preprocess(code)
end

p['قالب رمز لغة'] = function ( frame )
	lange= frame.args[1]
	text = frame.args[2]
	local c = p.getname(frame.args[1], 't')
	if lange and lange ~= '' 
	then
	lange = string.gsub(lange, ' ', '')
	lange = lange:lower()
	textout =  mw.text.tag('span', {lang= lange}, text)
		local p = 'زمرو:مضمون جن ۾ '
		local p2 = ' ٻولي جو متن شامل آھي'
		if lange =='sd' or lange =='snd'
			then cate = '' 
			else
				cate = '[['.. p .. (c or lange) .. p2 .. ']]'
		end
		return  textout .. cate
	else
		return ''
	end
end

p['رمز لغة'] = function ( frame )
	local na = frame.args[1]
		if na and na~= '' 
		then
			return goo(na) or goo(gsubname(na, 't'))
		else
			return ''
	end
end


p['قائمة'] = function ( frame )
	lang= require('Module:لغات/بيانات')
	for k, nam in pairs(lang) do
		for i, v in pairs(nam) do
			name= v.na 
		end
	end
	local list = mw.html.create("table")
	list:addClass("wikitable sortable collapsible")
	local head = list:tag("tr")
	head:tag("th"):wikitext("رمز")
	head:tag("th"):wikitext("تضمين قالب")
	head:tag("th"):wikitext("وصلة")
	head:tag("th"):wikitext("اسم")
	head:tag("th"):wikitext("التحويلات")
	for o, nam in pairs(lang) do
		local temp = nam.name-- or k
		local row = list:tag("tr")
		local oo = row:tag("td")
		local r = row:tag("td")
		local th = row:tag("td")
		local ss = row:tag("td")
		local rr = row:tag("td")
		local rsr = '{{اسم آيزو 639 ' .. o ..'}}'
		local er = '[[سانچو:اسم آيزو 639 ' .. o ..']]'
		local dd = "\n[[ٻولي ".. temp .."]]"
		local ooo = 'ٻولي ' .. gsubname(temp, 'r')
		oo:tag("span"):wikitext(er)
		r:tag("span"):wikitext(frame:preprocess(rsr))
		th:tag("span"):wikitext(dd)
		ss:tag("span"):wikitext(ooo)
			for k,v in pairs(nam) do
				local cc =  {}
						if v and v ~= '' then 
					  		u = v
					  	end
				rr:tag("code"):wikitext(u)
				rr:tag("span"):wikitext(' - ') 
			end
			end
	return list
end

return p