Difference between revisions of "Module:Template translation"

Jump to navigation Jump to search
m
92 revisions imported
en>Shirayuki
(add pagename parameter)
m (92 revisions imported)
 
(6 intermediate revisions by 5 users not shown)
Line 38: Line 38:
     and subpage ~= "init"
     and subpage ~= "init"
     and subpage ~= "preload"
     and subpage ~= "preload"
    and subpage ~= "subpage"
    and subpage ~= "subpage2"
    and subpage ~= "sub-subpage"
    and subpage ~= "sub-sub-subpage"
    and subpage ~= "sub-sub-sub-subpage"
     then
     then
         return subpage
         return subpage
Line 43: Line 48:
     -- Otherwise there's currently no known language subpage
     -- Otherwise there's currently no known language subpage
     return default
     return default
end
--[[Get the last subpage of an arbitrary page if it is a translation.
    To be used from templates.
    ]]
function this.getLanguageSubpage(frame)
local title = frame and frame.args[1]
if not title or title == '' then
title = mw.title.getCurrentTitle()
end
return this._getLanguageSubpage(title)
end
--[[Get the last subpage of an arbitrary page if it is a translation.
    To be used from Lua.
    ]]
function this._getLanguageSubpage(title)
if type(title) == 'string' then
title = mw.title.new(title)
end
if not title then
-- invalid title
return mw.language.getContentLanguage():getCode()
end
--[[This code does not work in all namespaces where the Translate tool works.
--  It works in the main namespace on Meta because it allows subpages there
--  It would not work in the main namespace of English Wikipedia (but the
--  articles are monolignual on that wiki).
--  On Meta-Wiki the main space uses subpages and its pages are translated.
--  The Translate tool allows translatng pages in all namespaces, even if
--  the namespace officially does not have subpages.
--  On Meta-Wiki the Category namespace still does not have subpages enabled,
--  even if they would be very useful for categorizing templates, that DO have
--  subpages (for documentatio and tstboxes pages). This is a misconfiguration
--  bug of Meta-Wiki. The work-around is to split the full title and then
--  get the last titlepart.
local subpage = title.subpageText
--]]
local titleparts = mw.text.split(title.fullText, '/')
local subpage = titleparts[#titleparts]
return this.checkLanguage(subpage, mw.language.getContentLanguage():getCode())
end
end


--[[Get the last subpage of the current page if it is a translation.
--[[Get the last subpage of the current page if it is a translation.
     ]]
     ]]
function this.getLanguageSubpage(pagename)
function this.getCurrentLanguageSubpage()
    --[[This code does not work in all namespaces where the Translate tool works.
return this._getLanguageSubpage(mw.title.getCurrentTitle())
    --  It works in the main namespace on Meta because it allows subpages there
    --  It would not work in the main namespace of English Wikipedia (but the
    --  articles are monolignual on that wiki).
    --  On Meta-Wiki the main space uses subpages and its pages are translated.
    --  The Translate tool allows translatng pages in all namespaces, even if
    --  the namespace officially does not have subpages.
    --  On Meta-Wiki the Category namespace still does not have subpages enabled,
    --  even if they would be very useful for categorizing templates, that DO have
    --  subpages (for documentatio and tstboxes pages). This is a misconfiguration
    --  bug of Meta-Wiki. The work-around is to split the full title and then
    --  get the last titlepart.
    local subpage = mw.title.getCurrentTitle().subpageText
    --]]
    local title = (pagename or mw.title.getCurrentTitle().fullText)
    local titleparts = mw.text.split(title, '/')
    local subpage = titleparts[#titleparts]
    return this.checkLanguage(subpage, '')
end
end


Line 71: Line 100:
     ]]
     ]]
function this.getMainLanguageSubpage()
function this.getMainLanguageSubpage()
parts = mw.text.split( this.getLanguageSubpage(), '-' )
parts = mw.text.split( this.getCurrentLanguageSubpage(), '-' )
return parts[1]
return parts[1]
end
end
Line 79: Line 108:
     ]]
     ]]
function this.getFrameLanguageSubpage(frame)
function this.getFrameLanguageSubpage(frame)
    local titleparts = mw.text.split(frame:getParent():getTitle(), '/')
return this._getLanguageSubpage(frame:getParent():getTitle())
    local subpage = titleparts[#titleparts]
    return this.checkLanguage(subpage, '')
end
end


Line 162: Line 189:
     if (subpage == '')
     if (subpage == '')
     then
     then
         subpage = this.getLanguageSubpage()
         subpage = this.getCurrentLanguageSubpage()
     end
     end
     if (subpage == '')
     if (subpage == '')

Navigation menu