Module:Template translation

Permanently protected module
From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Guillom (talk | contribs) at 20:03, 24 May 2013 (\o/). It may differ significantly from the current version.
Module documentation

Purpose

This template, originally named "Translatable template" (often abbreviated as TNT, but which never meant "dynamite"!) is used to show translatable templates in the language of the current page. Templates, like all other MediaWiki pages, can be translated using the Translate extension, which creates subpages with the form "pagename/language code". The template first checks if the name of the page contains a language code. If it does, it then checks if the template name given as a first parameter has a translation in that language. If the page name does not contain a language code, or if the navigation template doesn't exist in that language, it will display the English template.

How to use

  • {{Translatable template|name of template|parameters....}}
  • {{TNT|name of template|parameters....}}
  • {{tnt|name of template|parameters....}}

The above simplied syntax cannot work if the named template also needs to be transcluded in other translatable templates, because it would cause self-recursion of {{Translatable template}}. An alternative is to use {{Translatable template name}} which does not expand the template with its parameters, but only returns the resolved template name, which can then be transcluded normally:

  • {{{{Translatable template name|name of template}}|parameters....}}
  • {{{{TNTN|name of template}}|parameters....}}
  • {{{{tntn|name of template}}|parameters....}}

Example (from Meta:Administrator):

{{Translatable template|User groups}} or {{TNT|User groups}}

which includes translated versions of {{User groups}} if it exists, or the English version if translations don't exist.

Parameters

The current version of the template may now include any kind of named or numbered parameters, whose values will be transferred into the called template (with the exception of parameter 1 containing the basename of the translatable template to transclude). Numbered parameters will be shifted down by one position, all named parameters will be passed unchanged.

One named parameter is treated specially:

  • {{Translatable template|namespace=:somename:|page name|parameters....}}
  • {{TNT|namespace=:somename:|page name|parameters....}}
  • {{tnt|namespace=:somename:|page name|parameters....}}

This namespace will be used to specify another namespace from which the translatable pagename will be transcluded, instead of referencing the page name from the default :Template: namespace. Note that this parameter is also passed (without modification) within the parameters of the transclusion.

Example with one parameters (from Global sysops):

  • {{TNT|Special global permissions/Seealso|Global sysops}}

where the second parameter "Global sysops" is the value of the first numbered parameter transferred into called page "Special global permissions/Seealso".

Dependency

See also

-- THIS IS ALPHA CODE. USE CAREFULLY.

local p = {}

function p.renderTranslatedTemplate(frame)
    --[[ If on a translation subpage (like Foobar/de), this function renders
        a given template in the same language, if the translation is available.
        Otherwise, the template is rendered in its default language, without
        modification.
        This is aimed at replacing the current implementation of Template:TNT.
        ]]
    
    local template = frame.args['template']
    local langcode = '/en'
    
    -- Get the last subpage
    local subpage = mw.title.getCurrentTitle().subpageText
    
    --[[ If the subpage is a valid language code, check if a translation of the
        template exists in that language; if so, put it in langcode.
        ]]

    if ( mw.language.isKnownLanguageTag( subpage ) )
    then
        local translation = mw.title.new( template..'/'..subpage, 'Template' )
        if ( translation.id ~= 0)
        then
            langcode='/'..subpage
        end
    end

    -- Copy args pseudo-table to a proper table so we can feed it to expandTemplate
    local arguments = {}
    for k, v in pairs( frame.args ) do
        arguments[k] = v
    end

    -- Render the template
    return frame:expandTemplate{ title = template..langcode, args = arguments }
    
end

return p