Jump to content

Module:ISO 639

विकिपीडिया से
मॉड्यूल बिबरनलेख[बनाईं]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local data = mw.loadData('Module:ISO 639/data')
local altnames = mw.loadData('Module:ISO 639/data/altnames') 
local ISO_639_5 = mw.loadData('Module:ISO 639/data/ISO_639-5')

function p.part2(frame)                                                         -- to output part 3 code
	frame.args.type = "part2"
	return p.main(frame)
end

function p.part5(frame)                                                         -- to output part 3 code
	if yesno(frame.args.hierachy)  then
		frame.args.type = "hierachy"
	else
		frame.args.type = "part5"
	end
	return p.main(frame)
end

function p.part3(frame)                                                         -- to output part 3 code
	frame.args.type = "part3"
	return p.main(frame)
end

function p.part1(frame)                                                         -- to output part 1 code
	frame.args.type = "part1"
	return p.main(frame)
end

function p.name(frame)                                                          -- to output name
	frame.args.type = "name"
	return p.main(frame)
end

function p.get(text)                                                            -- remove junk and standardizes
	if text == table then
		text = text[1]
	end
	if string.upper(text) == text then                                          -- assume it's a code when the input is all uppercase
		text = string.lower(text)
	end
	local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A",                   -- accent list
		["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E",
		["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",["Î"]="I",
		["Ï"]="I",["Ñ"]="N",["Ò"]="O",["Ó"]="O",["Ô"]="O",
		["Õ"]="O",["Ö"]="O",["Ø"]="O",["Ù"]="U",["Ú"]="U",
		["Û"]="U",["Ü"]="U",["Ý"]="Y"
	}
		
	text = mw.ustring.gsub(text,"[À-Ý]",accents)                                -- Deaccent
	text = mw.ustring.gsub(text,"%[","")                                        -- Delink
	text = mw.ustring.gsub(text,"%]","")                                        -- Delink
	text = mw.ustring.gsub(text,"%{","")                                        -- Remove {
	text = mw.ustring.gsub(text,"%}","")                                        -- Remove }
	return text 
end

function p.main(frame)                                                          -- main function (doesn't need to be called directly)
	local args = getArgs(frame)
	args[1] = p.get(args[1])                                                    

	if altnames[args[1]] then args[1] = altnames[args[1]] end                   -- change alternate name to ISO 639-3 code
	
	if not args[1] then
		return '<span class="error">Argument 1 is not set!</span>'
	end
	
	for part3,table in pairs(data) do
		if args[1] == part3
			or args[1] == table["part1"]
			or args[1] == table["part2"]
			or args[1] == table["name"]
		then
			if args.type == "part3" then
				return part3 or ""
			elseif args.type == "part1" then
				return table["part1"] or ""
			elseif args.type == "part2" then
				return table["part2"] or ""
			elseif args.type == "name" then
				return table["name"] or ""
			end
		end
	end
	
	for hierachy,table in pairs(ISO_639_5) do
		if table.altnames then
			for _,value in pairs(table.altnames) do
				if args[1] == table then
					args[1] = table.name
				end
			end
		end
		
		if table[args[1]] == hierachy
			or args[1] == table["part5"]
			or args[1] == table["part2"]
			or args[1] == table["name"]
		then
			if args.type == "hierachy" then
				return hierachy or ""
			elseif args.type == "part5" then
				return table["part5"] or ""
			elseif args.type == "part2" then
				return table["part2"] or ""
			elseif args.type == "name" then
				return table["name"] or ""
			end
		end
	end
end


return p