Module:Languages

-- local l = {}

local function escRx(text, spaces) text = mw.ustring.gsub(text, '%\\', '\\\\') text = mw.ustring.gsub(text, '%(', '\\(') text = mw.ustring.gsub(text, '%)', '\\)') text = mw.ustring.gsub(text, '%.', '\\.') text = mw.ustring.gsub(text, '%+', '\\+') text = mw.ustring.gsub(text, '%*', '\\*') text = mw.ustring.gsub(text, '%?', '\\?') text = mw.ustring.gsub(text, '%^', '\\^') text = mw.ustring.gsub(text, '%$', '\\$') text = mw.ustring.gsub(text, '%-', '\\-') text = mw.ustring.gsub(text, '/', '\\/') text = mw.ustring.gsub(text, '=', '\\=') text = mw.ustring.gsub(text, '!', '\\!') text = mw.ustring.gsub(text, ':', '\\:') if spaces then text = mw.ustring.gsub(text, ' ', '_') end return text end local function makeLink(page, text, preload) if preload then return ' ['..tostring(mw.uri.fullUrl( page, 'action=edit&redlink=1&preload='..preload ))..' '..text..'] ' else return ..text.. end end

function l.subpages(page, namespace) local frame = mw.getCurrentFrame local existing = mw.ustring.lower(frame:preprocess('')) existing = select(3, mw.ustring.find(existing, '^%s*%|([%|a-z-]*)%|%s*$')) if existing then return mw.text.split(existing, '%s*|%s*') end end

function l.langs(frame) local args = require('Dev:Arguments').getArgs(frame, {   	trim = true,    	removeBlanks = true,    	frameOnly = true,    	readOnly = true    }) local title, root = args['page'] and mw.title.new(args['page']) or mw.title.getCurrentTitle if mw.ustring.find(title.subpageText, '[a-z-]+') and mw.language.fetchLanguageName(title.subpageText) ~= '' then root = title.baseText else root = title.text end local rootLang = mw.ustring.lower(args['root'] or 'en') local langs = {} for i,v in ipairs(args or {}) do       v = mw.ustring.lower(mw.text.trim(v or '')) if v ~= '' then langs[v] = false end end local existing = l.subpages(root, title.nsText) or {} for i,v in ipairs(existing) do       if v ~= '' then if v == rootLang then rootLang = nil end langs[v] = true end end for k,v in pairs(args) do       if type(k) == "string" and mw.language.fetchLanguageName(k) ~= '' then langs[k] = v       end end local ordered = {} for k,v in pairs(langs) do       if (not rootLang and k ~= 'en') or (rootLang and k ~= rootLang) then ordered[#ordered+1] = k       end end table.sort(ordered) local separator = frame:preprocess('') local div = mw.html.create('div'):addClass(args['class']) div:tag('strong'):wikitext(frame:preprocess('Languages')):done:wikitext(' ') root = mw.ustring.gsub(title.nsText .. ':' .. root, '^:', '') local preload = root if rootLang then div:wikitext(makeLink(root, mw.language.fetchLanguageName(rootLang))) else table.insert(ordered, 1, 'en') preload = preload .. '/en' end for i,lang in ipairs(ordered) do       local name = mw.language.fetchLanguageName(lang) if name ~= '' then if i ~= 1 and not rootLang then div:wikitext(separator) end if not langs[lang] then div:wikitext(makeLink(root .. '/' .. lang, name, preload)) else div:wikitext(makeLink(type(langs[lang]) == 'string' or langs[lang] and root .. '/' .. lang, name)) end end end return div end

return l