模組:ZhConversion

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

本模組可以在Lua階段實現文字的繁簡轉換,但不支援地區詞轉換和轉換組。(ZhConversion.php的Lua實現)

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

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

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

輸入到to_hans()函數變為(轉為簡體):
本文为测试文字。这段文字用于说明格式,请勿删除。这段文字用于说明格式,请勿删除。海纳百川,有容乃大。维基百科,自由的百科全书。

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

輸入到to_hant()函數變為(轉為繁體):繁體簡體

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

輸入到to_hans()函數變為(轉為簡體):繁体简体

用途

標題轉換

在魔術字和Lua的場合中,頁面標題的繁簡差異是無法被識別的,例如{{PAGESIZE:}}。以光泽 (矿物)為例,頁面光泽 (矿物)存在而頁面光澤 (礦物)不存在由系統自動轉換標題差異,這時:

{{PAGESIZE:光泽 (矿物)}}→「9,881」
{{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,881」
{{PAGESIZE:{{#invoke:ZhConversion|zh_title|光澤 (礦物)}}}}→「9,881」

函數說明

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