Difference between revisions of "Module:Template translation"

Jump to navigation Jump to search
m
forward the noshift parameter to avoid infinite recursion
en>Verdy p
m
en>Verdy p
m (forward the noshift parameter to avoid infinite recursion)
Line 85: Line 85:
end
end


function this.titleNew(pagename, namespace)
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 {
     return { -- "pseudo" mw.title object with id = nil in case of error
        message = message,
         prefixedText = pagename, -- the only property we need below
        id = 0,
         message = message -- only for debugging
         namespace = '',
         prefixedText = pagename,
     }
     }
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.titleNew(pagename, namespace) -- Costly
         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.titleNew(pagename, namespace) -- Costly
         title = this.title(namespace, pagename) -- Costly
         if title.id == 0
         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.titleNew(pagename, namespace) -- Costly
             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.titleNew(pagename .. '/' .. 'en', namespace) -- Costly
         local newtitle = this.title(namespace, pagename, 'en') -- Costly
         -- Use the translation when it exists
         -- Use the translation when it exists
         if newtitle.id ~= 0
         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.titleNew(pagename .. '/' .. subpage, namespace) -- Costly
         local newtitle = this.title(namespace, pagename, subpage) -- Costly
         if newtitle.id == 0
         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.titleNew(pagename .. '/' .. 'en', namespace) -- Costly
             newtitle = this.title(namespace, pagename, 'en') -- Costly
         end
         end
         -- Use the translation when it exists
         -- Use the translation when it exists
         if newtitle.id ~= 0
         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 == 0
     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.uselang -- argument added to parent frame
     arguments['uselang'] = args['uselang'] -- argument forwarded into parent frame
     arguments['noshift'] = args.noshift -- argument added to parent frame
     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}
Anonymous user

Navigation menu