跳转到内容

模組:地区用词/testcases

维基百科,自由的百科全书
local p = {}
local variants = {
    cn = 'hans',
    hk = 'hant',
    mo = 'hant',
--    my = 'hans',
    sg = 'hans',
    tw = 'hant',
}
local lc = require( 'Module:WikitextLC' )

function p.main( frame )
    local args = frame.args
    local values = {}

    for region, defaultScript in pairs( variants ) do
        if args['zh-hans-' .. region] ~= '' or args['zh-hant-' .. region] ~= '' or args['zh-' .. region] ~= '' then
            values[region] = { script = defaultScript }
            if args['zh-hans-' .. region] ~= '' then
                values[region].hans = args['zh-hans-' .. region]
            elseif args['zh-' .. region] ~= '' then
                values[region].hans = lc.converted( args['zh-' .. region], 'zh-hans' )
            else
                values[region].hans = lc.converted( args['zh-hant-' .. region], 'zh-hans' )
            end
            if args['zh-hant-' .. region] ~= '' then
                values[region].hant = args['zh-hant-' .. region]
            elseif args['zh-' .. region] ~= '' then
                values[region].hant = lc.converted( args['zh-' .. region], 'zh-hant' )
            else
                values[region].hant = lc.converted( args['zh-hans-' .. region], 'zh-hant' )
            end
        end
    end

    local localName = {}
    for region, value in pairs( values ) do
        localName['zh-' .. region] = value[value.script]
    end
    localName = lc.selective( localName )

    local foreignName = {}
    for region, script in pairs( variants ) do
        local snippets = {}
        for valueRegion, value in pairs( values ) do
            if valueRegion ~= region then
                table.insert( snippets, lc.converted( args['name-' .. valueRegion] .. args.colon, 'zh-' .. region ) .. value[script] )
            end
        end
        foreignNameText = mw.text.listToText( snippets, lc.converted( args.comma, 'zh-' .. region ), lc.converted( args.lastComma, 'zh-' .. region ) )
        if foreignNameText ~= '' then
            foreignNameText = lc.converted( args.openBracket, 'zh-' .. region ) .. foreignNameText .. lc.converted( args.closeBracket, 'zh-' .. region )
        end
        foreignName['zh-' .. region] = foreignNameText
    end
    foreignName = lc.selective( foreignName )

    ret = ''
    if args.showLocal and args.showLocal ~= '' then
        ret = ret .. localName
    end
    if args.showForeign and args.showForeign ~= '' then
        ret = ret .. foreignName
    end
    return ret
end

return p