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(lang) -- list the full fallback chain from a language to en local fbstr = p.fallbackloop(' ' .. lang) fbstr = fbstr .. ' ' .. 'default' fbstr = fbstr .. ' ' .. 'en' return mw.text.split(fbstr, " ") end

function p.fallbackloop(init) --list of fallback languages in string format (more convenient than tables) local new = init for i, j in pairs(mw.text.split(init, " ")) do		if langlist[j] then for k, l in pairs(langlist[j]) do if not string.find(new, ' ' .. l, 1, true) then new = new .. ' ' .. l				end end end end if new ~= init then return p.fallbackloop(new) end return new 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) local lang = args.lang if not lang or lang == '' then lang = frame:preprocess( "" ) end -- parses the argumen's about the user's language if args[lang] == '~' then -- special value signalling nothing should be shown return '' elseif args[lang] and args[lang] ~= ''then return args[lang] -- if not there, does the same thing for languages in the fallback list else for j, k in pairs(p.fblist(lang)) do 		if k == '~' then return '' end if k and k ~= '' then return k end end end end

return p