模 組 :ZhConversion
![]() | 此模块被 |
輸入 到 to_hant()函數 變 為 (轉 為 繁 體 ):本文 為 測 試 文字 。這段文字 用 於說明 格式 ,請勿刪除。這段文字 用 於說明 格式 ,請勿刪除。海 納 百 川 ,有 容 乃大。維基百科 ,自由 的 百科全書 。
輸入 到 to_hans()函數 變 為 (轉 為 簡體):本文 为测试文字 。这段文字 用 于说明 格式 ,请勿删除。这段文字 用 于说明 格式 ,请勿删除。海 纳百 川 ,有 容 乃大。维基百科 ,自由 的 百科 全 书。
(
輸入 到 to_hant()函數 變 為 (轉 為 繁 體 ):繁 體 簡體
(
輸入 到 to_hans()函數 變 為 (轉 為 簡體):繁 体 简体
用途
標題 轉換
{{PAGESIZE:}}
。以
{{PAGESIZE:
→「9,881」光 泽 (矿物)}}{{PAGESIZE:
→「0」光澤 (礦物)}}
<langconvert from="zh-hans" to="zh-hant">
→「光 泽 (矿物)</langconvert>光澤 (礦物)」{{PAGESIZE:<langconvert from="zh-hans" to="zh-hant">
→「0」光 泽 (矿物)</langconvert>}}
<langconvert from="zh-hant" to="zh-hans">
→「光澤 (礦物)</langconvert>光 泽 (矿物)」{{PAGESIZE:<langconvert from="zh-hant" to="zh-hans">
→「0」光澤 (礦物)</langconvert>}}
這意
{{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(
字 串 ,轉換 表 ,子 字 串 搜 尋 最大 長 度 ) 使用 指定 的 轉換 表 進行 轉換 。(不 支援 模 板 調 用 )
參 見
- Module:ZhConversion/data:
本 模 組 的 轉換 表
编者 |
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