Module:Lang-zh: Difference between revisions

Content deleted Content added
forgot the function used at end, inline it there too, rm unnecessary check (only one param so no chance of 'no' being set), change to use data tables for consistency
Publishing User:Northern Moonlight's implementation of new fields to specify locale-specific character glyphs according to the IETF standard
 
(56 intermediate revisions by 20 users not shown)
Line 1:
require('strict')
 
local p = {}
 
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
["228 Incident"] = true,
["Bao'an County"] = true,
["Chinese calendar"] = true,
["Lippo Centre, Hong Kong"] = true,
Line 25 ⟶ 26:
["j"] = "Jyutping",
["cy"] = "Cantonese Yale",
["sl"] = "Sidney Lau",
["poj"] = "Pe̍h-ōe-jī",
["tl"] = "Tâi-lô",
["zhu"] = "Zhuyin Fuhao",
["l"] = "literallylit.",
["tr"] = "trans.",
}
 
Line 40 ⟶ 44:
["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",
}
 
Line 49 ⟶ 57:
["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 = {
Line 58 ⟶ 92:
 
function p.Zh(frame)
-- load arguments module to simplify handling of args
local pframe = frame:getParent()
local argsgetArgs = pframerequire('Module:Arguments').argsgetArgs
local args = getArgs(frame)
return p._Zh(args)
end
function p._Zh(args)
localif noargs["link"] =then args["links"] == args["nolink"]; -- whether to add linksend
if args["label"] then args["labels"] = args["label"]; end
local t1 -- whether traditional Chinese characters go first
iflocal uselinks = (args["firstlinks"] ~== nil)"no" -- whether to add thenlinks
local uselabels = args["labels"] ~= "no" -- whether to have labels
local capfirst = args["scase"] ~= nil
local out = nil -- which term to put before the brackets
local usebrackets = 0 -- whether to have bracketed terms
local numargs = 0
local regionalvariant = nil
if args["out"] then
out = args["out"]
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
else
t1 = args["first"] == "t"
end
 
-- based on setting/preference specify order
local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "sl", "poj", "tl", "zhu", "l", "tr"}
local orderlist
if (t1) then
orderlist[2] = {"c", "t", "s", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
orderlist[3] = "s"
else
orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "poj", "zhu", "l"}
end
if (j1) then
orderlist[4] = "j"
-- rename rules. Rules to change parameters based on others
orderlist[5] = "cy"
-- hp an alias for p ([hanyu] pinyin)
orderlist[6] = "sl"
orderlist[7] = "p"
orderlist[8] = "tp"
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 args["hp"] then
-- hp an alias for p ([hanyu] pinyin)
args["p"] = args["hp"]
end
-- if also Tongyu pinyin use full name for Hanyu pinyin
if args["tp"] then
-- if also Tongyu pinyin use full name for Hanyu pinyin
labels["p"] = "Hanyu Pinyin"
end
-- Treatspecify Simplified + Traditional astraditional Chinese if they're the samevariants
if (args["st_hk"] == args["t"]) then
args["ct"] = args["st_hk"]
args["s"] regionalvariant = "HK"
elseif args["tt_tw"] = ""then
args["t"] = args["t_tw"]
regionalvariant = "TW"
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
if out == "s" or out == "t" then
out = "c"
end
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
if out then
for i, v in ipairs (orderlist) do -- shift `out` to the beginning of the order list
if v == out then
table.remove(orderlist, i)
table.insert(orderlist, 1, v)
break
end
end
end
 
if (out == "c" and args["s"]) then usebrackets = 2; end
local body = "" -- the output string
 
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, namepart in ipairs(orderlist) do
if (args[namepart]) then
local valnumargs = args[name]numargs + 1
if-- (valbuild ~= "") thenlabel
label = ""
if (ISOlang[name]) then -- add language if needed
if (uselabels) then
local params = {lang=ISOlang[name]}
params["xml:lang"]label = ISOlanglabels[namepart]
if (capfirst) then
val = mw.text.tag({name="span",attr=params, content=val})
label = mw.language.getContentLanguage():ucfirst(label)
elseif (name == "p") then -- italicise pinyin
valcapfirst = "''" .. val .. "''"false
elseif (name == "l") then -- put literals in quotes
val = '"' .. val .. '"'
end
if (uselinks and part ~= "l" and part ~= "tr") then
-- build this entry
label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
if (no) or name == "l" then
end
body = body .. labels[name] .. ": " .. val .. "; "
if (part == "l" or part == "tr") then
label = "<abbr title=\"" .. wlinks[part] .. "\"><small>" .. label .. "</small></abbr>"
else
label = label .. "&colon;"
body = body .. "[[" .. wlinks[name] .. "|" .. labels[name] .. "]]: " .. val .. "; "
end
label = label .. " "
-- if has associated category add it
end
if (cats[name]) then
-- build value
body = body .. cats[name]
val = args[part]
if (cats[part]) and mw.title.getCurrentTitle().namespace == 0 then
-- if has associated category AND current page in article namespace, add category
val = cats[part] .. val
end
if (ISOlang[part]) then
-- add span for language if needed
params = {["lang"] = ISOlang[part] .. (regionalvariant and "-" .. regionalvariant or "")}
val = mw.text.tag({name="span",attrs=params, content=val})
elseif (part == "l") then
local terms = ""
-- put individual, potentially comma-separated glosses in single quotes
-- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
for term in val:gmatch("[^;,]+") do
term = mw.text.trim(term, "%s\"'")
terms = terms .. "&apos;" .. term .. "&apos;, "
end
val = string.sub(terms, 1, -3)
elseif (part == "tr") then
-- put translations in double quotes
-- (first strip leading and trailing spaces and quotes, including bold/italic markup)
val = mw.text.trim(val, "%s\"'")
val = "&quot;" .. val .. "&quot;"
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
Line 129 ⟶ 279:
if (body > "") then -- check for empty string
returnbody = string.sub(body, 1, -3) -- chop off final semicolon and space
if out and numargs > usebrackets then
else --no named parameters; see if there's an unnamed first parameter
-- closing bracket after the rest of the terms
c = args[1]
body = body .. "&rpar;"
if (c) then
end
if (c > "") then -- if there is treat it as Chinese
return body
local params = {lang=ISOlang["c"]}
else --no named parameters; see if there's a first parameter, ignoring its name
params["xml:lang"] = ISOlang["c"]
if (args[1]) then
c = mw.text.tag({name="span",attr=params, content=c})
-- if there is treat it as Chinese
return "[[" .. wlinks["c"] .. "|" .. labels["c"] .. "]]: " .. c .. cats["c"]
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
end
return ""
end