跳转到内容

模块:ZhConversion

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书
文档图示 模块文档[查看] [编辑] [历史] [清除缓存]

本模块可以在Lua阶段实现文字的繁简转换,但不支援地区词转换和转换组。(ZhConversion.php的Lua实现)

原始文字 本文为测试文字。这段文字用于说明格式,请勿删除。这段文字用于说明格式,请勿删除。海纳百川,有容乃大。维基百科,自由的百科全书。

输入到to_hant()函数变为(转为繁体):
本文為測試文字。這段文字用於說明格式,請勿刪除。這段文字用於說明格式,請勿刪除。海納百川,有容乃大。維基百科,自由的百科全書。

原始文字 本文為測試文字。這段文字用於說明格式,請勿刪除。這段文字用於說明格式,請勿刪除。海納百川,有容乃大。維基百科,自由的百科全書。

输入到to_hans()函数变为(转为简体):
本文为测试文字。这段文字用于说明格式,请勿删除。这段文字用于说明格式,请勿删除。海纳百川,有容乃大。维基百科,自由的百科全书。

原始文字繁體简体(繁简混用)

输入到to_hant()函数变为(转为繁体):繁體簡體

原始文字繁體简体(繁简混用)

输入到to_hans()函数变为(转为简体):繁体简体

用途

标题转换

在魔术字和Lua的场合中,页面标题的繁简差异是无法被识别的,例如{{PAGESIZE:}}。以光泽 (矿物)为例,页面光泽 (矿物)存在而页面光澤 (礦物)不存在由系统自动转换标题差异,这时:

{{PAGESIZE:光泽 (矿物)}}→“9,900”
{{PAGESIZE:光澤 (礦物)}}→“0”

对于输入的标题同时,-{}-和<langconvert></langconvert>等转换语法在模板及模块阶段是不工作的:

<langconvert from="zh-hans" to="zh-hant">光泽 (矿物)</langconvert>→“光澤 (礦物)”
{{PAGESIZE:<langconvert from="zh-hans" to="zh-hant">光泽 (矿物)</langconvert>}}→“0”
<langconvert from="zh-hant" to="zh-hans">光澤 (礦物)</langconvert>→“光泽 (矿物)”
{{PAGESIZE:<langconvert from="zh-hant" to="zh-hans">光澤 (礦物)</langconvert>}}→“0”

这意味着,如果存在的页面是光泽 (矿物),输入光澤 (礦物)到模板或模块中有关功能是会失效的。

所以如果输入的值是光澤 (礦物)就有在Lua运算阶段需使用繁简转换的需求。

{{PAGESIZE:{{#invoke:ZhConversion|zh_title|光泽 (矿物)}}}}→“9,900”
{{PAGESIZE:{{#invoke:ZhConversion|zh_title|光澤 (礦物)}}}}→“9,900”

函数说明

to_hant(字符串)
输入一个字符串,转换为繁体中文(可模板调用)
to_hans(字符串)
输入一个字符串,转换为简体中文(可模板调用)
zh_convert(字符串)
输入一个字符串,进行繁简转换,若输入是简体,转换为繁体;若输入是繁体,转换为简体;若繁简混用,以繁体优先。(可模板调用)
zh_title(页面标题)
输入一个页面标题,若该页面标题页面不存在,但繁简转换后存在,则返回存在的标题。(可模板调用)
equals(字符串1,字符串2)
忽略繁简差异的文字比对,例如“光泽”与“光澤”视为相同。(可模板调用)
_language_cvt(字符串, 转换表, 子字符串搜索最大长度)
使用指定的转换表进行转换。(不支援模板调用)

参见

local p={}
local zhcvt = mw.loadData('Module:ZhConversion/data')
function p.to_hant(str)
	local input_str = str
	if type(str) == type({"table"}) then
		input_str = (str.args or {})[1] or str[1] or ''
	elseif type(str) ~= type("string") then
		input_str = tostring(str)
	end
	return p._language_cvt(input_str, zhcvt.to_hant, zhcvt.max_length)
end
function p.to_hans(str)
	local input_str = str
	if type(str) == type({"table"}) then
		input_str = (str.args or {})[1] or str[1] or ''
	elseif type(str) ~= type("string") then
		input_str = tostring(str)
	end
	return p._language_cvt(input_str, zhcvt.to_hans, zhcvt.max_length)
end
function p.zh_title(str)
	local input_str = str
	if type(str) == type({"table"}) then
		input_str = (str.args or {})[1] or str[1] or ''
	elseif type(str) ~= type("string") then
		input_str = tostring(str)
	end
	local checked_title = input_str
	xpcall(function()
		local function getTitle(...)
			local success, titleObj = pcall(mw.title.new, ...)
			if success then return titleObj else return nil end
		end
		local titleObj = getTitle(input_str)
		local ori_code = titleObj:getContent()
		if ori_code then checked_title = input_str else
			local zh_hant = p.to_hant(input_str)
			local titleHantObj = getTitle(zh_hant)
			local hant_code = titleHantObj:getContent()
			if hant_code then checked_title = zh_hant else
				local zh_hans = p.to_hans(input_str)
				local titleHansObj = getTitle(zh_hans)
				local hans_code = titleHansObj:getContent()
				if hans_code then checked_title = zh_hans end
			end
		end
	end,function()end)
	return checked_title
end
function p.equals(str1, str2)
	local input_str1 = str1
	local input_str2 = str2
	if type(str1) == type({"table"}) then
		input_str1 = (str1.args or {})[1] or str1[1] or ''
		input_str2 = (str1.args or {})[2] or str1[2] or ''
	elseif type(str1) ~= type("string") then
		input_str1 = tostring(str1)
		input_str2 = tostring(str2)
	end
	local result = false
	if input_str1 == input_str2 then
		result = true
	else
		local str1_hans = p._language_cvt(input_str1, zhcvt.to_hans, zhcvt.max_length)
		local str2_hans = p._language_cvt(input_str2, zhcvt.to_hans, zhcvt.max_length)
		if str1_hans == str2_hans or input_str1 == str2_hans or str1_hans == input_str2 then
			result = true
		else
			local str1_hant = p._language_cvt(input_str1, zhcvt.to_hant, zhcvt.max_length)
			local str2_hant = p._language_cvt(input_str2, zhcvt.to_hant, zhcvt.max_length)
			if str1_hant == str2_hant or input_str1 == str2_hant or str1_hant == input_str2 then
				result = true
			end
		end
	end
	if str1 == mw.getCurrentFrame() or (type(str1)==type({}) and type(str1.getParent)==type(function()end)) then
		return result and '1' or ''
	end
	return result
end
function p.zh_convert(str)
	local input_str = str
	if type(str) == type({"table"}) then
		input_str = (str.args or {})[1] or str[1] or ''
	elseif type(str) ~= type("string") then
		input_str = tostring(str)
	end
	local result = p._language_cvt(input_str, zhcvt.to_hant, zhcvt.max_length)
	if result == input_str then
		result = p._language_cvt(input_str, zhcvt.to_hans, zhcvt.max_length)
	end
	return result
end

function p._language_cvt(str, cvt_table, max_length)
	local strlen = mw.ustring.len(str)
	local result = ''
	local i=1
	while i<=strlen do
		local changed = false
		local this_char = mw.ustring.sub(str, i, i)
		for ji = 1,max_length do
			local j = max_length - ji
			if i + j <= strlen then
				local check_str = mw.ustring.sub(str, i, i + j)
				if cvt_table[check_str] then
					result = result .. cvt_table[check_str]
					i = i + j
					changed = true
					break
				end
			end
		end
		if not changed then result = result .. this_char end
		i = i + 1
	end
	return result
end
return p