Jump to content

Template:Fallback: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Zolo (talk | contribs)
test
Zolo (talk | contribs)
:-]
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'},
['be-x-old'] = {'be-tarask'},
br = {'fr'},
['br'] = {'fr'},
ckb = {'fa'},
['ckb'] = {'fa'},
co = {'fr'},
['co'] = {'fr'},
crhcyrl = {'crh'},
['crh-cyrl'] = {'crh'},
crhlatn = {'crh'},
['crh-latn'] = {'crh'},
csb = {'pl'},
['de-ch'] = {'de'},
deat = {'de'},
['de-formal'] = {'de'},
dech = {'de'},
['de-formal'] = {'de'},
deformal = {'de'},
['en-gb'] = {'en'},
engb = {'en'},
['frp'] = {'fr'},
frp = {'fr'},
['frr'] = {'de'},
frr = {'de'},
['fy'] = {'nl'},
fy = {'nl'},
['glk'] = {'fa'},
glk = {'fa'},
['hif-deva'] = {'hif'},
hifdeva = {'hif'},
['hif-latn'] = {'hif'},
hilatn = {'hif'},
['ht'] = {'fr'},
ht = {'fr'},
['ike-cans'] = {'ik'},
ikecans = {'ik'},
['kk-arab'] = {'kk'},
kkarab = {'kk'},
['kk-cn'] = {'kk'},
kkcn = {'kk'},
['kk-cyrl'] = {'kk'},
kkcyrl = {'kk'},
['kk-kz'] = {'kk'},
kkkz = {'kk'},
['kk-latn'] = {'kk'},
kklatn = {'kk'},
['kk-tr'] = {'kk'},
kktr = {'kk'},
['ksh'] = {'de'},
ksh = {'de'},
['ku-arab'] = {'ku'},
kuarab = {'ku'},
['ku-latn'] = {'ku'},
kulatn = {'ku'},
['li'] = {'nl'},
li = {'nl'},
['mg'] = {'fr'},
mg = {'fr'},
['mzn'] = {'fa'},
mzn = {'fa'},
['nds'] = {'nds-nl', 'de'},
nds = {'ndsnl', 'de'},
['nds-nl'] = {'nds', 'nl'},
['nds-nl'] = {'nds', 'nl'},
['nl-informal']= {'nl'},
nlinformal = {'nl'},
['pdt'] = {'nds', 'de'},
pdt = {'nds', 'de'},
['pt-br'] = {'pt'},
ptbr = {'pt'},
['ruq-grek'] = {'ruq'},
ruggrek = {'ruq'},
['rug-latn'] = {'ruq'},
rulatn = {'ruq'},
['ruq-cyrl'] = {'ruq'},
ruqcyrl = {'ruq'},
['sr-ec'] = {'sr'},
srec = {'sr'},
['sr-el'] = {'sr'},
srel = {'sr'},
['szl'] = {'pl'},
szl = {'pl'},
['tg-cyrl'] = {'tg'},
tgcyrl = {'tg'},
['tg-latn'] = {'tg'},
tglatn = {'tg'},
['tt-cyrl'] = {'tt'},
ttcyrl = {'tt'},
['tt-latn'] = {'tt'},
ttlatn = {'tt'},
['ty'] = {'fr'},
ty = {'fr'},
['vec'] = {'it'},
vec = {'it'},
['vls'] = {'nl'},
vls = {'nl'},
['wa'] = {'fr'},
wa = {'fr'},
['zea'] = {'nl'},
zea = {'nl'},
['zh'] = {'zh-hans'},
zh = {'zhhans'},
['zh-cn'] = {'zh-hans'},
zhcn = {'zhhans'},
['zh-hans'] = {'zh-cn', 'zh'},
zhhans = {'zhcn', 'zh'},
['zh-hant'] = {'zh'},
zhhant = {'zh'},
['zh-hk'] = {'zh-hant'},
zhhk = {'zhhant'},
['zh-mo'] = {'zh-hant'},
zhmo = {'zhhant'},
['zh-my'] = {'zh-hans'},
zhmy = {'zhhans'},
['zh-sg'] = {'zh-hans'},
zhsg = {'zhhans'},
['zh-tw'] = {'zh-hant'}
zhtw = {'zhhant'}
}
}


Line 121: Line 120:
else
else
lang = frame:preprocess( "{{int:lang}}" )
lang = frame:preprocess( "{{int:lang}}" )
end
-- remove dashes from language names
for i in pairs(args) do
args[mw.ustring.gsub( i, "-", "")] = args[i]
end
end
-- gets the most appropriate language
-- gets the most appropriate language

Revision as of 11:02, 21 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'}, 
 ['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.getfallback(args, lang)
	if not lang then lang = args.lang end
	
	if args[lang] and args[lang] ~= '' then
		return 	args[lang]
	end
	if langlist[lang] then -- if there is a fallbacklist, check if there is a message for its languages 
		text = p.fallbackloop(args, langlist[lang], '')
	end
	if text then
		return text
	end
	if args.default then
		return args.default
	end
	if args.en then 
		return args.en
	end
end

function p.fallbackloop(args, totest, tested) 
	-- iterates through the langlist fallback table
	-- totest = tables of languages whose fallbacks need to be examined
	-- tested = string with language that have been examined
	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, lang, 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
	-- gets the most appropriate language 
	text = p.getfallback(args)
	if text == '~' then return '' end -- ~ indicates that no message should be shown in this language
	return text 
end

return p