require('strict')
local getArgs = require('Module:Arguments').getArgs
local p = {}
function p.main(frame)
local args = getArgs(frame)
return p._main(args, frame)
end
function p._main(args, frame)
--------------------------------------------
local getLink = function(iw, label, to)
-- 预处理iw
if iw == nil then return '' end
if mw.ustring.sub(iw, 0, 1) == ':' then iw = mw.ustring.sub(iw, 2,-1) end -- 消除前导冒号
local sl, tl, u, text
-- 提取来源语言标签
sl = args.from or iw:match('^([^:]+):.+$')
-- 处理翻译语言
if to == nil and args.to then to = args.to end
if to == 'zh' then to = nil end -- 'zh'或缺省时处理简繁体
tl = to or require('Module:Lan')._main({['zh-cn']='zh-CN', ['zh-tw']='zh-TW'}, frame)
-- 获取URL
u = tostring(mw.uri.fullUrl(iw))
-- 获取外链显示文字
text = label or iw
local ret
ret = mw.ustring.format(
'https://translate.google.com/translate?sl=%s&tl=%s&prev=_t&hl=%s&u=%s',
sl, tl, tl, u
)
ret = mw.ustring.format(
'[%s %s]',
ret, text
)
ret = mw.ustring.format(
'<span class="machinetrans machinetrans_to_%s machinetrans_from_%s">%s</span>',
tl, sl, ret
)
return ret
end
--------------------------------------------
if args.sup then
local langs = mw.text.split(args.sup, ',')
local links = {}
for _, v in ipairs(langs) do
local lang = mw.text.trim(v)
table.insert(links, getLink(args[1], lang, lang))
end
local text = table.concat(links, ',')
text = '<sup>[' .. text .. ']</sup>'
text = '[[:' .. args[1] .. (args[2] and ('|' .. args[2]) or '') .. ']]' .. text
return text
end
return getLink(args[1], args[2])
end
return p