Template:Fallback: Difference between revisions

From Meta, a Wikimedia project coordination wiki
Content deleted Content added
Zolo (talk | contribs)
autotranslate: try other namespaces when page is Template:Base does not exist
m sync with sandbox: Fix fallback for de-ch to de; this version is possibly slower but is aimed to be more accurate
Line 4: Line 4:


function p.fblist(lang) -- list the full fallback chain from a language to en
function p.fblist(lang) -- list the full fallback chain from a language to en
local fbstr = p.fallbackloop(' ' .. lang)
local fbtable = p.fallbackloop{ lang:lower() }
fbstr = fbstr .. ' ' .. 'default'
table.insert(fbtable, 'default')
table.insert(fbtable, 'en')
fbstr = fbstr .. ' ' .. 'en'
return mw.text.split(fbstr, " ")
return fbtable
end
end


function _inArray(x, t)
function p.fallbackloop(init) --list of fallback languages in string format (more convenient than tables)
for i, v in ipairs(t) do
local new = init
if v == x then return i end
for i, j in pairs(mw.text.split(init, " ")) do
end
if langlist[j] then
return -1
for k, l in pairs(langlist[j]) do
end
if not string.find(new, ' ' .. l, 1, true) then

new = new .. ' ' .. l
function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables)
local changes = false
for i, j in ipairs(fbtable) do
local seq = langlist[j]
if seq then
for k, l in ipairs(seq) do
if _inArray(l, fbtable) == -1 then
table.insert(fbtable, l)
changes = true
end
end
end
end
end
end
end
end
if new ~= init then
if changes then
return p.fallbackloop(new)
return p.fallbackloop(fbtable)
end
end
return new
return fbtable
end
end


Line 38: Line 47:
-- get the list of accpetable language (lang + those in lang's fallback chain) and check their content
-- get the list of accpetable language (lang + those in lang's fallback chain) and check their content
local parselist = p.fblist(lang)
local parselist = p.fblist(lang)
for j, k in pairs(parselist) do
for i, k in ipairs(parselist) do
if args[k] == '~' then return '' end
if args[k] == '~' then return '' end
if args[k] and args[k] ~= '' then return args[k] end
if args[k] and args[k] ~= '' then return args[k] end
Line 61: Line 70:
function p.fallbackpage(base, lang, formatting)
function p.fallbackpage(base, lang, formatting)
local languages = p.fblist(lang)
local languages = p.fblist(lang)
for i, j in pairs(languages) do
for i, j in ipairs(languages) do
if mw.title.new(base .. '/' .. j).exists then
if mw.title.new(base .. '/' .. j).exists then
if formatting == 'table' then
if formatting == 'table' then
Line 105: Line 114:
end
end
return p._langSwitch(page, lang)
return p._langSwitch(page, lang)
end

function p.runTests()
local toFallbackTest = require('Module:Fallback/tests/fallbacks')
local result = true

mw.log('Testing fallback chains')
for i, t in ipairs(toFallbackTest) do
local fbtbl = table.concat(p.fblist(t.initial), ', ')
local expected = table.concat(t.expected, ', ')
local ret = (fbtbl == expected)
mw.log(i, ret and 'passed' or 'FAILED', t.initial, (not ret) and ('FAILED\nis >>' .. fbtbl .. '<<\nbut should be >>' .. expected .. '<<\n') or '')
result = result and ret
end
return result
end
end



Revision as of 23:48, 20 December 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 = require('Module:Fallbacklist')

function p.fblist(lang) -- list the full fallback chain from a language to en
	local fbtable = p.fallbackloop{ lang:lower() }
	table.insert(fbtable, 'default')
	table.insert(fbtable, 'en')
	return fbtable
end

function _inArray(x, t)
	for i, v in ipairs(t) do
		if v == x then return i end
	end
	return -1
end

function p.fallbackloop(fbtable)  --list of fallback languages in string format (more convenient than tables)
	local changes = false
	for i, j in ipairs(fbtable) do
		local seq = langlist[j]
		if seq then
			for k, l in ipairs(seq) do
				if _inArray(l, fbtable) == -1 then
					table.insert(fbtable, l)
					changes = true
				end
			end
		end
	end
	if changes then
		return p.fallbackloop(fbtable)
	end
	return fbtable
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 '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]' 
	end
	-- get language (either stated one or user's default language)
	if not lang then
		return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
	end
	-- get the list of accpetable language (lang + those in lang's fallback chain) and check their content
	local parselist = p.fblist(lang)
	for i, k in ipairs(parselist) do 
		if args[k] == '~' then return '' end
		if args[k] and args[k] ~= '' then return args[k] end
	end
end

function p.langSwitch(frame) -- version to be used from wikitext
	args = frame.args
	-- if no expected args provided than check parent template/module args
	if args.en==nil and args.default==nil and args.nocat==nil then
		args = mw.getCurrentFrame():getParent().args 
	end
	if args.lang and args.lang ~= '' then
		lang = args.lang
		args.lang = nil
	else -- get user's chosen language 
		lang = frame:preprocess( "{{int:lang}}" )
	end
	return p._langSwitch(args, lang)
end

function p.fallbackpage(base, lang, formatting)
	local languages = p.fblist(lang) 
	for i, j in ipairs(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 the 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( "{{int:lang}}" )
	end
	local page = p.fallbackpage('Template:' .. args.base, args.lang)
	if not page then 
		page = p.fallbackpage(args.base, args.lang)
	end
	if not page then
		return '<strong class="error">no fallback page found for autotranslate</strong>'
	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 .. '[[Category:Pages with incorrect template usage/Autotranslate]]'
		end
	end
	local maintext = frame:expandTemplate{ title = page, args = newargs }
	return maintext .. errormessage
end

function p.translatelua(frame)
	local lang = frame.args.lang
	local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations
	if not lang or mw.text.trim(lang) == '' then
		lang = frame:preprocess( "{{int:lang}}" )
	end
	return p._langSwitch(page, lang)	
end

function p.runTests()
	local toFallbackTest = require('Module:Fallback/tests/fallbacks')
	local result = true

	mw.log('Testing fallback chains')
	for i, t in ipairs(toFallbackTest) do
		local fbtbl = table.concat(p.fblist(t.initial), ', ')
		local expected = table.concat(t.expected, ', ')
		local ret = (fbtbl == expected)
		mw.log(i, ret and 'passed' or 'FAILED', t.initial, (not ret) and ('FAILED\nis >>' .. fbtbl .. '<<\nbut should be >>' .. expected .. '<<\n') or '')
		result = result and ret
	end
	
	return result
end

return p