本站文本内容除另有声明外,转载时均必须注明出处,并遵守CC BY-NC-SA 3.0协议。(转载须知)
本站是中文Minecraft Wiki的镜像站,与Mojang Studios、Weird Gloop没有从属关系。(免责声明)
模块:NameProvider
此模块实现本地化键名与游戏内文本的相互转换,以及美式英语(en_us)与三种中文变体(zh_cn、zh_tw、zh_hk)间的相互转换。
调用此模块的模板请见{{NameProvider}}
。
函数
通过其他模块调用时,应使用call
函数。
- 此函数首个形参为lua表,除匿名参数需要在其前加“arg”外,其他键名与下述参数一致,这些键名作为调用其他函数时的参数;第二个形参控制错误输出,若需要显示错误信息而不是直接返回nil,可将其置为true,由其他模块调用时则建议设为false。
通过模板调用时,应使用main
函数,并用function
参数指定使用下列函数之一:
getnamebykey
- 将本地化键名转换为指定语言变体的游戏内名称。1
- 要转换的本地化键名。to
- 目标语言代码,默认为en_us。
getkeybyname
- 将指定语言变体的游戏内名称转换为本地化键名。1
- 要转换的游戏内名称。nocase
- 是否不区分大小写。
gettranslation
- 将指定语言变体的游戏内名称转换为另一语言变体的游戏内名称。1
- 要转换的游戏内名称。to
- 要转换到的语言代码,默认为zh_cn。nocase
- 是否不区分大小写。
数据模块
此模块按snapshot,release,extension的顺序提供数据。更新数据模块时,需先将键名按字母排序后改为lua表格式(直接替换)。
release和snapshot的数据可以通过脚本生成。
local p = {}
-- data definitions
local dataset = {
release = mw.loadData('Module:NameProvider/release'), -- names in the latest release
snapshot = mw.loadData('Module:NameProvider/snapshot'), -- names in the latest snapshot (if exists)
extension = mw.loadData('Module:NameProvider/extension'), -- extension for special names
}
-- core functions (not exported)
function lookup_forward(key, variant, onlysource) -- forward lookup: translation_key -> in-game name
local variantlocation = { -- for locating correct datas, they should be lowercased ISO 639-1 codes
en_us = 1, -- default fallback
zh_cn = 2,
zh_tw = 3,
zh_hk = 4,
}
local target_variant = string.lower(mw.text.trim(variant or 'en_us'))
local datalocation = variantlocation[target_variant]
if datalocation == nil then
return nil
end
local result
if onlysource == 'release' then
result = dataset.release[key] or dataset.extension[key]
elseif onlysource == 'snapshot' then
result = dataset.snapshot[key] or dataset.extension[key]
else
result = dataset.snapshot[key] or dataset.release[key] or dataset.extension[key]
end
if result ~=nil then
result = result[datalocation]
end
return result
end
function lookup_reverse(name, nocase, onlysource) -- reverse lookup: translation_key -> in-game name
local strcmp = function(str1, str2, nocase)
if nocase then
return (string.lower(str1) == string.lower(str2))
else
return (str1 == str2)
end
end
local grabkey = function(data, name, nocase)
local current_key, current_value
for current_key, v in pairs(data) do
if type(v) == 'table' then
for _, current_value in pairs(v) do
if strcmp(name, current_value, nocase) then
return current_key
end
end
end
end
return nil
end
if onlysource == 'release' then
return grabkey(dataset.release, name, nocase) or grabkey(dataset.extension, name, nocase)
elseif onlysource == 'snapshot' then
return grabkey(dataset.snapshot, name, nocase) or grabkey(dataset.extension, name, nocase)
else
return grabkey(dataset.snapshot, name, nocase) or grabkey(dataset.release, name, nocase) or grabkey(dataset.extension, name, nocase)
end
end
function translate(source_name, target_variant, nocase, onlysource) --convert: in-game name (lang: a) -> in-game name (lang: b)
local translation_key = lookup_reverse(source_name, nocase, onlysource)
if translation_key == nil then
return nil
end
return lookup_forward(translation_key, target_variant, onlysource)
end
-- shell functions (not exported)
function getnamebykey(inputargs, prettyerror) -- convert: translation_key -> in-game name
local result = lookup_forward(inputargs.arg1, inputargs.to or 'en_us', inputargs.onlysource)
if prettyerror == true and result == nil then
result = mw.getCurrentFrame():expandTemplate { title = 'Error', args = { '未知键名:“' .. inputargs.arg1 .. '”。' } }
end
return result
end
function getkeybyname(inputargs, prettyerror) -- convert: in-game name -> translation_key
local result = lookup_reverse(inputargs.arg1, inputargs.nocase, inputargs.onlysource)
if prettyerror == true and result == nil then
result = mw.getCurrentFrame():expandTemplate { title = 'Error', args = { '未知名称:“' .. inputargs.arg1 .. '”。' } }
end
return result
end
function gettranslation(inputargs, prettyerror)
local to = inputargs.to or 'zh_cn'
local result = translate(inputargs.arg1, to, inputargs.nocase, inputargs.onlysource)
if prettyerror == true and result == nil then
result = mw.getCurrentFrame():expandTemplate { title = 'Error', args = { '无法找到与' .. inputargs.arg1 .. '”对应的' .. to .. '翻译。' } }
end
return result
end
-- exported functions
function p.call(inputargs, prettyerror) -- universal entry point for calling from other modules
if inputargs.arg1 == nil then
if prettyerror == true then
return mw.getCurrentFrame():expandTemplate { title = 'Error', args = { '第一个匿名参数的值不能为空。' } }
else
return nil
end
end
local registered_functions = {
['getnamebykey'] = getnamebykey,
['getkeybyname'] = getkeybyname,
['gettranslation'] = gettranslation,
}
local selected_function = mw.text.trim(inputargs['function'])
if registered_functions[selected_function] == nil then
if prettyerror == true then
return mw.getCurrentFrame():expandTemplate { title = 'Error', args = { '参数“function”指定的值“' .. selected_function .. '”未注册。可以是“getnamebykey”“getkeybyname”或“gettranslation”。' } }
else
return nil
end
end
local callargs = {
['arg1'] = mw.text.trim(inputargs.arg1),
['nocase'] = inputargs.nocase,
['onlysource'] = mw.text.trim(inputargs.onlysource or ''),
['to'] = inputargs.to,
}
return registered_functions[selected_function](callargs, prettyerror)
end
function p.main(f) -- universal entry point for calling from templates
local args = f
if f == mw.getCurrentFrame() then
args = f:getParent().args
end
local inputargs = {
['arg1'] = args[1],
['function'] = args['function'],
['nocase'] = args.nocase,
['onlysource'] = args.onlysource,
['to'] = args.to,
}
local showerror = true
if args.silent then
showerror = false
end
return p.call(inputargs, showerror)
end
function p.getversion() -- get data version by source
local args = require('Module:ProcessArgs').merge(true)
local source_type = string.lower(mw.text.trim(args.type or ''))
local result = nil
if source_type == 'release' then
result = dataset.release['_meta.version']
elseif source_type == 'snapshot' then
result = dataset.snapshot['_meta.version']
end
if result then
if not args.nolink then
if string.find(result, 'w') then
return table.concat({'[[', result, ']]'})
else
return table.concat({'[[Java版', result, '|', result, ']]'})
end
else
return result
end
else
return "''无''"
end
end
return p