Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Multilingual description: Difference between revisions

From Neodyland Wiki
m a couple of remaps for old codes; track all deprecated language codes used in multilingual descriptions (when they could be remapped)
m parentframe.args needs to be cloned to a standard Lua array (it is not modifiable, it is just a shadow interface to a PHP array)
Line 76: Line 76:


function p.mld(frame)
function p.mld(frame)
     local args = (frame:getParent() or {}).args or {}
     local args = mw.clone((frame:getParent() or {}).args) or {}
     return _mld(args)
     return _mld(args)
end
end

Revision as of 11:25, 17 April 2015

Module documentation[ create · purge ]
local p = {}

local sortedKnownLanguageTags = require('Module:Multilingual description/sort')

local function addDescription(descriptions, lang, description)
    if type(description) == 'string' then
        description = mw.text.trim(description)
        if description:len() > 0 then
            table.insert(descriptions, mw.getCurrentFrame():expandTemplate{
                title = 'Ls',
                args = {
                    lang,
                    description,
                    classes = 'description'
                }
            })
        end
    end
end

local function addRemappedTracking(descriptions)
    table.insert(descriptions, '[[Category:Multilingual descriptions using deprecated language codes]]')
end

local function _mld(args)
    local descriptions = {}
    -- Remap some "valid" language codes that are still unknown, but are known by another code
    -- in order to get a visible language name (and if possible, BCP47 conformance)!
    local remap = {
        ['als'] = 'gsw', -- legacy broken codes (but known) to changed new code (also known, but conforming to BCP47)
        ['bat-smg'] = 'sgs', -- same remark
        ['be-x-old'] = 'be-tarask', -- same remark
        ['bh'] = 'bho', -- same remark
        ['bu'] = 'my', -- same remark
        ['no'] = 'nb', -- same remark
        -- 'nrm' should be remapped here, but there's no known target for now
        ['fiu-vro'] = 'vro', -- same remark
        ['simple'] = 'en', -- same remark
        ['zh-classical'] = 'lzh', -- same remark
        ['zh-min-nan'] = 'nan', -- same remark
        ['zh-wuu'] = 'wuu', -- same remark
        ['zh-yue'] = 'yue', -- same remark
        ['ko-kr'] = 'ko', -- both codes are conforming and supported, only the shortest one is known
        ['sr-cyrl'] = 'sr-ec', -- this alternate known code is non-standard and in fact not supported, but has a correct native name
        ['sr-latn'] = 'sr-el', -- same remark
    }
    local remapped = false
    for cur,alt in pairs(remap) do
        if args[cur] and not(mw.language.isSupportedLanguage(cur) and mw.language.isKnownLanguageTag(cur))
                and (mw.language.isSupportedLanguage(alt) and mw.language.isKnownLanguageTag(alt)) then
            remapped = true
            if args[alt] == nil then -- only if this does not conflict
                args[alt] = args[cur] -- set key for alternate known language
            end
            args[cur] = nil -- remove the standard unknown key
        end
    end
    -- First all known languages in order if they have description
    for _, lang in ipairs(sortedKnownLanguageTags) do
        if args[lang] then
            addDescription(descriptions, lang, args[lang])
            args[lang] = nil
        end
    end
    -- Append other unknown languages, but only if they are supported
    for lang, description in pairs(args) do
        if type(lang) == 'string' and mw.language.isSupportedLanguage(lang) then
            addDescription(descriptions, lang, description)
        end
    end
    if remapped then
        addRemappedTracking(descriptions)
    end
    return table.concat(descriptions)
end

function p.mld(frame)
    local args = mw.clone((frame:getParent() or {}).args) or {}
    return _mld(args)
end

function p.runTests()
    local input = {
        [1] = 'One?', -- discarded
        unsupported = 'What?', -- discarded
        en = ' ', -- empty description after trimming (discarded)
        als = 'GSW', -- will be remapped
        ['en-gb'] = 'EN-GB ', -- trimming at end
        ['en-ca'] = 'EN-CA <nowiki/>',
        de = ' DE', -- trimming at start
        fr = ' FR ', -- trimming both ends
        rue = 'RUE',
        ru = 'RU',
        ko = 'KO',
        ja = 'JA',
        zh = 'ZH',
        he = 'HE',
        ur = 'UR',
        ar = 'AR',
        dv = 'DV',
    }
    local expect = {}
    --[=[
    This is the exact order to expect according to native language names,
    and after discarding empty descriptions or unsupported language codes.
    --]=]
    addDescription(expect, 'gsw', 'GSW') --[[Alemannisch]] -- remapped
    addDescription(expect, 'en-gb', 'EN-GB') --[[British English]]
    addDescription(expect, 'en-ca', 'EN-CA <nowiki/>') --[[Canadian English]]
    addDescription(expect, 'de', 'DE') --[[Deutsch]]
    addDescription(expect, 'fr', 'FR') --[[français]]
    addDescription(expect, 'rue', 'RUE') --[[русиньскый]]
    addDescription(expect, 'ru', 'RU') --[[русский]]
    addDescription(expect, 'ko', 'KO') --[[한국어]]
    addDescription(expect, 'ja', 'JA') --[[日本語]]
    addDescription(expect, 'zh', 'ZH') --[[中文]]
    addDescription(expect, 'he', 'HE') --[[עברית]]
    addDescription(expect, 'ur', 'UR') --[[اردو]]
    addDescription(expect, 'ar', 'AR') --[[العربية]]
    addDescription(expect, 'dv', 'DV') --[[ދިވެހިބަސް]]
    addRemappedTracking(expect)
    expect = table.concat(expect)
    -- mw.log(expect)
    return _mld(input) == expect
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.