Module:Fallback

local p = {} local langlist = { ['af']        = {'nl'}, ['als']       = {'de'}, ['arz']       = {'ar'}, ['bar']       = {'de'}, ['be-x-old']  = {'be-tarask'}, ['br']        = {'fr'}, ['ckb']       = {'fa'}, ['co']        = {'fr'}, ['crh-cyrl']  = {'crh'}, ['crh-latn']  = {'crh'}, ['de-ch']     = {'de'}, ['de-formal'] = {'de'}, ['de-formal'] = {'de'}, ['en-gb']     = {'en'}, ['frp']       = {'fr'}, ['frr']       = {'de'}, ['fy']        = {'nl'}, ['glk']       = {'fa'}, ['hif-deva']  = {'hif'}, ['hif-latn']  = {'hif'}, ['ht']        = {'fr'}, ['ike-cans']  = {'ik'}, ['kk-arab']   = {'kk'}, ['kk-cn']     = {'kk'}, ['kk-cyrl']   = {'kk'}, ['kk-kz']     = {'kk'}, ['kk-latn']   = {'kk'}, ['kk-tr']     = {'kk'}, ['ksh']       = {'de'}, ['ku-arab']   = {'ku'}, ['ku-latn']   = {'ku'}, ['li']        = {'nl'}, ['mg']        = {'fr'}, ['mzn']       = {'fa'}, ['nds']       = {'nds-nl', 'de'}, ['nds-nl']    = {'nds', 'nl'}, ['nl-informal']= {'nl'}, ['pdt']       = {'nds', 'de'}, ['pt-br']     = {'pt'}, ['ruq-grek']  = {'ruq'}, ['rug-latn']  = {'ruq'}, ['ruq-cyrl']  = {'ruq'}, ['sr-ec']     = {'sr'}, ['sr-el']     = {'sr'}, ['szl']       = {'pl'}, ['tg-cyrl']   = {'tg'}, ['tg-latn']   = {'tg'}, ['tt-cyrl']   = {'tt'}, ['tt-latn']   = {'tt'}, ['ty']        = {'fr'}, ['vec']       = {'it'}, ['vls']       = {'nl'}, ['wa']        = {'fr'}, ['zea']       = {'nl'}, ['zh']        = {'zh-hans'}, ['zh-cn']     = {'zh-hans'}, ['zh-hans']   = {'zh-cn', 'zh'}, ['zh-hant']   = {'zh'}, ['zh-hk']     = {'zh-hant'}, ['zh-mo']     = {'zh-hant'}, ['zh-my']     = {'zh-hans'}, ['zh-sg']     = {'zh-hans'}, ['zh-tw']     = {'zh-hant'} }

function p.fblist(args, lang) fblist = {} if langlist[lang] then fblist = p.fallbackloop(args, langlist[lang]) end table.insert(fblist, 1, lang) -- adds the used language at place one table.insert(fblist, 'default') table.insert(fblist, 'en') return fblist end

function p.fallbackloop(args, fblist) local newlist = fblist for i, j in pairs(fblist) do		if langlist[j] then for k, l in pairs (langlist[j]) do				for m, n in pairs(fblist) do					if n == l then break end table.insert(newlist, n)				end end end end if newlist ~= fblist then return newlist -- p.fallback(args, newlist) else return newlist end end

function p.langSwitch(frame) args = mw.getCurrentFrame:getParent.args -- Return error if there is not default and no english version if not args.en and not args.default and args.nocat ~= '1' then return frame:expandTemplate{ title = "red", args = {'LangSwitch error: no default'}} .. '' 	end -- get language (either stated one or user's default language) if args.lang then lang = args.lang else lang = frame:preprocess( "" ) end -- gets the most appropriate language local fblist = p.fblist(args,lang) for i, j in pairs(fblist) do		if args[j] and args[j] == '~' then -- '~' means, display nothing for this language return '' elseif args[j] then return args[j] end end end

return p