Module:Lang-zh: Difference between revisions

Content deleted Content added
edit request from talk page
Publishing User:Northern Moonlight's implementation of new fields to specify locale-specific character glyphs according to the IETF standard
 
(32 intermediate revisions by 15 users not shown)
Line 1:
require('Module:No globalsstrict')
 
local p = {}
 
-- articles in which traditional Chinese preceeds simplified Chinese
local t1st = {
Line 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 41 ⟶ 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 50 ⟶ 57:
["t"] = "zh-Hant",
["s"] = "zh-Hans",
["p"] = "zh-Latn-pinyin",
["tp"] = "zh-Latn-tongyong",
["w"] = "zh-Latn-wadegile",
["j"] = "yue-Latn-jyutping",
["cy"] = "yue-Latn",
["pojsl"] = "hakyue-Latn",
["poj"] = "nan-Latn",
["tl"] = "nan-Latn-tailo",
["zhu"] = "zh-Bopo",
}
Line 65 ⟶ 74:
["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 83 ⟶ 100:
function p._Zh(args)
localif uselinksargs["link"] =then not (args["links"] == args["nolink")]; -- whether to add linksend
localif uselabelsargs["label"] =then not (args["labels"] == args["nolabel")]; -- whether to have labelsend
local uselinks = args["links"] ~= "no" -- whether to add links
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
Line 94 ⟶ 124:
if (testChar == "t") then
t1 = true
end
if (testChar == "j") then
j1 = true
end
if (testChar == "poj") then
poj1 = true
end
end
end
Line 106 ⟶ 139:
 
-- based on setting/preference specify order
local orderlist = {"c", "s", "t", "p", "tp", "w", "j", "cy", "sl", "poj", "tl", "zhu", "l", "tr"}
if (t1) then
orderlist[2] = "t"
Line 114 ⟶ 147:
orderlist[4] = "j"
orderlist[5] = "cy"
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
Line 127 ⟶ 171:
-- if also Tongyu pinyin use full name for Hanyu pinyin
labels["p"] = "Hanyu Pinyin"
end
-- specify traditional Chinese variants
if args["t_hk"] then
args["t"] = args["t_hk"]
regionalvariant = "HK"
elseif args["t_tw"] then
args["t"] = args["t_tw"]
regionalvariant = "TW"
end
Line 134 ⟶ 187:
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
Line 139 ⟶ 195:
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
Line 148 ⟶ 215:
for i, part in ipairs(orderlist) do
if (args[part]) then
numargs = numargs + 1
-- build label
label = ""
Line 156 ⟶ 224:
capfirst = false
end
if (uselinks and part ~= "l" and part ~= "tr") then
label = "[[" .. wlinks[part] .. "|" .. label .. "]]"
end
labelif (part == label"l" ..or ":part == "tr") then
label = "<abbr title=\"" .. wlinks[part] .. "\"><small>" .. label .. "</small></abbr>"
else
label = label .. "&colon;"
end
label = label .. " "
end
-- build value
val = args[part]
if (cats[part]) and mw.title.getCurrentTitle().namespace == 0 then
-- if has associated category AND current page in article namespace, add itcategory
val = cats[part] .. val
end
if (ISOlang[part]) then
-- add span for language if needed
params = {["lang"] = ISOlang[part], [.. (regionalvariant and "xml:lang-"] =.. ISOlang[part]regionalvariant or "")}
val = mw.text.tag({name="span",attrs=params, content=val})
elseif (part == "l") then
--local putterms literals= in quotes""
-- put individual, potentially comma-separated glosses in single quotes
val = '"' .. val .. '"'
-- (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
body = body .. label .. val .. "; "
-- 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
returnbody = 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
Line 195 ⟶ 294:
label = "[[" .. wlinks["c"] .. "|" .. label .. "]]"
end
label = label .. "&#58colon; "
end
-- default to show links and labels as no options given
if mw.title.getCurrentTitle().namespace == 0 then
val = cats["c"] .. args[1]
-- if current page in article namespace
params = {["lang"] = ISOlang["c"], ["xml:lang"] = ISOlang["c"]}
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