Jump to content

Template:Fallback: Difference between revisions

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


Line 100: Line 100:
function p.langSwitch(frame)
function p.langSwitch(frame)
args = mw.getCurrentFrame():getParent().args
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
if not args.en and not args.default and args.nocat ~= '1' then
return frame:expandTemplate{ title = "red", args = {'LangSwitch error: no default'}} .. '[[Category:LangSwitch template without default version]]'
return frame:expandTemplate{ title = "red", args = {'LangSwitch error: no default'}} .. '[[Category:LangSwitch template without default version]]'
end
end
-- get language (either stated one or user's default language)
if args.lang then
if args.lang then
lang = args.lang
lang = args.lang
Line 108: Line 110:
lang = frame:preprocess( "{{int:lang}}" )
lang = frame:preprocess( "{{int:lang}}" )
end
end
--
for i in pairs(args) do
for i in pairs(args) do
args[mw.ustring.gsub( i, "-", "")] = args[i]
args[mw.ustring.gsub( i, "-", "")] = args[i]
end
end
-- if one of the arguments matches the desired language than return it
if args[lang] and args[lang] ~= '' then
if args[lang] and args[lang] ~= '' then
return p.processarg(args[lang])
return p.processarg(args[lang])
end
end
-- If language is in the fallback list
if langlist[lang] then
if langlist[lang] then
fb = p.getfallback(args, langlist[lang])
fb = p.getfallback(args, langlist[lang])

Revision as of 21:26, 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, totest, tested)
	if not tested then tested = '' end  -- tested values in a string, more convenient
	for i, j in pairs(totest) do
		if args[j] and args[j] ~= '' then
				return args[j]
		end
		if langlist[j] then 
			for k, l in pairs(langlist[j]) do
				table.insert(totest, l)
			end
		end
		tested = tested .. ' ' .. j
	end
	for i, j in pairs(totest) do
		if not string.find(tested, ' ' .. j) then 
			p.getfallback(args, totest, tested)
		end
	end
	return nil
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'}} .. '[[Category:LangSwitch template without default version]]' 
	end
	-- get language (either stated one or user's default language)
	if args.lang then 
		lang = args.lang
	else 
		lang = frame:preprocess( "{{int:lang}}" )
	end
	--
	for i in pairs(args) do
		args[mw.ustring.gsub( i, "-", "")] = args[i]
	end
	-- if one of the arguments matches the desired language than return it
	if args[lang] and args[lang] ~= '' then
		return p.processarg(args[lang])
	end
	-- If language is in the fallback list
	if langlist[lang] then
		fb = p.getfallback(args, langlist[lang])
		if fb and fb ~= '' then
			return p.processarg(fb)
		end
	end	
	if args.default and args.default ~= '' then
		return p.processarg(args.default)
	end
	if args.en and args.en ~= '' then 
		return p.processarg(args.en)
	end
end
return p