Module:Other Languages: Difference between revisions
Jump to navigation
Jump to search
ReinDevildom (talk | contribs) Created page with "local p = {} local lib = require('Module:Feature') local KOrm = require('Module:Ko-rm') local i18n = require('Module:I18n').loadMessages('Other Languages') local SelectiveSpan = require('Module:Lang').SelectiveSpan local search = lib.inArray local ne = lib.isNotEmpty local ie = lib.isEmpty local langOrder = {'en', 'zhs', 'zht', 'ja', 'ko', 'es', 'fr', 'ru', 'th', 'vi', 'de', 'id', 'pt', 'tr', 'it'} local categories = mw.html.create() local noCat = {} loc..." |
Tag: Undo |
||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
local p | local p = {} | ||
local lib | local lib = require('Module:Feature') | ||
local SelectiveSpan = require('Module:Lang').SelectiveSpan | local SelectiveSpan = require('Module:Lang').SelectiveSpan | ||
local | local i18n = require('Module:I18n').loadMessages('Other Languages') | ||
local ne | local ne = lib.isNotEmpty | ||
local ie | local ie = lib.isEmpty | ||
local langOrder | local langOrder = { 'en', 'zhs', 'zht', 'ja', 'ko', 'es', 'fr', 'ru', 'th', 'vi', 'de', 'id', 'pt' } | ||
local categories = mw.html.create() | local categories = mw.html.create() | ||
function p.main(frame) | function p.main(frame) | ||
| Line 16: | Line 12: | ||
parentFirst = true, | parentFirst = true, | ||
removeBlanks = false, | removeBlanks = false, | ||
wrappers = {'Template:Other Languages'} | wrappers = { 'Template:Other Languages' } | ||
}) | }) | ||
return p._main(frame, args) | |||
return p._main(args | |||
end | end | ||
function p._main(frame, args) | |||
if (ne(args['1_en'])) then | |||
-- multiple OL transcripts in 1 template call: use flex to take up less space | |||
out = mw.html.create('div'):css{ ['display'] = 'flex', ['column-gap'] = '15px', ['flex-direction'] = 'row', ['flex-wrap'] = 'wrap' } | |||
function p._main(args | local ITR = 1 | ||
local itr = tostring(ITR) | |||
if ne(args['1_en']) then | while args[itr .. '_en'] do | ||
local entry = out:tag('div') | |||
out = mw.html.create('div'):css{['display']='flex', ['column-gap'] = '15px', ['flex-direction'] = 'row', ['flex-wrap'] = 'wrap'} | entry:tag('h3'):wikitext(args[itr .. '_header'] or args[itr .. '_en']) | ||
local | entry:node(p._table(frame, args, (itr .. '_'))) | ||
local | |||
while args[ | |||
local entry = out:tag('div') | |||
entry:tag('h3'):wikitext(args[ | |||
entry:node(p. | |||
ITR = ITR + 1 | |||
itr = tostring(ITR) | |||
end | end | ||
else | else | ||
out = p. | -- single OL transcript | ||
out = p._table(frame, args) | |||
end | end | ||
return tostring(out:node(require('Module:Namespace detect').main{ ["main"] = categories })) | |||
return tostring(out:node(require('Module:Namespace detect').main{["main"]=categories}) | |||
end | end | ||
function p. | function p._table(frame, args, prefix) | ||
prefix = prefix or '' | local prefix = prefix or '' | ||
-- | -- lang code span to use in transcript if it differs from base lang code | ||
local lang = { | local lang = { | ||
['zhs'] = { | ['zhs'] = { span = 'zh-Hans' }, | ||
['zht'] = { | ['zht'] = { span = 'zh-Hant' } | ||
} | } | ||
- | local articleTable = mw.html.create('table'):addClass('article-table') | ||
local tr = | |||
local tr = articleTable:tag('tr') | |||
tr:tag('th'):wikitext('Language') | tr:tag('th'):wikitext('Language') | ||
tr:tag('th'):wikitext(args | tr:tag('th'):wikitext(args['name'] or 'Official Name') | ||
for _, langCode in ipairs(langOrder) do | for _, langCode in ipairs(langOrder) do | ||
local tx = args[prefix .. langCode] or nil | |||
local | |||
local Lang = lang[langCode] or {} | local Lang = lang[langCode] or {} | ||
-- default to pagename if en transcript is not given | |||
--default to pagename if en transcript is not given | |||
if langCode == 'en' then | if langCode == 'en' then | ||
if | if tx then | ||
if ie(tx) then tx = mw.title.getCurrentTitle().text end | |||
else tx = mw.title.getCurrentTitle().text end | |||
end | end | ||
-- check if transcript exists and is not blank, otherwise add missing category and move on | |||
-- | if tx then | ||
if | if ne(tx) then | ||
if | local tr = articleTable:tag('tr') | ||
if | if langCode == 'zhs' or langCode == 'zht' then | ||
tr:tag('td'):css('line-height', '1em'):wikitext(i18n:msg(langCode)) | |||
else | |||
tr:tag('td'):wikitext(i18n:msg(langCode)) | |||
end | end | ||
local td = tr:tag('td') | local td = tr:tag('td') | ||
-- might be necessary to adjust the actual displayed tx | |||
local displayTx = tx | |||
if langCode == 'fr' then | |||
displayTx = tx | |||
:gsub(' !', ' !') -- add after/before punctuation so it does not break from the text it relates to | |||
:gsub(' %?', ' ?') -- | |||
:gsub(' :', ' :') -- | |||
:gsub(' ~', ' ~') -- | |||
displayTx = | :gsub(' %-', ' -') -- | ||
:gsub(' &', ' &') -- | |||
:gsub(' »', ' »') -- | |||
:gsub('« ', '« ') -- | |||
elseif langCode == 'ru' then | elseif langCode == 'ru' then | ||
displayTx = p.ruBreak( | displayTx = p.ruBreak(tx) -- add <wbr> after hyphens as chromium based browsers do not support Russian properly | ||
elseif langCode == 'th' then | elseif langCode == 'th' then | ||
displayTx = | displayTx = tx | ||
:gsub('%.%.%.', '...<wbr>') -- add <wbr> after elipses as they do not break properly due to not spacing in Thai, making scroll horizontally | |||
elseif langCode == 'de' then | elseif langCode == 'de' then | ||
displayTx = | displayTx = tx | ||
:gsub('%.%.%.', '...<wbr>') -- add <wbr> after elipses as they do not break properly due to not spacing in German, making scroll horizontally | |||
end | end | ||
local code = Lang.span or langCode | local code = Lang.span or langCode | ||
if tx:find('^[%s\n]*%*') then td:newline() end | |||
td:node(SelectiveSpan(displayTx, code, (code == 'vi' and true or false))) | |||
else | |||
categories:wikitext('[[Category:Incomplete Transcripts for Other Languages]][[Category:', string.upper(langCode), ' Transcript Missing]]') | |||
end | end | ||
else | else | ||
categories:wikitext('[[Category:Incomplete Transcripts for Other Languages]][[Category:', string.upper(langCode), ' Transcript Missing]]') | |||
end | end | ||
if args[prefix .. langCode .. '_rm'] ~= nil or args[prefix .. langCode .. '_tl'] ~= nil then | |||
categories:wikitext('[[Category:Pages Using Deprecated Template Parameters]]') | |||
mw.addWarning("This wiki's [[Template:Other Languages|Other Languages]] template does not support _tl or _rm parameters. '''Please disable them when generating Other Languages.'''") | |||
if | |||
end | end | ||
end | end | ||
return articleTable | |||
return | |||
end | end | ||
| Line 491: | Line 119: | ||
table.insert(split, mw.ustring.char(c)) | table.insert(split, mw.ustring.char(c)) | ||
end | end | ||
-- mw.logObject(split,'split') --debug | --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 | --separate the split string into ASCII and non-ASCII, include anything inside '<>' as ASCII to not mess with html tags rendering | ||
| Line 498: | Line 126: | ||
while split[loopnum] do | while split[loopnum] do | ||
--check for start of ref tag | --check for start of ref tag | ||
if split[loopnum] == '\ | if split[loopnum] == '\'"`' then | ||
ref = true | ref = true | ||
end | end | ||
| Line 512: | Line 140: | ||
--check for end of ref tag | --check for end of ref tag | ||
if split[loopnum] == '` | if split[loopnum] == '`"\'' then | ||
ref = false | ref = false | ||
end | end | ||
Latest revision as of 23:07, 13 July 2026
This Lua module is used primarily on pages in the Main namespace. It can be found on 544 pages, or on about 73.22% of those pages.
Edits to this Lua module will be widely noticed. To avoid large-scale disruption and unnecessary server load, any changes to this Lua module should be tested in its sandbox or testcases subpages. The tested changes can then be added to this page in one single edit by an administrator or a content moderator.
Please consider discussing any changes in the wiki's Discord.
This module implements {{Other Languages}}.
local p = {}
local lib = require('Module:Feature')
local SelectiveSpan = require('Module:Lang').SelectiveSpan
local i18n = require('Module:I18n').loadMessages('Other Languages')
local ne = lib.isNotEmpty
local ie = lib.isEmpty
local langOrder = { 'en', 'zhs', 'zht', 'ja', 'ko', 'es', 'fr', 'ru', 'th', 'vi', 'de', 'id', 'pt' }
local categories = mw.html.create()
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
removeBlanks = false,
wrappers = { 'Template:Other Languages' }
})
return p._main(frame, args)
end
function p._main(frame, args)
if (ne(args['1_en'])) then
-- multiple OL transcripts in 1 template call: use flex to take up less space
out = mw.html.create('div'):css{ ['display'] = 'flex', ['column-gap'] = '15px', ['flex-direction'] = 'row', ['flex-wrap'] = 'wrap' }
local ITR = 1
local itr = tostring(ITR)
while args[itr .. '_en'] do
local entry = out:tag('div')
entry:tag('h3'):wikitext(args[itr .. '_header'] or args[itr .. '_en'])
entry:node(p._table(frame, args, (itr .. '_')))
ITR = ITR + 1
itr = tostring(ITR)
end
else
-- single OL transcript
out = p._table(frame, args)
end
return tostring(out:node(require('Module:Namespace detect').main{ ["main"] = categories }))
end
function p._table(frame, args, prefix)
local prefix = prefix or ''
-- lang code span to use in transcript if it differs from base lang code
local lang = {
['zhs'] = { span = 'zh-Hans' },
['zht'] = { span = 'zh-Hant' }
}
local articleTable = mw.html.create('table'):addClass('article-table')
local tr = articleTable:tag('tr')
tr:tag('th'):wikitext('Language')
tr:tag('th'):wikitext(args['name'] or 'Official Name')
for _, langCode in ipairs(langOrder) do
local tx = args[prefix .. langCode] or nil
local Lang = lang[langCode] or {}
-- default to pagename if en transcript is not given
if langCode == 'en' then
if tx then
if ie(tx) then tx = mw.title.getCurrentTitle().text end
else tx = mw.title.getCurrentTitle().text end
end
-- check if transcript exists and is not blank, otherwise add missing category and move on
if tx then
if ne(tx) then
local tr = articleTable:tag('tr')
if langCode == 'zhs' or langCode == 'zht' then
tr:tag('td'):css('line-height', '1em'):wikitext(i18n:msg(langCode))
else
tr:tag('td'):wikitext(i18n:msg(langCode))
end
local td = tr:tag('td')
-- might be necessary to adjust the actual displayed tx
local displayTx = tx
if langCode == 'fr' then
displayTx = tx
:gsub(' !', ' !') -- add after/before punctuation so it does not break from the text it relates to
:gsub(' %?', ' ?') --
:gsub(' :', ' :') --
:gsub(' ~', ' ~') --
:gsub(' %-', ' -') --
:gsub(' &', ' &') --
:gsub(' »', ' »') --
:gsub('« ', '« ') --
elseif langCode == 'ru' then
displayTx = p.ruBreak(tx) -- add <wbr> after hyphens as chromium based browsers do not support Russian properly
elseif langCode == 'th' then
displayTx = tx
:gsub('%.%.%.', '...<wbr>') -- add <wbr> after elipses as they do not break properly due to not spacing in Thai, making scroll horizontally
elseif langCode == 'de' then
displayTx = tx
:gsub('%.%.%.', '...<wbr>') -- add <wbr> after elipses as they do not break properly due to not spacing in German, making scroll horizontally
end
local code = Lang.span or langCode
if tx:find('^[%s\n]*%*') then td:newline() end
td:node(SelectiveSpan(displayTx, code, (code == 'vi' and true or false)))
else
categories:wikitext('[[Category:Incomplete Transcripts for Other Languages]][[Category:', string.upper(langCode), ' Transcript Missing]]')
end
else
categories:wikitext('[[Category:Incomplete Transcripts for Other Languages]][[Category:', string.upper(langCode), ' Transcript Missing]]')
end
if args[prefix .. langCode .. '_rm'] ~= nil or args[prefix .. langCode .. '_tl'] ~= nil then
categories:wikitext('[[Category:Pages Using Deprecated Template Parameters]]')
mw.addWarning("This wiki's [[Template:Other Languages|Other Languages]] template does not support _tl or _rm parameters. '''Please disable them when generating Other Languages.'''")
end
end
return articleTable
end
function p.ruBreak(tx)
local split = {}
local allowed = true
--separate the string through unicode codepoints as default lua split cannot handle non-UTF-8 characters
for c in mw.ustring.gcodepoint(tx) do
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, ref = 0, false
while split[loopnum] do
--check for start of ref tag
if split[loopnum] == '\'"`' then
ref = true
end
--check for start of html
if (split[loopnum] == '<' and split[loopnum+1]:find('%w')) then
html = html + 1
end
if (split[loopnum]:find('%-') and html == 0 and ref == false) then
split[loopnum] = '-<wbr>'
end
--check for end of ref tag
if split[loopnum] == '`"\'' then
ref = false
end
--check for end of html
if (split[loopnum] == '|' or (split[loopnum] == ']' and split[loopnum+1] == ']')) then
link = false
end
loopnum = loopnum + 1
end
return table.concat(split, '')
end
return p