Module:Template translation: Difference between revisions
Content added Content deleted
mNo edit summary |
m (forward the noshift parameter to avoid infinite recursion) |
||
Line 85: | Line 85: | ||
end |
end |
||
function this. |
function this.title(namespace, basepagename, subpage) |
||
local message, title |
local message, title |
||
local pagename = basepagename |
|||
if (subpage or '') ~= '' |
|||
then |
|||
pagename = pagename .. '/' .. subpage |
|||
end |
|||
local valid, title = xpcall(function() |
local valid, title = xpcall(function() |
||
return mw.title.new(pagename, namespace) -- costly |
return mw.title.new(pagename, namespace) -- costly |
||
end, function(msg) -- catch undocumented exception (!?) |
end, function(msg) -- catch undocumented exception (!?) |
||
-- thrown when namespace does not exist |
-- thrown when namespace does not exist. The doc still |
||
-- says it should return a title, even in that case... |
|||
message = msg |
message = msg |
||
end) |
end) |
||
if valid and title ~= nil and title.id ~= 0 |
if valid and title ~= nil and (title.id or 0) ~= 0 |
||
then |
then |
||
return title |
return title |
||
end |
end |
||
return { -- "pseudo" mw.title object with id = nil in case of error |
|||
return { |
|||
⚫ | |||
message = message, |
|||
message = message -- only for debugging |
|||
namespace = '', |
|||
⚫ | |||
} |
} |
||
end |
end |
||
Line 123: | Line 127: | ||
if (namespace ~= '') -- Checks for namespace parameter for custom ns. |
if (namespace ~= '') -- Checks for namespace parameter for custom ns. |
||
then |
then |
||
title = this. |
title = this.title(namespace, pagename) -- Costly |
||
else -- Supposes that set page is in ns10. |
else -- Supposes that set page is in ns10. |
||
namespace = 'Template' |
namespace = 'Template' |
||
title = this. |
title = this.title(namespace, pagename) -- Costly |
||
if title.id == |
if title.id == nil |
||
then -- not found in the Template namespace, assume the main namespace (for backward compatibility) |
then -- not found in the Template namespace, assume the main namespace (for backward compatibility) |
||
namespace = '' |
namespace = '' |
||
title = this. |
title = this.title(namespace, pagename) -- Costly |
||
end |
end |
||
end |
end |
||
Line 143: | Line 147: | ||
then |
then |
||
-- Check if a translation of the pagename exists in English |
-- Check if a translation of the pagename exists in English |
||
local newtitle = this. |
local newtitle = this.title(namespace, pagename, 'en') -- Costly |
||
-- Use the translation when it exists |
-- Use the translation when it exists |
||
if newtitle.id ~= |
if newtitle.id ~= nil |
||
then |
then |
||
title = newtitle |
title = newtitle |
||
Line 151: | Line 155: | ||
else |
else |
||
-- Check if a translation of the pagename exists in that language |
-- Check if a translation of the pagename exists in that language |
||
local newtitle = this. |
local newtitle = this.title(namespace, pagename, subpage) -- Costly |
||
if newtitle.id == |
if newtitle.id == nil |
||
then |
then |
||
-- Check if a translation of the pagename exists in English |
-- Check if a translation of the pagename exists in English |
||
newtitle = this. |
newtitle = this.title(namespace, pagename, 'en') -- Costly |
||
end |
end |
||
-- Use the translation when it exists |
-- Use the translation when it exists |
||
if newtitle.id ~= |
if newtitle.id ~= nil |
||
then |
then |
||
title = newtitle |
title = newtitle |
||
Line 165: | Line 169: | ||
-- At this point the title should exist, otherwise render a red link to the missing page (resolved in its assumed namespace) |
-- At this point the title should exist, otherwise render a red link to the missing page (resolved in its assumed namespace) |
||
if title.id == |
if title.id == nil |
||
then |
then |
||
return '[[' .. title.prefixedText .. ']]' |
return '[[' .. title.prefixedText .. ']]' |
||
Line 196: | Line 200: | ||
arguments['template'] = title.prefixedText -- override the existing parameter of the base template name supplied with the full name of the actual template expanded |
arguments['template'] = title.prefixedText -- override the existing parameter of the base template name supplied with the full name of the actual template expanded |
||
arguments['namespace'] = nil -- discard the specified namespace override |
arguments['namespace'] = nil -- discard the specified namespace override |
||
arguments['uselang'] = args |
arguments['uselang'] = args['uselang'] -- argument forwarded into parent frame |
||
arguments['noshift'] = args |
arguments['noshift'] = args['noshift'] -- argument forwarded into parent frame |
||
return frame:expandTemplate{title = ':' .. title.prefixedText, args = arguments} |
return frame:expandTemplate{title = ':' .. title.prefixedText, args = arguments} |