Documentation for this module may be created at Module:Template translation/sandbox/doc

local this = {}

function this.checkLanguage(subpage)
    --[[Check first if there's an any invalid character that would cause the
        mw.language.isKnownLanguageTag function() to throw an exception:
        - all ASCII controls in [\000-\031\127],
        - double quote ("), sharp sign (#), ampersand (&), apostrophe ('),
        - slash (/), colon (:), semicolon (;), lower than (<), greater than (>),
        - brackets and braces ([, ], {, }), pipe (|), backslash (\\)
        All other characters are accepted, including space and all non-ASCII
        characters (including \192, which is invalid in UTF-8).
    --]]
    if mw.language.isValidCode(subpage) and mw.language.isKnownLanguageTag(subpage)
    --[[However "SupportedLanguages" are too restrictive, as they discard many
        valid BCP47 script variants (only because MediaWiki still does not
        define automatic transliterators for them, e.g. "en-dsrt" or
        "fr-brai" for French transliteration in Braille), and country variants,
        (useful in localized data, even if they are no longer used for
        translations, such as zh-cn, also useful for legacy codes).
        We want to avoid matching subpagenames containing any uppercase letter,
        (even if they are considered valid in BCP 47, in which they are
        case-insensitive; they are not "SupportedLanguages" for MediaWiki, so
        they are not "KnownLanguageTags" for MediaWiki).
        To be more restrictive, we exclude any character that is not
        ASCII and not a lowercase letter, minus-hyphen, or digit, and any code
        that does not start by a letter or does not finish by a letter or digit.
        of that has more than 8 characters between hyphens, or has two hyphens.
    --]]
    or  string.find(subpage, "^[%l][%-%d%l]*[%d%l]$") ~= nil
    and string.find(subpage, "[%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l][%d%l]") == nil
    and string.find(subpage, "%-%-") == nil
    then
        return subpage
    end
    -- Otherwise there's currently no known language subpage
    return default
end

--[[Get the language of the given page, if it is a translation.
--]]
function this.getTitleLanguage(title, default)
    --[[This code does not work in all namespaces where the Translate tool works:
    local subpage = title.subpageText
    ]]
    local titleparts = mw.text.split(title.fullText, '/')
    local subpage = titleparts[#titleparts]
    return this.checkLanguage(subpage, default)
end

--[[Get the language of the current page, if it is a translation.
--]]
function this.getCurrentLanguage(default)
    return this.getTitleLanguage(mw.title.getCurrentTitle(), default)
end

--[[Get the language of the current frame, if it is a translation.
--]]
function this.getFrameLanguage(frame)
    local args = frame.args
    local default = args['default'] or ''
    return this.getTitleLanguage(frame:getParent():getTitle(), default)
end

--[[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.
    ]]
function this.renderTranslatedTemplate(frame)
    local args = frame.args
    local pagename = args['template']
    
    --[[Check whether the pagename is actually in the Template namespace, or
        if we're transcluding a main-namespace page.
        (added for backward compatibility of Template:TNT)
        ]]
    local title
    local namespace = args['namespace'] or ''
    if namespace ~= '' -- Checks for namespace parameter for custom ns.
    then
        title = mw.title.new(pagename, namespace) or {} -- Costly
    else -- Supposes that set page is in ns10.
    	namespace = 'Template'
        title = mw.title.new(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 = mw.title.new(pagename, namespace) or {} -- Costly
        end
    end
    
    -- Get the last subpage and check if it matches a known language code.
    local subpage = args['uselang'] or ''
    if subpage == ''
    then
        subpage = this.getFrameLanguage(frame)
    end
    if subpage == ''
    then
        -- Check if a translation of the pagename exists in English
        local newtitle = mw.title.new(pagename .. '/' .. 'en', namespace) or {} -- Costly
        -- Use the translation when it exists
        if (newtitle.id or 0) ~= 0
        then
            title = newtitle
        end
    else
        -- Check if a translation of the pagename exists in that language
        local newtitle = mw.title.new(pagename .. '/' .. subpage, namespace) or {} -- Costly
        if (newtitle.id or 0) == 0
        then
            -- Check if a translation of the pagename exists in English
            newtitle = mw.title.new(pagename .. '/' .. 'en', namespace) or {} -- Costly
        end
        -- Use the translation when it exists
        if (newtitle.id or 0) ~= 0
        then
            title = newtitle
        end
    end
    
    -- 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 .. ']]'
    end
    
    -- Copy args pseudo-table to a proper table so we can feed it to expandTemplate.
    -- Then render the pagename.
    local arguments = {}
    for k, v in pairs((frame:getParent() or {}).args) do
        -- numbered args >= 1 need to be shifted
    	local n = tonumber(k) or 0
    	if n > 0
    	then
    		if n >= 2
    		then
                arguments[n - 1] = v
            end
        else
            arguments[k] = v
        end
    end
    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
    
    return frame:expandTemplate{title = ':' .. title.prefixedText, args = arguments}
end

return this