Module:Fallback

local p = {} local langlist = { af = {'nl'}, als = {'de'}, arz = {'ar'}, bar = {'de'}, bexold = {'betarask'}, br = {'fr'}, ckb = {'fa'}, co = {'fr'}, crhcyrl = {'crh'}, crhlatn = {'crh'}, csb = {'pl'}, deat = {'de'}, dech = {'de'}, deformal = {'de'}, engb = {'en'}, frp = {'fr'}, frr = {'de'}, fy = {'nl'}, glk = {'fa'}, hifdeva = {'hif'}, hilatn = {'hif'}, ht = {'fr'}, ikecans = {'ik'}, kkarab = {'kk'}, kkcn = {'kk'}, kkcyrl = {'kk'}, kkkz = {'kk'}, kklatn = {'kk'}, kktr = {'kk'}, ksh = {'de'}, kuarab = {'ku'}, kulatn = {'ku'}, li = {'nl'}, mg = {'fr'}, mzn = {'fa'}, nds = {'ndsnl', 'de'}, ndsnl = {'nds', 'nl'}, nlinformal = {'nl'}, pdt = {'nds', 'de'}, ptbr = {'pt'}, ruggrek = {'ruq'}, rulatn = {'ruq'}, ruqcyrl = {'ruq'}, srec = {'sr'}, srel = {'sr'}, szl = {'pl'}, tgcyrl = {'tg'}, tglatn = {'tg'}, ttcyrl = {'tt'}, ttlatn = {'tt'}, ty = {'fr'}, vec = {'it'}, vls = {'nl'}, wa = {'fr'}, zea = {'nl'}, zh = {'zhhans'}, zhcn = {'zhhans'}, zhhans = {'zhcn', 'zh'}, zhhant = {'zh'}, zhhk = {'zhhant'}, zhmo = {'zhhant'}, zhmy = {'zhhans'}, zhsg = {'zhhans'}, zhtw = {'zhhant'} }

function p.processarg(arg) if arg == '~' then return '' elseif arg then return arg else return nil end end

function p.getfallback(args, lang, totest, tested) if not tested then tested = {} end for i, j in pairs(totest) do		if args[j] and args[j] ~= '' then return args[j] elseif not tested[j] then table.insert(totest, j)		end table.insert(tested, j)	totest[j] = nil end if totest == {} then return nil end return p.getfallback(args, lang, totest, tested) end

function p.langSwitch(frame) args = mw.getCurrentFrame:getParent.args if not args.en and not args.default and args.nocat ~= '1' then return ' LangSwitch error: no default ' .. mw.text.listToText(frame.args) end if args.lang then lang = args.lang else lang = frame:preprocess( "" ) end for i, j in pairs(args) do		i = mw.ustring.gsub( i, "-", "") end if args[lang] then return p.processarg(args[lang]) end if langlist[lang] then fb = p.getfallback(args, lang, langlist[lang]) if fb and fb ~= '' then return p.processarg(fb) end end if args.default and args.default ~= '' then return p.processarg(args.default) end if args.en and args.en ~= '' then return p.processarg(args.en) end end return p