Modulo:zh-trans
Come che se vede
La documentazione per questo modulo può essere creata in Modulo:zh-trans/man
local M = {}
local len = mw.ustring.len
local sub = mw.ustring.sub
local gsub = mw.ustring.gsub
local find = mw.ustring.find
local function replace_chars(s, tab)
-- use UTF-8 character pattern for speed
return string.gsub(s, "[%z\1-\127\194-\244][\128-\191]*", tab)
end
function M.ts_determ(f)
local m_ts_data = mw.loadData("Module:zh-trans/ts")
local m_st_data = mw.loadData("Module:zh-trans/st")
local text = type(f) == 'table' and f.args[1] or f
local i = 0
for cp in mw.ustring.gcodepoint(text) do
local ch = mw.ustring.char(cp)
if m_ts_data.ts[ch] then return 'trad' end
if m_st_data.st[ch] then if i > 1 then return 'simp' else i = i + 1 end end
end
return (i > 0 and 'simp' or 'both')
end
function M.ts(f)
local m_ts_data = mw.loadData("Module:zh-trans/ts")
local text = type(f) == 'table' and f.args[1] or f
text = replace_chars(text, m_ts_data.ts)
return text
end
function M.st(f)
local m_st_data = mw.loadData("Module:zh-trans/st")
local text = type(f) == 'table' and f.args[1] or f
text = replace_chars(text, m_st_data.st)
return text
end
function M.py(text, comp, pos, p, is_erhua)
local m_cmn_pron = mw.loadData("Module:zh-trans/cmn-pron")
if not is_erhua then is_erhua = false end
if type(text) == 'table' then
text, comp, pos, p, is_erhua = text.args[1], text.args[2], text.args[3], text.args[4], text.args[5]
end
comp = comp or ''
local q = {}
local sum = 0
local length = len(text)
if is_erhua then length = length - 1 end
local textconv = text
text = ''
if comp ~= '' and comp ~= '12' and comp ~= '21' and not ((pos == 'cy' or pos == 'Idiom' or pos == 'idiom') and length == 4) and not is_erhua then
for i = 1, len(comp) do
sum = sum + tonumber(sub(comp,i,i))
q[sum] = 'y'
end
end
if not p then p={} end
local initial = true
for i = 1, length do
if p[i] and p[i] ~= '' then --pronunciation supplied
text = text .. p[i]
else
local char = sub(textconv,i,i)
char = m_cmn_pron.py[char] or m_cmn_pron.py[M.ts(char)] or char
if not is_erhua and not initial and find(char,'^[aoeāōēáóéǎǒěàòè]') then
text = text .. "'"
end
text = text .. char
initial = char == sub(textconv,i,i)
and sub(textconv,i-3,i) ~= "</b>" --checks for closing bold tag
and (i-2 == 1 or sub(textconv,i-2,i) ~= "<b>" or sub(textconv,i-3,i) == "^<b>") --checks for opening bold tag
and (i-3 == 1 or sub(textconv,i-3,i) ~= "^<b>") --checks for opening bold tag with capitalization
end
if q[i] == 'y' and i ~= length and not is_erhua then text = text .. ' ' end
end
text = gsub(text, "<b>'", "'<b>") --fix bolding of apostrophe
if is_erhua then text = text .. 'r' end
if pos == 'nom propi' then
local characters = mw.text.split(text,' ')
for i=1,#characters do
characters[i] = mw.language.getContentLanguage():ucfirst(characters[i])
end
text = table.concat(characters,' ')
end
return text
end
function M.tr(text)
if type(text) == "table" then text = text.args[1] end
local ret = ''
if M.ts_determ(text) == 'trad' then
local simp = M.ts(text)
if simp ~= text then
ret = '<span class="Hans" lang="zh">' .. simp .. '</span>, '
end
end
return ret .. M.py(text)
end
return M