Fandom Developers Wiki
Register
Advertisement
Documentation icon Module documentation

The documentation for this module is missing. Click here to create it.

-- used within [[Template:Language list]] - see docs there for usage info
-- <nowiki>
local p = {}

-- For manual filling up the language list
function p.makeLanguageList(frame)
    local args = frame:getParent().args
    local settings = {
        collapse = args.collapse == 'yes',
        nocat = args.nocat and args.nocat ~= 'false',
    }

    if args['source-js'] then
        return p.listFromJavascript(args['source-js'], settings)
    elseif args['source-json'] then
        return p.listFromJsonGeneric(args['source-json'], settings)
    else
        return p.listToWikitext(args, settings)
    end
end

-- For automatic filling up the language list
-- with languages from [[I18n-js]] JSON data pages
function p.listFromJson(frame)
    -- Get the current title
    local title = mw.title.getCurrentTitle()
    if title.namespace ~= 0 then
        -- Return if not in main namespace
        return 'Namespace not supported.'
    else
        -- Get the translation data page
        local pagename = title.rootText
        if pagename == 'I18n-js' then
            pagename = 'I18nEdit'
        end
        local i18n = mw.title.new('MediaWiki:Custom-' .. pagename .. '/i18n.json')

        if i18n.exists then
            -- Get content of translation page and find all languages in it
            local content = i18n:getContent()
            -- indent is 4 spaces for i18n-js pages
            local indent = '    '

            -- Return the language list and edit link
            return p.keysToList(content, indent) .. '[[Special:BlankPage/I18nEdit/' .. pagename .. '|' .. frame.args[1] .. ']]'
        else
            -- Return if the translation page does not exist
            return 'Error: Page "MediaWiki:Custom-' .. pagename .. '/i18n.json" does not exist.'
        end
    end
end

-- For automatic filling up the language list
-- with languages from a JSON-like page
function p.listFromJsonGeneric(title, settings)
    local page = mw.title.new(title)
    local start = '{\n([ \t]+)"'

    if page.exists then
        -- Get content of JSON page and find all languages in it
        local content = page:getContent()
        local indent = content:match(start)

        if indent == nil then
            return 'Error: Page "' .. title .. '" does not seem to be JSON-like.'
        end

        -- Return the language list
        return p.keysToList(content, indent, settings)
    else
        -- Return if the translation page does not exist
        return 'Error: Page "' .. title .. '" does not exist.'
    end
end

-- For automatic filling up the language list
-- with languages from a JavaScript code page
function p.listFromJavascript(title, settings)
    local page = mw.title.new(title)
    local start = '\n([ \t]+)// ?[Ll]anguage list %- start'
    local stop = '// ?[Ll]anguage list %- stop'

    if page.exists then
        -- Get content of code page and find all languages in it
        local content = page:getContent()

        local startPos = content:find(start)
        local _, stopPos = content:find(stop, startPos)

        if startPos == nil or stopPos == nil then
            return 'Error: Page "' .. title .. '" does not contain language list delimiters.'
        end

        local langContent = content:sub(startPos, stopPos)
        local indent = content:match(start)

        -- Return the language list
        return p.keysToList(langContent, indent, settings)
    else
        -- Return if the translation page does not exist
        return 'Error: Page "' .. title .. '" does not exist.'
    end
end

-- Search JSON data or JavaScript object and add language keys to list
function p.keysToList(content, indent, settings)
    settings = settings or {}

    -- Find all language codes
    local matches = content:gmatch('\n' .. indent .. '["\']?([a-z-]+)')
    local list = {}

    for lang in matches do
        if lang ~= 'qqq' then
            list[#list + 1] = lang
        end
    end

    -- If there are more than 10 languages automatically collapse the list
    if #list > 10 then
        settings.collapse = true
    end

    -- Return the language list
    return p.listToWikitext(list, settings)
end

-- List generator
function p.listToWikitext(args, settings)
    local list = mw.html.create('ul'):addClass('language-list')
    local link = args.link

    -- style as plain list - ideally, styling would be done in MediaWiki:Common.css
    list:css('list-style', 'none')

    -- add collapsible classes if requested
    if settings.collapse then
        list:addClass('mw-collapsible'):addClass('mw-collapsed')
    end

    for index, value in ipairs(args) do
        value = mw.text.trim(value)
        local listItem = list:tag('li')
        local lang = mw.language.fetchLanguageName(value)
        local text = (lang ~= '' and lang) or value

        -- use either a specific or generic link
        -- if generic, substitute $1 for the current language code
        if  args[value] then
            text = '[[' .. args[value] .. '|' .. text .. ']]'
        elseif link then
            text = '[[' .. mw.ustring.gsub(link, '$1', value) .. '|' .. text .. ']]'
        end

        listItem:wikitext(text)

        -- for recognised languages, append the language code in brackets and add category
        if lang ~= '' then
            listItem:tag('span')
                :css('font-size', '0.8em')
                :wikitext(' (' .. value .. ')')
            if not settings.nocat then
                listItem:wikitext('[[Category:Translated scripts/' .. lang .. ']]')
            end
        end
    end

    return tostring(list)
end

-- legacy export names - make sure they're not used, then remove
p.auto = p.listFromJson
p.main = p.listToWikitext

return p
Advertisement