模块:SpecialConversion

来自Minecraft Wiki
跳转至: 导航搜索
local p = {}
--X zh-cn
--Y zh-tw
--Z zh-hk
local all = mw.loadData('Module:SpecialConversion/All')
local snap = mw.loadData('Module:SpecialConversion/Snapshot')

function p.all(type)
	if type == 'advdesc' then
		type = 'advancements'
	end
	if type ~= 'all' then
		return p.insertAll(type)
	else
		return p.insertAll('advancements', 'stats', 'subtitles')
	end
end

function p.insertAll(...)
	local t = {}
	for k in pairs(all) do
		for _, name in ipairs {...} do
			if k:find('^' .. name .. '.') then
				table.insert(t, '-{H|zh-cn:' .. all[k][1] .. ';zh-tw:' .. all[k][2] ..';zh-hk:'..all[k][3].. '}-')
			end
		end
	end
	table.sort(t)
	return table.concat(t)
end

function p.one(t, key, mode, argX, argY, argZ)
	local resultX = ''--X zh-cn
	local resultY = ''--Y zh-tw
	local resultZ = ''--Z zh-hk
	mode = mode ~= '' and mode .. '|' or mode
	if t[key] then
		resultX = t[key][1] --zh-cn
		resultY = t[key][2] --zh-tw
		resultZ = t[key][3] --zh-hk
	end
	argX = padArg(argX, resultX)
	argY = padArg(argY, resultY)
	argZ = padArg(argZ, resultZ)
	if resultX ~= '' and resultY ~= '' and resultZ ~= '' then
		return table.concat {
			'-{',
			mode,
			'zh-cn:',
			resultX:format(unpack(argX)),
			';zh-tw:',
			resultY:format(unpack(argY)),
			';zh-hk:',
			resultZ:format(unpack(argZ)),
			'}-'
		}
	end
	if resultX ~= '' and resultY == '' then
		return resultX:format(unpack(argX)) .. '[[Category:缺少zh-tw翻译]]'
	elseif resultX ~= '' and resultZ == '' then
		return resultX:format(unpack(argX)) .. '[[Category:缺少zh-hk翻译]]'
		else return ''
	end
end

function padArg(arg, pattern)
	local t = arg or {}
	local i = 1
	for _ in pattern:gmatch('%%s') do
		t[i] = t[i] or '%s'
		i = i + 1
	end
	return t
end

function getPrefixedArgs(args, prefix)
	local t = {}
	local n
	for k, v in pairs(args) do
		if type(k) == 'string' and k:match('^' .. prefix .. '%d+$') then
			n = tonumber(k:match('^' .. prefix .. '(%d+)$'))
			t[n] = v
		end
	end
	return #t ~= 0 and t or nil
end

function p.convert(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	local type = args.type -- advancements (advdesc), stat, subtitles
	local mode = args.mode or '' -- H
	local argX = getPrefixedArgs(args, 's')
	local argY = getPrefixedArgs(args, 't') or argX
	local argZ = getPrefixedArgs(args, 'h') or argX
	local cat = ''

	if args[1] == 'all' then
		return p.all(type)
	else
		local conv = p.one(all, args[1], mode, argX, argY, argZ)
		if conv == '' then
			conv = p.one(snap, args[1], mode, argX, argY, argZ)
			cat = '[[Category:快照转换内容]]'
		end
		if conv ~= '' then
			return conv .. cat
		else
			cat = '[[Category:未知的本地化键名]]'
			return "'''" .. args[1] .. "'''" .. cat
		end
	end
end

function p.list(f)
	local args = f
	if f == mw.getCurrentFrame() then
		args = f:getParent().args
	end
	
	local p1=args[1] 
	local p2=args[2]
	local p3=args[3]
	local title=args['title'] or '类别'
	
	local body ={'{| class="wikitable"\n|-\n!本地化键名 !! 游戏内字符串 '}
	local find = false
	
	local look =''
	if (p1 or p2 or p3 or false) == false then 
		return "'''空参数'''[[Category:未知的本地化键名]]"
	end
	if p1 then 
		look = look ..p1..'%.'
	end
	
	if p2 then 
		look = look ..p2..'%.'
	end
	
	if p3 then 
		look = look ..p3..'%.'
	end
	
	for k in pairs(all) do
			if string.find(k,look) == 1 then
				find = find or true
				table.insert(
					body,
					table.concat(
						{
							'|-\n',
							'| ',
							'<code>'..k..'</code>',
							'|| ',
							table.concat(
									{
										'-{zh-cn:',
										all[k][1],
										';zh-tw:',
										all[k][2],
										';zh-hk:',
										all[k][3],
										'}-'
									}
								)
						}
						)
					)
			end
		
	end
	table.insert(
				body,
				'|}'
				)
	if find then
		table.sort(body)
		return table.concat(body, '\n')
	else
		return "'''未知的本地化键名前缀[[Category:未知的本地化键名]]'''"
	end
end
return p