Difference between revisions of "Module:Assemble multilingual message"

From Northumberland Climbing
Jump to navigation Jump to search
module>Tacsipacsi
(fix language code of {{int}}, as guessing the page language won’t work on a page without a language subpage (i.e. the sandbox))
 
m (1 revision imported)
 
(No difference)

Latest revision as of 05:49, 31 July 2020

See m:Newsletters/Translation for usage instructions.


-- THIS IS BETA SOFTWARE. USE CAREFULLY AND TEST EXTENSIVELY.

local p = {}


function p.assembleMessage(frame)
     --[[ This function takes existing translations for a given page
        translated using the translate extension and puts them together to
        generate the wikicode ready to be delivered by [Global message delivery]
    ]]

    -- Initialize the message's static content

    local message = '<pre>{{subst:#switch:{{subst:CONTENTLANG}}'

    --[[ Build the list of language codes for which there is a translation,
        and remove English if it was mistakenly added since we already include
        it as default.
        We can't automatically list all available translations of the issue, so
        we need to input them manually as part of the module's call.
    ]]
    
    translations = {}
    
    for key, value in pairs( frame.args ) do
        if ( mw.language.isKnownLanguageTag( value ) and value ~= 'en' )
        then
            translations[value] = value
        end
    end
    
    -- Add English as default
    
    translations['#default'] = 'en'

    -- Loop through the languages to assemble the available translations

    for switchKey, langCode in pairs( translations ) do
        
        --[[ TODO: add existence check of the translation in case of human
            error. Ignore the incorrect language code and give warning.
        ]]
        
        -- Add new switch key, and language and directionality metadata
        
        local direction = mw.language.new( langCode ):getDir()
        
        --[[ TODO: add switch keys for languages that have a fallback language
            for which we do have a translation, instead of English.
        ]]
    
        message = message .. '|' .. switchKey .. '=<div class="plainlinks mw-content-' .. direction .. '" lang="' .. langCode .. '" dir="' .. direction .. '">'
        
        -- Get the translation's content
        
        local page = frame.args['page']
        
        local content = mw.title.new( page .. '/' .. langCode ):getContent()
        
        --[[ Clean up stuff we don't want to include (headers, footers, etc.)
            There's probably a cleaner way to do this but for now it'll work.
            TODO: add error checks.
        ]]
    
        local startSection, startIndex = mw.ustring.find( content, '<section begin="' .. frame.args['marker'] .. '"/>', 1, true )
        local endIndex = mw.ustring.find( content, '<section end="' .. frame.args['marker'] .. '"/>', 1, true )
    
        content = mw.ustring.sub( content, startIndex +1 , endIndex -1 )
        
        content = mw.ustring.gsub( content, '{{int|', '{{int|lang=' .. langCode .. '|' )
        
        message = message .. content .. '</div>'
        
    end
    
    -- Add footer static content
    
    message = message .. '}} ~~~~~</pre>'
    
    -- Hide nowiki tags from the preprocessor
    
    message = mw.ustring.gsub ( message, '<nowiki>(.-)</nowiki>', '&lt;nowiki&gt;%1&lt;/nowiki&gt;' )
    
    -- Preprocess to make <pre> work
    
    message = frame:preprocess( message )
    
    -- We're done

    return message

end


return p