From Afropedia.world
Jump to: navigation, search
Djehuti
Djehuti (talk | contribs) (1 revision imported)
m
 
Djehuti
Djehuti (talk | contribs) (Created Module:Zh for Chinese language support)
 
Line 1: Line 1:
require('Module:No globals')
-- Module:Zh
-- Handles Chinese language text display with traditional/simplified characters and romanization


local p = {}
local p = {}
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
["228 Incident"] = true,
["Chinese calendar"] = true,
["Lippo Centre, Hong Kong"] = true,
["Republic of China"] = true,
["Republic of China at the 1924 Summer Olympics"] = true,
["Taiwan"] = true,
["Taiwan (island)"] = true,
["Taiwan Province"] = true,
["Wei Boyang"] = true,
}


-- the labels for each part
function p.zh(frame)
local labels = {
    local args = frame:getParent().args
["c"] = "Chinese",
   
["s"] = "simplified Chinese",
    -- Get parameters
["t"] = "traditional Chinese",
    local traditional = args['t'] or args['traditional'] or ''
["p"] = "pinyin",
    local simplified = args['s'] or args['simplified'] or ''
["tp"] = "Tongyong Pinyin",
    local pinyin = args['p'] or args['pinyin'] or ''
["w"] = "Wade–Giles",
    local hanpin = args['hp'] or args['hanpin'] or ''
["j"] = "Jyutping",
    local wade = args['w'] or args['wade'] or ''
["cy"] = "Cantonese Yale",
    local literal = args['l'] or args['literal'] or ''
["poj"] = "Pe̍h-ōe-jī",
   
["zhu"] = "Zhuyin Fuhao",
    -- Determine which Chinese text to display
["l"] = "literally",
    local chinese_text = ''
}
    if simplified ~= '' then
 
        chinese_text = simplified
-- article titles for wikilinks for each part
    elseif traditional ~= '' then
local wlinks = {
        chinese_text = traditional
["c"] = "Chinese language",
    end
["s"] = "simplified Chinese characters",
   
["t"] = "traditional Chinese characters",
    -- Build romanization
["p"] = "pinyin",
    local romanization = hanpin or pinyin or wade
["tp"] = "Tongyong Pinyin",
   
["w"] = "Wade–Giles",
    -- Build output
["j"] = "Jyutping",
    local result = ''
["cy"] = "Yale romanization of Cantonese",
   
["poj"] = "Pe̍h-ōe-jī",
    if chinese_text ~= '' then
["zhu"] = "Bopomofo",
        result = '<span lang="zh" class="zh">' .. chinese_text .. '</span>'
}
    end
 
   
-- for those parts which are to be treated as languages their ISO code
    if romanization ~= '' then
local ISOlang = {
        if result ~= '' then
["c"] = "zh",
            result = result .. '; '
["t"] = "zh-Hant",
        end
["s"] = "zh-Hans",
        result = result .. '<i>' .. romanization .. '</i>'
["p"] = "zh-Latn-pinyin",
    end
["tp"] = "zh-Latn",
   
["w"] = "zh-Latn-wadegile",
    if literal ~= '' then
["j"] = "yue-jyutping",
        if result ~= '' then
["cy"] = "yue",
            result = result .. ', literally: '
["poj"] = "hak",
        end
["zhu"] = "zh-Bopo",
        result = result .. '"' .. literal .. '"'
}
    end
 
   
local italic = {
    return result
["p"] = true,
["tp"] = true,
["w"] = true,
["j"] = true,
["cy"] = true,
["poj"] = true,
}
-- Categories for different kinds of Chinese text
local cats = {
["c"] = "[[Category:Articles containing Chinese-language text]]",
["s"] = "[[Category:Articles containing simplified Chinese-language text]]",
["t"] = "[[Category:Articles containing traditional Chinese-language text]]",
}
 
function p.Zh(frame)
-- load arguments module to simplify handling of args
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
return p._Zh(args)
end
function p._Zh(args)
local uselinks = not (args["links"] == "no") -- whether to add links
local uselabels = not (args["labels"] == "no") -- whether to have labels
local capfirst = args["scase"] ~= nil
local t1 = false -- whether traditional Chinese characters go first
local j1 = false -- whether Cantonese Romanisations go first
local testChar
if (args["first"]) then
for testChar in mw.ustring.gmatch(args["first"], "%a+") do
if (testChar == "t") then
t1 = true
end
if (testChar == "j") then
j1 = true
end
end
end
if (t1 == false) then
local title = mw.title.getCurrentTitle()
t1 = t1st[title.text] == true
end
 
-- based on setting/preference specify order
local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
if (t1) then
orderlist[2] = "t"
orderlist[3] = "s"
end
if (j1) then
orderlist[4] = "j"
orderlist[5] = "cy"
orderlist[6] = "p"
orderlist[7] = "tp"
orderlist[8] = "w"
end
-- rename rules. Rules to change parameters and labels based on other parameters
if args["hp"] then
-- hp an alias for p ([hanyu] pinyin)
args["p"] = args["hp"]
end
if args["tp"] then
-- if also Tongyu pinyin use full name for Hanyu pinyin
labels["p"] = "Hanyu Pinyin"
end
if (args["s"] and args["s"] == args["t"]) then
-- Treat simplified + traditional as Chinese if they're the same
args["c"] = args["s"]
args["s"] = nil
args["t"] = nil
elseif (not (args["s"] and args["t"])) then
-- use short label if only one of simplified and traditional
labels["s"] = labels["c"]
labels["t"] = labels["c"]
end
 
local body = "" -- the output string
local params -- for creating HTML spans
local label -- the label, i.e. the bit preceeding the supplied text
local val -- the supplied text
-- go through all possible fields in loop, adding them to the output
for i, part in ipairs(orderlist) do
if (args[part]) then
-- build label
label = ""
if (uselabels) then
label = labels[part]
if (capfirst) then
label = mw.language.getContentLanguage():ucfirst(label)
capfirst = false
end
if (uselinks and part ~= "l") then
label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
end
label = label .. "&#58; "
end
-- build value
val = args[part]
if (cats[part]) then
-- if has associated category add it
val = cats[part] .. val
end
if (ISOlang[part]) then
-- add span for language if needed
params = {["lang"] = ISOlang[part], ["xml:lang"] = ISOlang[part]}
val = mw.text.tag({name="span",attrs=params, content=val})
elseif (part == "l") then
-- put literals in quotes
val = '"' .. val .. '"'
end
if (italic[part]) then
-- italicise
val = "''" .. val .. "''"
end
-- add both to body
body = body .. label .. val .. "; "
end
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 a first parameter, ignoring its name
if (args[1]) then
-- if there is treat it as Chinese
label = ""
if (uselabels) then
label = labels["c"]
if (uselinks) then
label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
end
label = label .. "&#58; "
end
-- default to show links and labels as no options given
val = cats["c"] .. args[1]
params = {["lang"] = ISOlang["c"], ["xml:lang"] = ISOlang["c"]}
val = mw.text.tag({name="span",attrs=params, content=val})
return label .. val
end
return ""
end
end
end


return p
return p

Latest revision as of 17:47, 19 January 2026

-- Module:Zh -- Handles Chinese language text display with traditional/simplified characters and romanization

local p = {}

function p.zh(frame)

   local args = frame:getParent().args
   
   -- Get parameters
   local traditional = args['t'] or args['traditional'] or 
   local simplified = args['s'] or args['simplified'] or 
   local pinyin = args['p'] or args['pinyin'] or 
   local hanpin = args['hp'] or args['hanpin'] or 
   local wade = args['w'] or args['wade'] or 
   local literal = args['l'] or args['literal'] or 
   
   -- Determine which Chinese text to display
   local chinese_text = 
   if simplified ~=  then
       chinese_text = simplified
   elseif traditional ~=  then
       chinese_text = traditional
   end
   
   -- Build romanization
   local romanization = hanpin or pinyin or wade
   
   -- Build output
   local result = 
   
   if chinese_text ~=  then
       result = '' .. chinese_text .. ''
   end
   
   if romanization ~=  then
       if result ~=  then
           result = result .. '; '
       end
       result = result .. '' .. romanization .. ''
   end
   
   if literal ~=  then
       if result ~=  then
           result = result .. ', literally: '
       end
       result = result .. '"' .. literal .. '"'
   end
   
   return result

end

return p