Module:Lang
Jump to navigation
Jump to search
Documentation for this module may be created at Module:Lang/doc
local p = {}
local param = {}
local langCode = require('Module:Lang/code')
local lib = require('Module:Feature')
function trim(s)
return s:match '^%s*(.-)%s*$'
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
wrappers = {
'Template:Lang'
}
})
return p._main(args)
end
function eachLang(altLangs, args, tBlock, key, value)
local part = mw.html.create('span')
local vertical = args.vertical or nil
local append = {}
if langCode[key] then
if not(args.mini) then
part:wikitext('<u>' .. langCode[key] .. '</u>' .. ': ')
end
if not(args.selective) then
part:tag('span')
:attr('lang', key)
:wikitext(value)
else
part:node(p.SelectiveSpan(value, key))
end
-- Auto romanization if none given
if lib.isEmpty(tBlock[key .. '_rm']) and args.auto_rm then
local rm
if key == 'ru' then
rm = require('Module:Ru-rm')._main{value}
elseif key == 'ko' then
rm = require('Module:Ko-rm')._main{value}
end
-- mw.logObject(rm, 'result')
if lib.isNotEmpty(rm) then
args[key .. '_rm'] = rm
tBlock[key .. '_rm'] = true
end
end
-- Special font for chinese rm due to Rubik's lack of support for some tones
if lib.isNotEmpty(tBlock[key .. '_rm']) and lib.inArray({'zh', 'zh-Hans', 'zh-Hant'}, key) then
args[key .. '_rm'] = '<span lang="zh-rm">' .. args[key .. '_rm'] .. '</span>'
end
-- Romanization
if lib.isNotEmpty(tBlock[key .. '_rm']) then
table.insert(append, '<i>' .. args[key .. '_rm'] .. '</i>')
end
-- Translation
if lib.isNotEmpty(tBlock[key .. '_tl']) then
if vertical then
table.insert(append, args[key .. '_tl'])
else
table.insert(append, '"' .. args[key .. '_tl'] .. '"')
end
end
-- Literal translation
if lib.isNotEmpty(tBlock[key .. '_lit']) then
table.insert(append, 'lit. "' .. args[key .. '_lit'] .. '"')
end
-- Build end string
if #append>0 and vertical then
part:wikitext('<br />' .. table.concat(append, '<br />'))
elseif #append>0 then
part:wikitext(' ' .. table.concat(append, ', '):gsub('", ','," '))
end
else
part:wikitext(key .. ': ' .. value)
end
table.insert(altLangs, tostring(part))
end
function p._main(args)
local out = mw.html.create('')
local tBlock = {sort = true, mini = true, selective = true, vertical = true}
--Obtain all _tn keys
for k, v in pairs(args) do
if mw.ustring.find(k, '_rm$')
or mw.ustring.find(k, '_tl$')
or mw.ustring.find(k, '_lit$') then
tBlock[k] = true
end
end
--Obtain all numerical keys
for k,v in ipairs(args) do
tBlock[k] = true
end
--Output default Value
out:wikitext(args[1])
local altLangs = {}
--Process Sort Paremeter (Split by Delimiter: comma)
for sKey in string.gmatch(args.sort or '', '([^,]+)') do
if not(tBlock[sKey]) and args[sKey] then
eachLang(altLangs, args, tBlock, sKey, args[sKey])
tBlock[sKey]=true
end
end
--Process Rest
for key, value in pairs(args) do
if not(tBlock[key]) then
eachLang(altLangs, args, tBlock, key, value)
end
end
if #altLangs>0 then
if not(args.mini) then
out:wikitext(' (')
end
out:wikitext(table.concat(altLangs,'; '))
if not(args.mini) then
out:wikitext(')')
end
end
return tostring(out)
end
function p.SelectiveSpan(s, lang, if1all)
--initial check, return blank if nil
if s then else return '' end
--mw.logObject(s,'Input') --debug
--initialize tables
local span = {}
local nospan = {}
local split = {}
--separate the string through unicode codepoints as default lua split cannot handle non-UTF-8 characters
for c in mw.ustring.gcodepoint(s) do
--mw.logObject(c,'c') --debug
table.insert(split, mw.ustring.char(c))
end
--mw.logObject(split,'split') --debug
--separate the split string into ASCII and non-ASCII, include anything inside '<>' as ASCII to not mess with html tags rendering
local loopnum = 1
local html, link = false, false
while split[loopnum] do
--check for start of html tag
if split[loopnum] == '<' then
html = true
end
--check for start of link
if (split[loopnum] == '[' and split[loopnum+1] == '[') then
link = true
end
if (split[loopnum]:find('[\201-\255ÊêÔôƠơƯưẮắẤấẾếỐốỚớỨứẰằẦầỀềỒồỜờỪừẢảẲẳẨẩẺẻỂểỈỉỎỏỔổỞởỦủỬửỶỷẴẵẪẫẼẽỄễỖỗỠỡỮữỸỹẠạẶặẬậẸẹỆệỊịỌọỘộỢợỤụỰựỴỵ]') and html == false and link == false) then
span[loopnum] = split[loopnum]
else
nospan[loopnum] = split[loopnum]
end
--check for end of html tag
if split[loopnum] == '>' then
html = false
end
--check for end of link
if (split[loopnum] == '|' or (split[loopnum] == ']' and split[loopnum+1] == ']')) then
link = false
end
loopnum = loopnum + 1
end
--mw.logObject(span,'span') --debug
--mw.logObject(nospan,'nospan') --debug
if (if1all and next(span, nil) ~= nil) then return mw.html.create('span'):attr('lang', lang):wikitext(s) end
--build the string while applying the lang span to the non-ASCII characters only
local i = 1
local s = mw.html.create()
while (span[i] or nospan[i]) do
if span[i] then
local text = mw.html.create()
local try = true
text:wikitext(span[i])
while try do
if span[i+1] then
text:wikitext(span[i+1])
i = i + 1
else
try = false
end
end
s:tag('span'):attr('lang', lang):node(text):done()
elseif nospan[i] then
s:wikitext(nospan[i])
end
i = i + 1
end
--mw.logObject(s,'Output') --debug
return s
end
function p.CleanTT(tx)
local full = tx
local result = tx
for tooltip, display in string.gmatch(full, '<span[^>]+title="([^>]-)">(.-)</span>') do
search_str = '<span[^>]+title="' .. tooltip .. '">(' .. display .. ')</span><span[^>]+><span[^>]+>' .. display .. '</span><span[^>]+>' .. tooltip .. '</span></span>'
result = result:gsub(search_str, display)
end
return result
end
return p