Module:Fallback: Difference between revisions
From Neodyland Wiki
More actions
Remove several preprocess functions; Change langSwitch and autotranslate to use mediawiki language fallback chain instead of Commons specific one |
|||
| Line 3: | Line 3: | ||
local langlist = require('Module:Fallbacklist') | local langlist = require('Module:Fallbacklist') | ||
function | --[[ | ||
_langSwitch | |||
This function is the core part of the LangSwitch template. | |||
Example usage from Lua: | |||
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._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 | if not args.en and not args.default then | ||
return '<strong class="error">LangSwitch Error: no default</strong>[[Category:LangSwitch template without default version]]' | if args.nocat == '1' then | ||
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 45: | Line 33: | ||
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 | -- get the list of accepetable language (lang + those in lang's fallback chain) and check their content | ||
local | local langList = mw.language.getFallbacksFor(lang) | ||
for | table.insert(langList,1,lang) | ||
if args[ | table.insert(langList, 'default') | ||
for _, language in ipairs(langList) do | |||
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 61: | Line 69: | ||
if args.lang and args.lang ~= '' then | if args.lang and args.lang ~= '' then | ||
lang = args.lang | lang = args.lang | ||
else -- get user's chosen language | else -- get user's chosen language | ||
lang = frame: | lang = frame:callParserFunction( "int", "lang" ) | ||
end | end | ||
args.lang = nil | |||
return p._langSwitch(args, lang) | return p._langSwitch(args, lang) | ||
end | end | ||
function | --[[ | ||
autotranslate | |||
This function is the core part of the Autotranslate template. | |||
Usage from a template: | |||
{{#invoke:fallback|autotranslate|base=|lang= }} | |||
Parameters: | |||
frame.args.base - base page name | |||
frame.args.lang - desired language (often user's native language) | |||
Error Handling: | |||
function p.autotranslate(frame) | ]] | ||
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: | args.lang = frame:callParserFunction( "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 92: | Line 104: | ||
return '<strong class="error">Base page not provided for autotranslate</strong>' | return '<strong class="error">Base page not provided for autotranslate</strong>' | ||
end | end | ||
if | if not mw.ustring.find(base,':') then -- if base page does not indicate namespace | ||
base = 'Template:' .. base | base = 'Template:' .. base -- than assume it is a template | ||
end | end | ||
-- find base template language subpage | -- find base template language subpage | ||
local page = | local page = nil | ||
for _, language in ipairs(langList) do | |||
if mw.title.new(base .. '/' .. language).exists then | |||
page = base .. '/' .. language -- returns only the page | |||
break | |||
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 | end | ||
-- repack args in a standard table | |||
local inArgs = {} | |||
for field, value in pairs(args) do | for field, value in pairs(args) do | ||
inArgs[field] = value; | |||
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. | ||
inArgs.base = nil | |||
return frame:expandTemplate{ title = page, args = inArgs } | |||
end | end | ||
--[[ | |||
fblist | |||
Similar to mw.language.getFallbacksFor(lang) but uses Commons old fallback chain | |||
Parameters: | |||
lang - desired language (often user's native language) | |||
Error Handling: | |||
]] | |||
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 | end | ||
function | function _inArray(x, t) | ||
for i, v in ipairs(t) do | |||
if v == x then return i end | |||
for i, | |||
end | end | ||
return -1 | |||
return | |||
end | end | ||
function p. | function p.fallbackloop(fbtable) --list of fallback languages in string format (more convenient than tables) | ||
-- list | local changes = false | ||
local | for i, j in ipairs(fbtable) do | ||
for | local seq = langlist[j] | ||
if | 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 | ||
return | if changes then | ||
return p.fallbackloop(fbtable) | |||
end | |||
return fbtable | |||
end | end | ||
return p | return p | ||
Revision as of 11:56, 8 September 2017
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')
--[[
_langSwitch
This function is the core part of the LangSwitch template.
Example usage from Lua:
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._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 then
if args.nocat == '1' then
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
-- 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 accepetable language (lang + those in lang's fallback chain) and check their content
local langList = mw.language.getFallbacksFor(lang)
table.insert(langList,1,lang)
table.insert(langList, 'default')
for _, language in ipairs(langList) do
if args[language ] == '~' then
return ''
elseif args[language] and args[language] ~= '' then
return args[language]
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
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
else -- get user's chosen language
lang = frame:callParserFunction( "int", "lang" )
end
args.lang = nil
return p._langSwitch(args, lang)
end
--[[
autotranslate
This function is the core part of the Autotranslate template.
Usage from a template:
{{#invoke:fallback|autotranslate|base=|lang= }}
Parameters:
frame.args.base - base page name
frame.args.lang - desired language (often user's native language)
Error Handling:
]]
function p.autotranslate(frame)
local args = frame.args
if not args.lang or args.lang == '' then
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
end
local langList = mw.language.getFallbacksFor(args.lang)
table.insert(langList,1,args.lang)
-- 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 not mw.ustring.find(base,':') then -- if base page does not indicate namespace
base = 'Template:' .. base -- than assume it is a template
end
-- find base template language subpage
local page = nil
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 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
local inArgs = {}
for field, value in pairs(args) do
inArgs[field] = value;
end
-- Transclude {{page |....}} with template arguments the same as the ones passed to {{autotranslate}} template.
inArgs.base = nil
return frame:expandTemplate{ title = page, args = inArgs }
end
--[[
fblist
Similar to mw.language.getFallbacksFor(lang) but uses Commons old fallback chain
Parameters:
lang - desired language (often user's native language)
Error Handling:
]]
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
return p