FANDOM


local l = {}
 
local function escRx(text, spaces)
    text = mw.ustring.gsub(text, '%\\', '\\\\')
    text = mw.ustring.gsub(text, '%(', '\\(')
    text = mw.ustring.gsub(text, '%)', '\\)')
    text = mw.ustring.gsub(text, '%.', '\\.')
    text = mw.ustring.gsub(text, '%+', '\\+')
    text = mw.ustring.gsub(text, '%*', '\\*')
    text = mw.ustring.gsub(text, '%?', '\\?')
    text = mw.ustring.gsub(text, '%^', '\\^')
    text = mw.ustring.gsub(text, '%$', '\\$')
    text = mw.ustring.gsub(text, '%-', '\\-')
    text = mw.ustring.gsub(text, '/', '\\/')
    text = mw.ustring.gsub(text, '=', '\\=')
    text = mw.ustring.gsub(text, '!', '\\!')
    text = mw.ustring.gsub(text, ':', '\\:')
    if spaces then text = mw.ustring.gsub(text, ' ', '_') end
    return text
end
local function makeLink(root, lang, text, exists)
    local page = root
    if lang then page = page .. '/' .. lang end
    if exists then
        return '[['..page..'|'..text..']]'
    else
        root = mw.ustring.gsub(root, '^:', '')
        return '<span class="redlink">['..tostring(mw.uri.fullUrl( page, 'action=edit&preload='..root ))..' '..text..']</span>'
    end
end
function l.langs(frame)
    local title, root = mw.title.getCurrentTitle()
    if mw.ustring.find(title.subpageText, '[a-z-]+') and mw.language.fetchLanguageName(title.subpageText) ~= '' then
        root = title.baseText
    else
        root = title.text
    end
 
    local args = require('Dev:Arguments').getArgs(frame, {
    	trim = true,
    	removeBlanks = true,
    	frameOnly = true,
    	readOnly = true
    })
    local rootLang = mw.ustring.lower(args['root'] or 'en')
 
    local langs = {}
    for i,v in ipairs(args or {}) do
        v = mw.ustring.lower(mw.text.trim(v or ''))
        if v ~= '' then langs[v] = false end
    end
 
    local existing = frame:preprocess('\n{{#dpl:namespace='..title.nsText..'|titleregexp=^'..escRx(root, true)..'\\/[a-z-]+$|replaceintitle=/^'..escRx(root, false)..'\\//,|skipthispage=no|format=¦,%TITLE%¦|noresultsheader=¦¦}}')
    mw.log('='..existing..'=')
    existing = select(3, mw.ustring.find(existing, '^%s*%|([%|a-z-]*)%|%s*$'))
    mw.log('='..existing..'=')
    if existing then
        existing = mw.text.split(existing, '|')
        mw.log(existing, #existing)
        for i,v in ipairs(existing or {}) do
            mw.log(i,v)
            v = mw.ustring.lower(mw.text.trim(v or ''))
            if v ~= '' then langs[v] = true end
        end
    end
    local ordered = {}
    for k,v in pairs(langs) do
        if k ~= rootLang then
            ordered[#ordered+1] = k
        end
    end
    table.sort(ordered)
 
    local separator = frame:preprocess('{{int:pipe-separator}}')
 
    local div = mw.html.create('div'):addClass(args['class'])
    div:tag('strong'):wikitext(frame:preprocess('{{int:oasis-interlang-languages}}')):done():wikitext(' ')
 
    root = mw.ustring.gsub(':' .. title.nsText .. ':' .. root, '^::', '')
    div:wikitext(makeLink(root, nil, mw.language.fetchLanguageName(rootLang), true))
    for _,lang in ipairs(ordered) do
        local name = mw.language.fetchLanguageName(lang)
        div:wikitext(separator):wikitext(makeLink(root, lang, name, langs[lang]))
    end
 
    return div
end
 
return l

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.