跳转到内容

模組:ZhConversion

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2022年8月18日 (四) 13:13 (技術限制需求 Special:PermaLink/73258479#Template:Va不能识别重定向和繁简重定向编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

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(str, zhcvt.to_hans, zhcvt.max_length)
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