وحدة:لغات

صفحة محمية جزئيًّا (سماح للمحررين)
من ويكيبيديا، الموسوعة الحرة

local p = {}
local data = require("Module:لغات/بيانات")
local scripts = {
	["-latn"] = " لاتينية",
	["-cyrl"] = " سيريلية",
	["-arab"] = " عربية"
}
local function isvalid(x)
	if x and x ~= "" then
		return x
	end
	return nil
end

local function gsubname(temp, the, code)
	local al = data.lang_name_with_al[code] or data.lang_name_with_al[code:lower()]
	if al then
		return al
	end
	if isvalid(the) then
		return "ال" .. string.gsub(temp, " ", " ال")
	end
	return temp
end

local function LatnCyrl(code, al, number, returnnil)
	local ar_name = ""
	code = code:lower()
	number = (number or 0) + 1
	local e = string.sub(code, -5) -- 5 from the end until the end
	local s = string.gsub(code, e, "")
	local d = p.getname(s, "", number)
	local co = "" and isvalid(returnnil) or code

	if isvalid(d) and scripts[e] then
		ar_name = d .. scripts[e]
	end

	if ar_name == "" then
		return co
	end
	return gsubname(ar_name, al, code)
end

function p.getcode(code)
	local s = code
	s = string.gsub(code, " ", "")
	return data.lang_name[s] or data.lang_name[s:lower()] or data.lang_name_with_al[s:lower()]
end

function p.get_name_from_code(code)
	return p.getcode(code)
end

function p.getname(code, al, number, returnnil)
	number = (number or 0) + 1
	if number and number > 3 then
		return nil
	end
	if not isvalid(code) then
		return ""
	end
	if string.find(code, "[)|(]") then
		return code
	end
	code = string.gsub(code, " ", "")
	local fi
	local name = p.get_name_from_code(code)
	if isvalid(name) then
		fi = gsubname(name, al, code)
	else
		fi = LatnCyrl(code, al, number, returnnil)
	end
	return fi
end

local function get_code_from_name(name)
	if data.lang_codes[name] then
		return data.lang_codes[name]
	end
	for ss, v in pairs(data.lang_table) do
		if (name == p.getname(ss, "t") or name == p.getname(ss, "")) then
			return ss
		else
			for q, codee in pairs(v.codes) 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

local function make_cat(lange)
	if lange == "ar" or lange == "ara" then
		return ""
	end
	local c = p.getname(lange, "t")

	return "[[تصنيف:مقالات تحوي نصا ب" .. (c or lange) .. "]]"
end

p["اسم لغة"] = function(frame)
	local na = frame.args[1]
	if not isvalid(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)
	local lange = frame.args[1]
	local text = frame.args[2]
	if not isvalid(lange) then
		return ""
	end
	lange = string.gsub(lange, " ", "")
	lange = lange:lower()
	local textout = mw.text.tag("span", {lang = lange}, text)
	local cate = make_cat(lange)

	return textout .. cate
end

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

p["قائمة"] = function(frame)
	local list = mw.html.create("table")
	list:addClass("wikitable sortable collapsible")
	list:tag("tr"):tag("th"):wikitext("رمز"):tag("th"):wikitext("تضمين قالب"):tag("th"):wikitext("وصلة"):tag("th"):wikitext(
		"اسم"
	):tag("th"):wikitext("التحويلات")

	for code, nam in pairs(data.lang_table) do
		local temp = nam.name
		local rsr = "{{اسم آيزو 639 " .. code .. "}}"
		local er = "[[قالب:اسم آيزو 639 " .. code .. "]]"
		local dd = "\n[[لغة " .. temp .. "]]"
		local ooo = "اللغة " .. gsubname(temp, "r", code)

		local row = list:tag("tr")
		row:tag("td"):tag("span"):wikitext(er)

		row:tag("td"):tag("span"):wikitext(frame:preprocess(rsr))

		row:tag("td"):tag("span"):wikitext(dd)

		row:tag("td"):tag("span"):wikitext(ooo)

		local rr = row:tag("td")
		for k, v in pairs(nam.codes) do
			local u
			if isvalid(v) then
				u = v
			end
			rr:tag("code"):wikitext(u)
			rr:tag("span"):wikitext(" - ")
		end
	end
	return list
end

return p