Module:Template translation: Difference between revisions

m
catch undocumented exveption thrown by mw.title.new
mNo edit summary
m (catch undocumented exveption thrown by mw.title.new)
Line 83:
local subpage = titleparts[#titleparts]
return this.checkLanguage(subpage, mw.language.getContentLanguage():getCode())
end
 
function this.titleNew(pagename, namespace)
local message, title
local valid, title = xpcall(function()
return mw.title.new(pagename, namespace) -- costly
end, function(msg) -- catch undocumented exception (!?)
-- thrown when namespace does not exist
message = msg
end)
if valid and title ~= nil and title.id ~= 0
then
return title
end
return {
message = message,
id = 0,
namespace = '',
prefixedText = pagename,
}
end
 
Line 103 ⟶ 123:
if (namespace ~= '') -- Checks for namespace parameter for custom ns.
then
title = mwthis.title.newtitleNew(pagename, namespace) or {} -- Costly
else -- Supposes that set page is in ns10.
namespace = 'Template'
title = mwthis.title.newtitleNew(pagename, namespace) or {} -- Costly
if (title.id or 0) == 0
then -- not found in the Template namespace, assume the main namespace (for backward compatibility)
namespace = ''
title = mwthis.title.newtitleNew(pagename, namespace) or {} -- Costly
end
end
Line 123 ⟶ 143:
then
-- Check if a translation of the pagename exists in English
local newtitle = mwthis.title.newtitleNew(pagename .. '/' .. 'en', namespace) or {} -- Costly
-- Use the translation when it exists
if (newtitle.id or 0) ~= 0
then
title = newtitle
Line 131 ⟶ 151:
else
-- Check if a translation of the pagename exists in that language
local newtitle = mwthis.title.newtitleNew(pagename .. '/' .. subpage, namespace) or {} -- Costly
if (newtitle.id or 0) == 0
then
-- Check if a translation of the pagename exists in English
newtitle = mwthis.title.newtitleNew(pagename .. '/' .. 'en', namespace) or {} -- Costly
end
-- Use the translation when it exists
if (newtitle.id or 0) ~= 0
then
title = newtitle
Line 145 ⟶ 165:
-- At this point the title should exist, otherwise render a red link to the missing page (resolved in its assumed namespace)
if (title.id or 0) == 0
then
return '[[' .. title.prefixedText .. ']]'
Anonymous user