模組:Vgname/sandbox
外观
![]() | 这是Module:Vgname(差异)的沙盒。 参见本模块的测试样例(运行)。 |
请在 Module:Vgname/languages定义新的外语
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local lc = require('Module:WikitextLC')
local foreignRegionList = mw.loadData('Module:沙盒/D2F0F5/First')
local chineseRegion = {
["cn"] = {simp = "中国大陆", trad = "中國大陸"},
["cnhk"] = {simp = "中国大陆和香港", trad = "中國大陸和香港"},
["cntw"] = {simp = "中国大陆和台湾", trad = "中國大陸和台灣"},
["hk"] = {simp = "香港", trad = "香港"},
["tw"] = {simp = "台湾", trad = "台灣"},
["twhk"] = {simp = "港台", trad = "港台"},
}
local p = {}
local function separateFootnotesFromText(str)
local pattern = '\127\'"`UNIQ%-%-ref%-%x+%-QINU`"\'\127' -- [[en:WP:UNIQ]]
str = str or ''
local s, _ = str:find(pattern)
if s then
return str:sub(1, s-1), str:sub(s)
end
return str, ''
end
local function bold(args, str, openBracket, closeBracket, isConvert)
local text, footnotes = separateFootnotesFromText(str)
if yesno(isConvert) == true then
text = lc.converted(text, {'zh-hans', 'zh-hant'})
end
if yesno(args.bold) ~= false then
text = '<b>' .. text .. '</b>'
end
if yesno(args.bracket) == false then
-- do nothing
elseif openBracket and closeBracket then
text = openBracket .. text .. closeBracket
end
return text .. footnotes
end
local function boldWithIdeographicComma(args, str, openBracket, closeBracket, isConvert)
local tab, _tab = mw.text.split(str, '、'), {}
for _, value in ipairs(tab) do
local str = bold(args, value, openBracket, closeBracket, isConvert)
table.insert(_tab, str)
end
return table.concat(_tab, '、')
end
local function styleForeignLanguageAndAddLabel(args, indexOfTheForeignRegion, str)
local text, footnotes = separateFootnotesFromText(str)
local theLanguage = foreignRegionList[indexOfTheForeignRegion]
local langcode = theLanguage.langcode
local style = theLanguage.italic and 'italic' or 'normal'
local label
if yesno(args.diff) == true then
label = theLanguage.diff
elseif yesno(args.diff) == false then
label = theLanguage.same
elseif args.na or args.eu or args.au then
label = theLanguage.diff
else
label = theLanguage.same
end
return string.format('%s:<span lang="%s" style="font-style:%s;">-{%s}-</span>%s',
label,
langcode,
style,
text,
footnotes
)
end
local function buildVariety(args, _args, tab, usedRegionList, mode)
local textModeConfig = {
simp = {
'hans', 'cn', 'sg';
inEnglish = '常用英文', translationAs = '译作',
openBracket = "“", closeBracket = "”"
},
trad = {
'hant', 'hk', 'tw';
inEnglish = '常用英文', translationAs = '譯作',
openBracket = "「", closeBracket = "」"
},
}
for _, variety in ipairs(textModeConfig[mode]) do
local subTab = {}
for _, region in ipairs(usedRegionList) do
local para, text = region:sub(1, 2), region:sub(-2, -1) -- reuse text
if para ~= variety and text ~= variety then
if _args[para] == 'en' then
text = chineseRegion[region][mode] .. textModeConfig[mode].inEnglish
else
text = chineseRegion[region][mode] .. textModeConfig[mode].translationAs .. bold(
args,
_args[para],
textModeConfig[mode].openBracket,
textModeConfig[mode].closeBracket,
true
)
end
table.insert(subTab, text)
end
end
tab['zh-' .. variety] = table.concat(subTab, ',')
end
return tab
end
local function head(args)
local str = args[1]
local openBracket, closeBracket
if args.bracket == 'q' then
openBracket, closeBracket = '「', '」'
else
openBracket, closeBracket = '《', '》'
end
return bold(args, str, openBracket, closeBracket, false)
end
local function foreign(args)
local tab = {}
local englishRegionList = {'na', 'eu', 'au'}
for _, value in ipairs(englishRegionList) do
local str = args[value]
if str then
local text = styleForeignLanguageAndAddLabel(args, value, str)
table.insert(tab, text)
end
end
if #tab == 0 and args.en then
local text = styleForeignLanguageAndAddLabel(args, 'en', args.en)
table.insert(tab, text)
end
for key, value in ipairs(foreignRegionList) do
local str = args[value.langcode]
if str then
local text = styleForeignLanguageAndAddLabel(args, key, str)
table.insert(tab, 1, text)
end
end
if #tab == 0 then return '' end
return table.concat(tab, ',')
end
local function variety(args)
if args.cn == nil or args.tw == nil then
return ''
end
local tab, usedRegionList = {}, {}
local _args = {}
for _, value in ipairs{'cn', 'hk', 'tw'} do
_args[value] = args[value]
end
if _args.hk == nil or _args.hk == 'tw' or _args.hk == _args.tw then
usedRegionList = {'cn', 'twhk'}
elseif _args.hk == 'cn' or _args.hk == _args.cn then
usedRegionList = {'cnhk', 'tw'}
elseif _args.tw == 'cn' or _args.tw == _args.cn then
usedRegionList = {'cntw', 'hk'}
elseif _args.cn == 'tw' then
_args.cn = _args.tw
usedRegionList = {'cntw', 'hk'}
else
usedRegionList = {'cn', 'hk', 'tw'}
end
tab = buildVariety(args, _args, tab, usedRegionList, 'simp')
tab = buildVariety(args, _args, tab, usedRegionList, 'trad')
tab['zh-mo'] = tab['zh-hk']
return lc.selective(tab)
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
local tab, _tab = {}, {}
tab = {
foreign(args),
variety(args)
}
for _, value in ipairs(tab) do
if value ~= '' then
table.insert(_tab, value)
end
end
if #_tab > 0 then
return string.format('%s<span style="font-weight:normal;">(%s)</span>',
head(args),
table.concat(_tab, ',')
)
end
return head(args)
end
return p