Module:Fallback

local p = {}

local langlist = require('Module:Fallbacklist')

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(args, lang) -- args: table of translations -- 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 'LangSwitch error: no default ' end -- get language (either stated one or user's default language) if not lang then return "langSwitch error: no lang" -- must become proper error 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.langSwitchfromWiki(frame) -- version to be used from wikitext args = mw.getCurrentFrame:getParent.args if args.lang and args.lang ~= '' then lang = args.lang args.lang = nil else lang = frame:preprocess( "" ) end return p.langSwitch(args, lang) end

function p.fallbackpage(base, lang) local languages = p.fblist(lang) for i, j in pairs(languages) do if mw.title.new(base .. '/' .. j).exists then if formatting == 'table' then return {base .. '/' .. j, j} -- returns name of the page + name of the language else return base .. '/' .. j -- returns only thn page end end end end

function p.autotranslate(frame) -- logic for template:Autotranslate local args = frame.args local maintext = '' local errormessage = '' if not args.lang or args.lang == '' then args.lang = frame:preprocess( "" ) end local page = p.fallbackpage('Template:' .. args.base, args.lang) if not page then return "no fallback page found for autotranslate" end newargs = {} for i, j in pairs(args) do		if type(i) == 'number' then table.insert(newargs, j)		elseif i ~= 'base' and i ~= 'lang' then errormessage = errormessage .. ''		end end local maintext = frame:expandTemplate{ title = page, args = newargs } return maintext .. errormessage end

return p