Jump to content

Template:Fallback: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Zolo (talk | contribs)
Undo revision 110105589 by Zolo (talk) ???
Zolo (talk | contribs)
??
Line 67: Line 67:
}
}


function returnarg(arg)
function p.processarg(arg)
if arg == '~' then
if arg == '~' then
return ''
return ''
Line 75: Line 75:
return nil
return nil
end
end
end

function p.getfallback(args, lang, totest, tested)
if not tested then tested = {} end
for i, j in pairs(totest) do
for k, l in pairs(langlist[j]) do
if args[l] then
return args[l]
else if not tested[l] then
table.insert(totest, l)
end
end
table.insert(tested, j)
table.remove(totest, j)
end
if totest == {} then
return nil
end
return p.getfallback(args, lang, totest, tested)
end
end


Line 88: Line 107:
end
end
if args[lang] then
if args[lang] then
return returnarg(args[lang])
return processarg(args[lang])
end
end
if langlist[lang] then
if langlist[lang] then
fb = p.getfallback(args, lang, langlist[lang])
tested = {} -- keep track of tested languages to avoid loops
if fb then
for i, j in pairs(langlist[lang]) do
return p.processarg[bf]
if args[j] then
return returnarg(args[j])
end
end
end
end
end
end
if args.default then
if args.default then
return returnarg(args.default)
return p.processarg(args.default)
end
end
if args.en then
if args.en then
return returnarg(args.en)
return processarg(args.en)
end
end
return '[[Category:LangSwitch template without default version]]'
return '[[Category:LangSwitch template without default version]]'

Revision as of 09:02, 20 November 2013

Template documentation

This template was imported from commons:Template:Fallback without the here unusable documentation.


Use localised subpage for the user language with fallback to {{CONTENTLANGUAGE}}

Template parameters[Edit template data]

ParameterDescriptionTypeStatus
page1

Template name

Page namerequired
lang2

override user language on localised page

Default
{{int:Lang}}
Stringoptional

local p = {}
local langlist = {
 af = {'nl'}, 
 als = {'de'}, 
 arz = {'ar'}, 
 bar = {'de'}, 
 bexold = {'betarask'}, 
 br = {'fr'}, 
 ckb = {'fa'}, 
 co = {'fr'}, 
 crhcyrl = {'crh'}, 
 crhlatn = {'crh'}, 
 csb = {'pl'}, 
 deat = {'de'}, 
 dech = {'de'}, 
 deformal = {'de'}, 
 engb = {'en'}, 
 frp = {'fr'}, 
 frr = {'de'}, 
 fy = {'nl'}, 
 glk = {'fa'}, 
 hifdeva = {'hif'}, 
 hilatn = {'hif'}, 
 ht = {'fr'}, 
 ikecans = {'ik'}, 
 kkarab = {'kk'}, 
 kkcn = {'kk'}, 
 kkcyrl = {'kk'}, 
 kkkz = {'kk'}, 
 kklatn = {'kk'}, 
 kktr = {'kk'}, 
 ksh = {'de'}, 
 kuarab = {'ku'}, 
 kulatn = {'ku'}, 
 li = {'nl'}, 
 mg = {'fr'}, 
 mzn = {'fa'}, 
 nds = {'ndsnl', 'de'}, 
 ndsnl = {'nds', 'nl'}, 
 nlinformal = {'nl'}, 
 pdt = {'nds', 'de'}, 
 ptbr = {'pt'}, 
 ruggrek = {'ruq'}, 
 rulatn = {'ruq'}, 
 ruqcyrl = {'ruq'}, 
 srec = {'sr'}, 
 srel = {'sr'}, 
 szl = {'pl'}, 
 tgcyrl = {'tg'}, 
 tglatn = {'tg'}, 
 ttcyrl = {'tt'}, 
 ttlatn = {'tt'}, 
 ty = {'fr'}, 
 vec = {'it'}, 
 vls = {'nl'}, 
 wa = {'fr'}, 
 zea = {'nl'}, 
 zh = {'zhhans'}, 
 zhcn = {'zhhans'}, 
 zhhans = {'zhcn', 'zh'}, 
 zhhant = {'zh'}, 
 zhhk = {'zhhant'}, 
 zhmo = {'zhhant'}, 
 zhmy = {'zhhans'}, 
 zhsg = {'zhhans'}, 
 zhtw = {'zhhant'}
}

function p.processarg(arg)
	if arg == '~' then
		return ''
	elseif arg then
		return arg
	else
		return nil
	end
end

function p.getfallback(args, lang, totest, tested)
	if not tested then tested = {} end
	for i, j in pairs(totest) do
		for k, l in pairs(langlist[j]) do
			if args[l] then
				return args[l]
			else if not tested[l] then
				table.insert(totest, l)
			end
		end
	table.insert(tested, j)
	table.remove(totest, j)
	end
	if totest == {} then 
		return nil
	end
	return p.getfallback(args, lang, totest, tested)
end

function p.langSwitch(frame)
	local args = frame.args
	for i, j in pairs(args) do
		i = mw.ustring.gsub( i, "-", "")
	end
	if args.lang then
		lang = args.lang
	else 
		lang = frame:preprocess('{{int:lang}}')
	end
	if args[lang] then
		return processarg(args[lang])
	end
	if langlist[lang] then
		fb = p.getfallback(args, lang, langlist[lang])
		if fb then
			return p.processarg[bf]
		end
	end
	end	
	if args.default then
		return p.processarg(args.default)
	end
	if args.en then 
		return processarg(args.en)
	end
	return '[[Category:LangSwitch template without default version]]'
end 
return p