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 -- get lhe list of accpetable language (lang + those in lang's fallback chain) and check their content local parselist = p.fblist(lang) for j, k in pairs(parselist) do 		if args[k] == '~' then return '' end if args[k] and args[k] ~= '' then return args[k] end end end

function p.fallbackpage(base, lang) local candidates = p.fblist(lang) for i, j in pairs(candidates) do		local page = '' local frame = mw.getCurrentFrame frame:callParserFunction{'#ifexist', args= {base .. '/' .. j, page = base .. '/' .. j} } if page ~= '' then return page end end end

function p.autotranslate(frame) local args = mw.getCurrentFrame:getParent.args if not args.lang or args.lang == '' then args.lang = frame:preprocess( "" ) end local page = p.fallbackpage('Heavily used template', args.lang) if not page then return "error" end return frame:expandTemplate{ title = page, args = args } end

return p