Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Fallback: Difference between revisions

From Neodyland Wiki
No edit summary
Undo revision 257942246 by Jarekt (talk) I mean to change sandbox
Line 3: Line 3:
local langlist = require('Module:Fallbacklist')
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)
_langSwitch
for i, v in ipairs(t) do
if v == x then return i end
This function is the core part of the LangSwitch template.
end
return -1
Example usage from Lua:
end
text = _langSwitch({en='text in english', pl='tekst po polsku'}, lang)
Parameters:
  args - table with translations by language
  lang - desired language (often user's native language)


  Error Handling:
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
function p._langSwitch(args, lang) -- args: table of translations
-- Return error if there is not default and no english version
-- Return error if there is not default and no english version
if not args.en and not args.default then
if not args.en and not args.default and args.nocat ~= '1' then
if args.nocat == '1' then
return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]'  
return '<strong class="error">LangSwitch Error: no default</strong>'
else
return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]'  
end
end
end
-- get language (either stated one or user's default language)
-- get language (either stated one or user's default language)
Line 33: Line 45:
return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
return '<strong class="error">LangSwitch Error: no lang</strong>' -- must become proper error
end
end
-- get the list of accepetable 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 langList = mw.language.getFallbacksFor(lang)
local parselist = p.fblist(lang)
table.insert(langList,1,lang)
for i, k in ipairs(parselist) do  
table.insert(langList, 'default')
if args[k] == '~' then return '' end
for _, language  in ipairs(langList) do  
if args[k] and args[k] ~= '' then return args[k] end
if args[language ] == '~' then  
return ''
elseif args[language] and args[language] ~= '' then  
return args[language]  
end
end
end
end
end


--[[
langSwitch
This function is the core part of the LangSwitch template.
Example Usage from a template:
{{#invoke:fallback|langSwitch|en=text in english|pl=tekst po polsku|lang={{int:lang}} }}
Parameters:
  frame.args - table with translations by language
  frame.args.lang - desired language (often user's native language)
Error Handling:
]]
function p.langSwitch(frame) -- version to be used from wikitext
function p.langSwitch(frame) -- version to be used from wikitext
args = frame.args
args = frame.args
Line 69: Line 61:
if args.lang and args.lang ~= '' then
if args.lang and args.lang ~= '' then
lang = args.lang
lang = args.lang
args.lang = nil
else -- get user's chosen language  
else -- get user's chosen language  
lang = frame:callParserFunction( "int", "lang" )
lang = frame:preprocess( "{{int:lang}}" )
end
end
args.lang = nil
return p._langSwitch(args, lang)
return p._langSwitch(args, lang)
end
end


--[[
function p.fallbackpage(base, lang, formatting)
autotranslate
local languages = p.fblist(lang)
for i, lng in ipairs(languages) do
This function is the core part of the Autotranslate template.  
if mw.title.new(base .. '/' .. lng).exists then
if formatting == 'table' then
Usage from a template:
return {base .. '/' .. lng, lng} -- returns name of the page + name of the language
{{#invoke:fallback|autotranslate|base=|lang= }}
else
return base .. '/' .. lng -- returns only the page
Parameters:
end
  frame.args.base - base page name
end
  frame.args.lang - desired language (often user's native language)
end
end


Error Handling:
function p.autotranslate(frame) -- logic for [[template:Autotranslate]]
 
]]
function p.autotranslate(frame)  
local args = frame.args
local args = frame.args
if not args.lang or args.lang == '' then
if not args.lang or args.lang == '' then
args.lang = frame:callParserFunction( "int", "lang" )          -- get user's chosen language  
args.lang = frame:preprocess( "{{int:lang}}" )          -- get user's chosen language  
end
end
local langList = mw.language.getFallbacksFor(args.lang)
table.insert(langList,1,args.lang)
   
   
-- find base page
-- find base page
Line 109: Line 97:
   
   
-- find base template language subpage
-- find base template language subpage
local page = nil
local page = p.fallbackpage(base, args.lang) --  
for _, language in ipairs(langList) do
if mw.title.new(base .. '/' .. language).exists then
page =  base .. '/' .. language -- returns only the page
break
end
end
if (not page and base ~= args.base) then  
if (not page and base ~= args.base) then  
-- try the original args.base string. This case is only needed if base is not in template namespace  
-- try the original args.base string. This case is only needed if base is not in template namespace  
for _, language in ipairs(langList) do
page = p.fallbackpage(args.base, args.lang)  
if mw.title.new(args.base .. '/' .. language).exists then
page =  args.base .. '/' .. language -- returns only the page
break
end
end
end
end
if not page then
if not page then
return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang)
return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang)
end
        -- repack args in a standard table
newargs = {}
for field, value in pairs(args) do
if field ~= 'base' then
newargs[field] = value;
end
end
end
   
   
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
args.base = nil
        return frame:expandTemplate{ title = page, args = newargs }
return frame:expandTemplate{ title = page, args = args }
end
end


--[[
function p.translatelua(frame)
fblist
local lang = frame.args.lang
local page = require('Module:' .. mw.text.trim(frame.args[1])) -- page should only contain a simple of translations
Similar to  mw.language.getFallbacksFor(lang) but uses Commons old fallback chain
if not lang or mw.text.trim(lang) == '' then
lang = frame:preprocess( "{{int:lang}}" )
Parameters:
end
  lang - desired language (often user's native language)
if frame.args[2] then
page = page[mw.text.trim(frame.args[2])]
end
return p._langSwitch(page, lang)
end


Error Handling:
function p.runTests()
 
local toFallbackTest = require('Module:Fallback/tests/fallbacks')
]]
local result = true
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)
mw.log('Testing fallback chains')
for i, v in ipairs(t) do
for i, t in ipairs(toFallbackTest) do
if v == x then return i end
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
end
return -1
return result
end
end


function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables)
function p.showTemplateArguments(frame)
local changes = false
-- list all input arguments of the template that calls "{{#invoke:Fallback|showTemplateArguments}}"
for i, j in ipairs(fbtable) do
local str = ''
local seq = langlist[j]
for name, value in pairs( mw.getCurrentFrame():getParent().args ) do
if seq then
if str=='' then
for k, l in ipairs(seq) do
str = string.format('%s=%s', name, value)         -- argument #1
if _inArray(l, fbtable) == -1 then
else
table.insert(fbtable, l)
str = string.format('%s, %s=%s', str, name, value) -- the rest
changes = true
end
end
end
end
end
end
if changes then
return str
return p.fallbackloop(fbtable)
end
return fbtable
end
end


return p
return p

Revision as of 13:49, 7 September 2017

Module documentation[ view · edit · history · purge ]
This documentation is transcluded from Module:Fallback/doc.

Dependencies

  • Module:Fallbacklist catalogs old fallback chains for each language as it was set up on Commons a decade ago. Lua allows now to access the default MediaWiki fallback chains which are used by most templates.
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, lng in ipairs(languages) do
		if mw.title.new(base .. '/' .. lng).exists then
			if formatting == 'table' then
				return {base .. '/' .. lng, lng} -- returns name of the page + name of the language
			else
				return base .. '/' .. lng -- returns only the page
			end
		end
	end
end

function p.autotranslate(frame) -- logic for [[template:Autotranslate]]
	local args = frame.args
	if not args.lang or args.lang == '' then
		args.lang = frame:preprocess( "{{int:lang}}" )           -- get user's chosen language 
	end
 
	-- find base page
	local base = args.base
	if not base or base == '' then
		return '<strong class="error">Base page not provided for autotranslate</strong>' 
	end
	if string.sub(base,2,9) ~= 'emplate:' then
		base = 'Template:' .. base   -- base provided without 'Template:' part 
	end
 
	-- find base template language subpage
	local page = p.fallbackpage(base, args.lang) -- 
	if (not page and base ~= args.base) then 
		-- try the original args.base string. This case is only needed if base is not in template namespace 
		page = p.fallbackpage(args.base, args.lang) 
	end
	if not page then
		return string.format('<strong class="error">no fallback page found for autotranslate (base=[[%s]], lang=%s)</strong>', args.base, args.lang)
	end
 
        -- repack args in a standard table
	newargs = {}
	for field, value in pairs(args) do
		if field ~= 'base' then
			newargs[field] = value;
		end
	end
 
	-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
        return frame:expandTemplate{ title = page, args = newargs }
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
	if frame.args[2] then
		page = page[mw.text.trim(frame.args[2])]
	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

function p.showTemplateArguments(frame)
-- list all input arguments of the template that calls "{{#invoke:Fallback|showTemplateArguments}}"
	local str = ''
	for name, value in pairs( mw.getCurrentFrame():getParent().args ) do
		if str=='' then
			str = string.format('%s=%s', name, value)          -- argument #1
		else
			str = string.format('%s, %s=%s', str, name, value) -- the rest
		end
	end
	return str
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.