Difference between revisions of "Module:Template translation"

Jump to navigation Jump to search
m
catch undocumented exveption thrown by mw.title.new
en>Verdy p
m
en>Verdy p
m (catch undocumented exveption thrown by mw.title.new)
Line 83: Line 83:
     local subpage = titleparts[#titleparts]
     local subpage = titleparts[#titleparts]
     return this.checkLanguage(subpage, mw.language.getContentLanguage():getCode())
     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
end


Line 103: Line 123:
     if (namespace ~= '') -- Checks for namespace parameter for custom ns.
     if (namespace ~= '') -- Checks for namespace parameter for custom ns.
     then
     then
         title = mw.title.new(pagename, namespace) or {} -- Costly
         title = this.titleNew(pagename, namespace) -- Costly
     else -- Supposes that set page is in ns10.
     else -- Supposes that set page is in ns10.
     namespace = 'Template'
     namespace = 'Template'
         title = mw.title.new(pagename, namespace) or {} -- Costly
         title = this.titleNew(pagename, namespace) -- Costly
         if (title.id or 0) == 0
         if title.id == 0
         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 = mw.title.new(pagename, namespace) or {} -- Costly
             title = this.titleNew(pagename, namespace) -- Costly
         end
         end
     end
     end
Line 123: Line 143:
     then
     then
         -- Check if a translation of the pagename exists in English
         -- Check if a translation of the pagename exists in English
         local newtitle = mw.title.new(pagename .. '/' .. 'en', namespace) or {} -- Costly
         local newtitle = this.titleNew(pagename .. '/' .. 'en', namespace) -- Costly
         -- Use the translation when it exists
         -- Use the translation when it exists
         if (newtitle.id or 0) ~= 0
         if newtitle.id ~= 0
         then
         then
             title = newtitle
             title = newtitle
Line 131: Line 151:
     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 = mw.title.new(pagename .. '/' .. subpage, namespace) or {} -- Costly
         local newtitle = this.titleNew(pagename .. '/' .. subpage, namespace) -- Costly
         if (newtitle.id or 0) == 0
         if newtitle.id == 0
         then
         then
             -- Check if a translation of the pagename exists in English
             -- Check if a translation of the pagename exists in English
             newtitle = mw.title.new(pagename .. '/' .. 'en', namespace) or {} -- Costly
             newtitle = this.titleNew(pagename .. '/' .. 'en', namespace) -- Costly
         end
         end
         -- Use the translation when it exists
         -- Use the translation when it exists
         if (newtitle.id or 0) ~= 0
         if newtitle.id ~= 0
         then
         then
             title = newtitle
             title = newtitle
Line 145: Line 165:
      
      
     -- 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 or 0) == 0
     if title.id == 0
     then
     then
     return '[[' .. title.prefixedText .. ']]'
     return '[[' .. title.prefixedText .. ']]'
Anonymous user

Navigation menu