Jump to content

Module:Lang-zh

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by JohnBlackburne (talk | contribs) at 18:49, 8 April 2014 (New test module). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local p = {
    language = require "Module:Language",

    fr = {
        args = {}
    }

}


function p.getParent()
    return p.fr
end


function p.Zh(frame)
-- Get the args. I don't know if there's a preferred way to do it but this works
    local args = {}
     local pframe = frame:getParent()
    for k, v in pframe:argumentPairs() do
        args[k] = v
    end
    local body = "" -- the output string
-- Get all the parameters into vars, or use "" if none is supplied (avoids seperate test for empty strings)
    local c = args["c"] or ""
    local s = args["s"] or ""
    local t = args["t"] or ""
    local hp = args["p"] or args["hp"] or ""
    local tp = args["tp"] or ""
    local w = args["w"] or ""
    local j = args["j"] or ""
    local cy = args["cy"] or ""
    local poj = args["poj"] or ""
    local zhu = args["zhu"] or ""
    local l = args["l"] or ""
    local no = args["links"] == "no"
    local t1 = args["first"] == "t"

    if (c > "" or (s > "" and s == t)) then -- if simplified and traditional the same they are merged into Chinese.
        c = c or s
        p.fr.args[1], p.fr.args[2] = "zh", c
        c = p.language.lang(p)
        if (no) then -- handle "links=no"
            body = body .. "Chinese: " .. c .. "; "
        else 
            body = body .. "[[Chinese language|Chinese]]: " .. c .. "; "
        end
    end
-- temporaries, used for reordering
    local sOut, tOut = "", ""

    if (s ~= t) then -- if they are the same should under Chinese
        if (s  > "") then
            p.fr.args[1], p.fr.args[2]  = "zh-Hans", s
            s = p.language.lang(p)
            if (t  > "") then -- only display full name if traditional also appears
                name = "simplified Chinese"
            else 
                name = "Chinese"
            end
            if (no) then -- handle "links=no"
                sOut = name .. ": " .. s .. "; "
            else
                sOut = "[[Simplified Chinese character|" .. name .."]]: " .. s .. "; "
            end
        end
        if (t  > "") then
            p.fr.args[1], p.fr.args[2]  = "zh-Hant", t
            t = p.language.lang(p)
            if (s  > "") then -- only display full name if simplified also appears
                name = "traditional Chinese"
            else 
                name = "Chinese"
            end
            if (no) then -- handle "links=no"
                tOut = name .. ":" .. t .. "; "
            else
                tOut = "[[Traditional Chinese character|" .. name .."]]: " .. t .. "; "
            end
        end
        if (t1) then -- order based on "first=t"
            body = body .. tOut .. sOut
        else
            body = body .. sOut .. tOut
        end
    end

    sOut = ""
    tOut = ""
    if (hp > "") then
            if (tp  > "") then -- only display full name if Tongyo pinyin also appears
                name = "Hanyu Pinyin"
            else 
                name = "pinyin"
            end
        if (no) then -- handle "links=no"
            sOut = name .. ": ''" .. hp .. "''; "
        else
            sOut = "[[pinyin|" .. name .. "]]: ''" .. hp .. "''; "
        end
    end
    if (tp  > "") then
        if (no) then -- handle "links=no"
            tOut = "Tongyong Pinyin: " .. tp .. "; "
        else
            tOut = "[[Tongyong Pinyin]]: " .. tp .. "; "
        end
    end
    if (t1) then -- order based on "first=t"
        body = body .. tOut .. sOut
    else
        body = body .. sOut .. tOut
    end

    if (w  > "") then
        if (no) then -- handle "links=no"
            body = body .. "Wade-Giles: " .. w .. "; "
        else
            body = body .. "[[Wade-Giles]]: " .. w .. "; "
        end
    end
    if (j  > "") then
        if (no) then -- handle "links=no"
            body = body .. "Jyutping: " .. j .. "; "
        else
            body = body .. "[[Jyutping]]: " .. j .. "; "
        end
    end
    if (cy  > "") then
        if (no) then -- handle "links=no"
            body = body .. "Yale: " .. cy .. "; "
        else
            body = body .. "[[Yale romanization|Yale]]: " .. cy .. "; "
        end
    end
    if (poj  > "") then
        if (no) then -- handle "links=no"
            body = body .. "Pe̍h-ōe-jī: " ..poj .. "; " 
        else
            body = body .. "[[Pe̍h-ōe-jī]]: " ..poj .. "; "
        end
    end
    if (zhu  > "") then
        if (no) then -- handle "links=no"
            body = body .. "Zhuyin Fuhao: " .. zhu .. "; "
        else
            body = body .. "[[Bopomofo|Zhuyin Fuhao]]: " .. zhu .. "; "
        end
    end
    if (l  > "") then
        body = body .. "literally: \"" .. l .. "\"; "
    end
    if (body > "") then -- check for empty string
        return string.sub(body, 1, -3) -- chop off final semicolon and space
    else --no named parameters; see if there's an unnamed first parameter
        c = pframe.args[1] or ""
        if (c > "") then -- if there is treat it as Chinese
            if (no) then -- handle "links=no"
                return "Chinese: " .. c
            else 
                return "[[Chinese language|Chinese]]: " .. c
            end
        end
        return body
    end
end

return p