Module:Foreign names

--a table that maps language codes to the english name of the corresponding 	language. we should just use mw.language.fetchLanguageName for this, but i 	guess it's broken...? ``mw.language.fetchLanguageName("de", "en")`` returns 	"Deutsch" even though the documentation says it should return "German" local LANGUAGE_CODES = { cs = "Czech", da = "Danish", de = "German", en = "English", es = "Spanish", fi = "Finnish", fr = "French", he = "Hebrew", hu = "Hungarian", it = "Italian", ja = "Japanese", ko = "Korean", nl = "Dutch", no = "Norwegian", pl = "Polish", pt = "Portuguese", ro = "Romanian", ru = "Russian", sv = "Swedish", zh = "Chinese", ["zh-hans"] = "Simplified Chinese", ["zh-hant"] = "Traditional Chinese", --propritary language codes from template:foreign_names. most of them 		to match iso 639-2, but one is also literally a racial slur, so i don't		know whether it's really worth keeping them cze = "Czech", dan = "Danish", ger = "German", spa = "Spanish", fin = "Finnish", fra = "French", fre = "French", heb = "Hebrew", hun = "Hungarian", ita = "Italian", jap = "Japanese", kor = "Korean", dut = "Dutch", nor = "Norwegian", pol = "Polish", por = "Portuguese", rom = "Romanian", rus = "Russian", swe = "Swedish", chi = "Chinese", chis = "Simplified Chinese", chit = "Traditional Chinese", }

--allow case-insensitive language codes, show warning if no code was found setmetatable(LANGUAGE_CODES, {		__index = function(this, key) 			local code = rawget(this, string.lower(key))			if code == nil then				mw.addWarning("WARNING (Foreign names): Could not find a language code corresponding to \"" .. mw.text.nowiki(key) .. "\".")				code = key			end			return code		end	} )

--splits a string at the first ":" and returns the (whitespace-stripped) 	characters on either side local function split( value ) local pos = string.find(value, ":") return string.gsub(string.sub(value, 0, pos - 1), '^%s*(.-)%s*$', '%1'), string.gsub(string.sub(value, pos + 1, -1), '^%s*(.-)%s*$', '%1') end

--searches a table t for a member that is evaluated to true by the test 	function f. if found, return that member. if not, create a new object with	function m, add it to t, and return it local function getOrCreateMember ( t, f, m ) for _, value in ipairs(t) do 		if f(value) then return value end end local created = m table.insert(t, created) return created end

--processes the data from frame.args into an intermediate format that can be	converted into an html table local function processLangs( arg ) local out = {} local lang = {} local word = {} for _, parameter in ipairs(arg) do   	local name, value if pcall(function name, value = split(parameter) end) then name = string.lower(name) if name == "l" then decodedValue = LANGUAGE_CODES[value] lang = getOrCreateMember(out, function(f) return f.l == decodedValue end, function return {["l"] = decodedValue} end) elseif name == "w" then word = getOrCreateMember(lang, function(f) return f.w == value end, function return {["w"] = value} end) elseif name == "c" or name == "r" or name == "m" then word[name] = value else mw.addWarning("WARNING (Foreign names): Could not parse \"" .. mw.text.nowiki(parameter) .. "\".") end else mw.addWarning("WARNING (Foreign names): Could not parse \"" .. mw.text.nowiki(parameter) .. "\".") end end return out end

--generate html for one word local function processWord(word) out = " " .. word.w	if word.c then out = out .. " (" .. word.c .. ") " end if word.r then out = out .. " ''" .. word.r .. "''"	end out = out .. " "

out = out .. " "	if word.m then out = out .. word.m 	end out = out .. "  "	return out

end

--generate html for one language local function processLang(lang) out = " " .. lang.l .. " "	for _, value in ipairs(lang) do out = out .. processWord(value) end out = out .. " "	return out end

local p = {}

--entry point, generates an html table of the names of a subject in various 	languages function p.table( frame ) local langs = processLangs(frame.args) out = [[ "   return out end

return p