Module:Lang-zh: Difference between revisions

Content deleted Content added
try another fix
Publishing User:Northern Moonlight's implementation of new fields to specify locale-specific character glyphs according to the IETF standard
 
(76 intermediate revisions by 20 users not shown)
Line 1:
require('strict')
local p = {
fr = {
args = {}
}
 
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
local labels = {
["c"] = "Chinese",
["s"] = "simplified Chinese",
["t"] = "traditional Chinese",
["p"] = "pinyin",
["tp"] = "Tongyong Pinyin",
["w"] = "Wade–Giles",
["j"] = "Jyutping",
["cy"] = "Cantonese Yale",
["sl"] = "Sidney Lau",
["poj"] = "Pe̍h-ōe-jī",
["tl"] = "Tâi-lô",
["zhu"] = "Zhuyin Fuhao",
["l"] = "lit.",
["tr"] = "trans.",
}
 
-- article titles for wikilinks for each part
function p.getParent()
local wlinks = {
return p.fr
["c"] = "Chinese language",
end
["s"] = "simplified Chinese characters",
["t"] = "traditional Chinese characters",
["p"] = "pinyin",
["tp"] = "Tongyong Pinyin",
["w"] = "Wade–Giles",
["j"] = "Jyutping",
["cy"] = "Yale romanization of Cantonese",
["sl"] = "Sidney Lau romanisation",
["poj"] = "Pe̍h-ōe-jī",
["tl"] = "Tâi-uân Lô-má-jī Phing-im Hong-àn",
["zhu"] = "Bopomofo",
["l"] = "Literal translation",
["tr"] = "Translation",
}
 
-- for those parts which are to be treated as languages their ISO code
local ISOlang = {
["c"] = "zh",
["t"] = "zh-Hant",
["s"] = "zh-Hans",
["p"] = "zh-Latn",
["tp"] = "zh-Latn-tongyong",
["w"] = "zh-Latn-wadegile",
["j"] = "yue-Latn-jyutping",
["cy"] = "yue-Latn",
["sl"] = "yue-Latn",
["poj"] = "nan-Latn",
["tl"] = "nan-Latn-tailo",
["zhu"] = "zh-Bopo",
}
 
local italic = {
["p"] = true,
["tp"] = true,
["w"] = true,
["j"] = true,
["cy"] = true,
["sl"] = true,
["poj"] = true,
["tl"] = true,
}
 
local superscript = {
["w"] = true,
["sl"] = 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
-- Get the args. I don't know if there's a preferred way to do it but this works
local getArgs = require('Module:Arguments').getArgs
local args = {}
local pframe = frame:getParent()
local args = getArgs(frame)
for k, v in pframe:argumentPairs() do
return p._Zh(args)
args[k] = v
end
local body = "" -- the output string
function p._Zh(args)
-- Get all the parameters into vars, or use "" if none is supplied (avoids seperate test for empty strings)
if args["link"] local c =then args["clinks"] or= args["link"]; end
if args["label"] local s =then args["slabels"] or= args["label"]; end
local t = args["t"] or ""
local hpuselinks = args["plinks"] or~= args["hpno"] or-- whether ""to add links
local tpuselabels = args["tplabels"] or~= "no" -- whether to have labels
local wcapfirst = args["wscase"] or~= ""nil
local out = nil -- which term to put before the brackets
local j = args["j"] or ""
local usebrackets = 0 -- whether to have bracketed terms
local cy = args["cy"] or ""
local pojnumargs = args["poj"] or ""0
local regionalvariant = nil
local zhu = args["zhu"] or ""
local l = args["l"] or ""
local no = if args["linksout"] == "no"then
local t1 out = args["firstout"] == "t"
usebrackets = 1
end
 
local t1 = false -- whether traditional Chinese characters go first
local j1 = false -- whether Cantonese Romanisations go first
local poj1 = false -- whether Hokkien 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
if (testChar == "poj") then
poj1 = 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
if (c > "" or (s > "" and s == t)) then -- if simplified and traditional the same they are merged into Chinese.
local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "sl", "poj", "tl", "zhu", "l", "tr"}
c = c or s
if (t1) then
p.fr.args[1], p.fr.args[2] = "zh", c
orderlist[2] = "t"
c = mw.language.fetchLanguageName(p)
orderlist[3] = "s"
if (no) then -- handle "links=no"
end
body = body .. "Chinese: " .. c .. "; "
if (j1) then
else
orderlist[4] = "j"
body = body .. "[[Chinese language|Chinese]]: " .. c .. "; "
orderlist[5] = "cy"
end
orderlist[6] = "sl"
end
orderlist[7] = "p"
-- temporaries, used for reordering
orderlist[8] = "tp"
local sOut, tOut = "", ""
orderlist[9] = "w"
end
if (poj1) then
orderlist[4] = "poj"
orderlist[5] = "tl"
orderlist[6] = "p"
orderlist[7] = "tp"
orderlist[8] = "w"
orderlist[9] = "j"
orderlist[10] = "cy"
orderlist[11] = "sl"
end
 
-- rename rules. Rules to change parameters and labels based on other parameters
if (s ~= t) then -- if they are the same should under Chinese
if (s > args["hp")] then
-- hp an alias for p ([hanyu] pinyin)
p.fr.args[1], p.fr.args[2] = "zh-Hans", s
args["p"] = args["hp"]
s = mw.language.fetchLanguageName(p)
end
if (t > "") then -- only display full name if traditional also appears
if args["tp"] then
name = "simplified Chinese"
-- if also Tongyu pinyin use full name for Hanyu pinyin
else
labels["p"] = "Hanyu Pinyin"
name = "Chinese"
end
end
if (no) then -- handle "links=no"
-- specify traditional Chinese variants
sOut = name .. ": " .. s .. "; "
if args["t_hk"] then
else
args["t"] = args["t_hk"]
sOut = "[[Simplified Chinese character|" .. name .."]]: " .. s .. "; "
regionalvariant = "HK"
end
elseif args["t_tw"] then
end
args["t"] = args["t_tw"]
if (t > "") then
regionalvariant = "TW"
p.fr.args[1], p.fr.args[2] = "zh-Hant", t
end
t = mw.language.fetchLanguageName(p)
if (s > "") then -- only display full name if simplified also appears
if (args["s"] and args["s"] == args["t"]) then
name = "traditional Chinese"
-- Treat simplified + traditional as Chinese if they're the same
else
args["c"] = args["s"]
name = "Chinese"
args["s"] = nil
end
args["t"] = nil
if (no) then -- handle "links=no"
if out == "s" or out == "t" then
tOut = name .. ":" .. t .. "; "
out = "c"
else
end
tOut = "[[Traditional Chinese character|" .. name .."]]: " .. t .. "; "
elseif (not (args["s"] and args["t"])) then
end
-- use short label if only one of simplified and traditional
end
labels["s"] = labels["c"]
if (t1) then -- order based on "first=t"
labels["t"] = labels["c"]
body = body .. tOut .. sOut
end
else
if out then
body = body .. sOut .. tOut
for i, v in ipairs (orderlist) do -- shift `out` to the beginning of the order list
end
if v == out endthen
table.remove(orderlist, i)
table.insert(orderlist, 1, v)
break
end
end
end
 
if (out == "c" and args["s"]) then usebrackets = 2; 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
 
local body = "" -- the output string
if (w > "") then
local params -- for creating HTML spans
if (no) then -- handle "links=no"
local label -- the label, i.e. the bit preceeding the supplied text
body = body .. "Wade-Giles: " .. w .. "; "
local val -- the supplied text
else
body = body .. "[[Wade-Giles]]: " .. w .. "; "
-- go through all possible fields in loop, adding them to the output
end
for i, part in ipairs(orderlist) do
end
if (j > ""args[part]) then
numargs = numargs + 1
if (no) then -- handle "links=no"
-- build label
body = body .. "Jyutping: " .. j .. "; "
label = ""
else
if (uselabels) then
body = body .. "[[Jyutping]]: " .. j .. "; "
label = labels[part]
end
if (capfirst) then
end
label = mw.language.getContentLanguage():ucfirst(label)
if (cy > "") then
capfirst = false
if (no) then -- handle "links=no"
end
body = body .. "Yale: " .. cy .. "; "
if (uselinks and part ~= "l" and part ~= "tr") then
else
body label = body"[[" .. "wlinks[[Yale romanization|Yalepart]]: .. "|" .. cylabel .. "; ]]"
end
if (part == "l" or part == "tr") then
end
label = "<abbr title=\"" .. wlinks[part] .. "\"><small>" .. label .. "</small></abbr>"
if (poj > "") then
else
if (no) then -- handle "links=no"
label = label .. "&colon;"
body = body .. "Pe̍h-ōe-jī: " ..poj .. "; "
end
else
label = label .. " "
body = body .. "[[Pe̍h-ōe-jī]]: " ..poj .. "; "
end
-- build value
end
val = args[part]
if (zhu > "") then
if (cats[part]) and mw.title.getCurrentTitle().namespace == 0 then
if (no) then -- handle "links=no"
-- if has associated category AND current page in article namespace, add category
body = body .. "Zhuyin Fuhao: " .. zhu .. "; "
val = cats[part] .. val
else
end
body = body .. "[[Bopomofo|Zhuyin Fuhao]]: " .. zhu .. "; "
if (ISOlang[part]) then
end
-- add span for language if needed
end
params = {["lang"] = ISOlang[part] .. (regionalvariant and "-" .. regionalvariant or "")}
if (l > "") then
val = mw.text.tag({name="span",attrs=params, content=val})
body = body .. "literally: \"" .. l .. "\"; "
elseif (part == "l") then
end
local terms = ""
if (body > "") then -- check for empty string
-- put individual, potentially comma-separated glosses in single quotes
return string.sub(body, 1, -3) -- chop off final semicolon and space
-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
else --no named parameters; see if there's an unnamed first parameter
for term in val:gmatch("[^;,]+") do
c = pframe.args[1] or ""
term = mw.text.trim(term, "%s\"'")
if (c > "") then -- if there is treat it as Chinese
terms = terms .. "&apos;" .. term .. "&apos;, "
if (no) then -- handle "links=no"
end
return "Chinese: " .. c
val = string.sub(terms, 1, -3)
else
elseif (part == "tr") then
return "[[Chinese language|Chinese]]: " .. c
-- put translations in double quotes
end
-- (first strip leading and trailing spaces and quotes, including bold/italic markup)
end
val = mw.text.trim(val, "%s\"'")
return body
val = "&quot;" .. val .. "&quot;"
end
end
if (italic[part]) then
-- italicise
val = "<i>" .. val .. "</i>"
end
if string.match(val, "</?sup>") then val = val.."[[Category:Pages using template Zh with sup tags]]" end
if (superscript[part]) then
-- superscript
val = val:gsub("(%d)", "<sup>%1</sup>"):gsub("(%d)</sup>%*<sup>(%d)", "%1*%2"):gsub("<sup><sup>([%d%*]+)</sup></sup>", "<sup>%1</sup>")
end
-- add both to body
if numargs == usebrackets then
-- opening bracket after the `out` term
body = body .. label .. val .. " ("
else
body = body .. label .. val .. "; "
end
end
end
if (body > "") then -- check for empty string
body = string.sub(body, 1, -3) -- chop off final semicolon and space
if out and numargs > usebrackets then
-- closing bracket after the rest of the terms
body = body .. "&rpar;"
end
return body
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 .. "&colon; "
end
-- default to show links and labels as no options given
if mw.title.getCurrentTitle().namespace == 0 then
-- if current page in article namespace
val = cats["c"] .. args[1]
else
val = args[1]
end
params = {["lang"] = ISOlang["c"]}
val = mw.text.tag({name="span",attrs=params, content=val})
return label .. val
end
return ""
end
end