Mòdul:zh-trans

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Mòdul per a la transcripció del xinès entre tradicional, simplificat i pinyin:

En la proposta de transcripció del xinès al català de l’IEC es recomana usar pinyin descartant altres sistemes de transcripció històrics i en ús. En un context de divulgació es pot prescindir de les marques de to. Aquest mòdul manté els diacrítics.

Limitacions:

  • Si l’escriptura en xinès no té marcada tipogràficament la separació de paraules, es manté aglutinat en la transcripció. La separació de mots en pinyin pot dependre de diversos aspectes. Vegeu la proposta de l’IEC per les indicacions sobre la separació de mots.
  • Els noms propis s’escriuen amb majúscula inicial en pinyin, però és un factor extern a la transcripció que no sempre es pot detectar.

Funcions:

  • ts_determ determina si un text és "trad", "simp" o "both".
  • ts transliteració de tradicional a simplificat.
  • st transliteració de simplificat a tradicional.
  • py transcripció de tradicional o simplificat a pinyin.
  • tr transcripció amb dues possibilitats: 1) de tradicional a simplificat, si són diferents; 2) de tradicional o simplificat a pinyin.

Mòduls de suport:

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>&#39;", "&#39;<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