--[[ __ __ _ _ _ _ _ _ _ | \/ | ___ __| |_ _| | ___ _ / \ _ _| |_ ___ | |_ _ __ __ _ _ __ ___| | __ _| |_ ___ | |\/| |/ _ \ / _` | | | | |/ _ (_) / _ \| | | | __/ _ \| __| '__/ _` | '_ \/ __| |/ _` | __/ _ \ | | | | (_) | (_| | |_| | | __/_ / ___ \ |_| | || (_) | |_| | | (_| | | | \__ \ | (_| | || __/ |_| |_|\___/ \__,_|\__,_|_|\___(_)_/ \_\__,_|\__\___/ \__|_| \__,_|_| |_|___/_|\__,_|\__\___| Authors and maintainers:* User:Zolo - original version* User:Jarekt ]]-- local function to help normalize input argumentslocalfunctionnormalize_input_args(input_args,output_args)forname,valueinpairs(input_args)doifvalue~=''then-- nuke empty stringsiftype(name)=='string'thenname=string.lower(name)end-- convert to lower caseoutput_args[name]=valueendendreturnoutput_argsend-- initialize object to be returnedlocalp={}--[[autotranslateThis function is the core part of the Autotranslate template. Usage from a template:{{#invoke:autotranslate|autotranslate|base=|lang= }}Parameters: frame.args.base - base page name frame.args.lang - desired language (often user's native language) Error Handling:]]functionp.autotranslate(frame)-- switch to lowercase parameters to make them case independentlocalargs={}args=normalize_input_args(frame:getParent().args,args)args=normalize_input_args(frame.args,args)-- get language fallback listifnotargs.langornotmw.language.isSupportedLanguage(args.lang)thenargs.lang=frame:callParserFunction("int","lang")-- get user's chosen language endlocallangList=mw.language.getFallbacksFor(args.lang)table.insert(langList,1,args.lang)-- user's language will be the first one to check-- find base pagelocalbase=args.baseargs.base=nil-- blank it so it is not passed to language sub-templatesassert(baseand#base>0,'Base page not provided for autotranslate')-- Local function for expanding a template that can be pcall()ed: call the template -- with the same template arguments as the ones passed to {{autotranslate}} template.localfunctionexpandTemplate(title)returnframe:expandTemplate{title=title,args=args}end-- find base template language subpagelocalsuccess,resfor_,languageinipairs(langList)dosuccess,res=pcall(expandTemplate,base..'/'..language)ifsuccessthenbreakendendlocalerr_msg='No fallback page found for autotranslate (base=[[:%s]], lang=%s)'if(notsuccess)thenassert(args.default,string.format(err_msg,base,args.lang))success,res=pcall(expandTemplate,args.default)assert(success,string.format(err_msg,base,args.default))end-- If this if the base page being translatedaif(mw.title.getCurrentTitle().fullText==base)then-- English language is the last fallback language for most languages, if-- nothing else there should be at least an english subpagelocalen_exist=mw.title.new(base..'/en').existsassert(en_exist,string.format(err_msg,base,'en'))if(mw.site.siteName=='Wikimedia Commons')thenres=res..'\n[[Category:Autotranslated templates|'..base..']]'endendreturnresendreturnp