Module:Other Languages: Difference between revisions

From Honkai: Nexus Anima Wiki
Jump to navigation Jump to search
copying star rail
Kuhlau (talk | contribs)
Undid revision 8857 by Kuhlau (talk)
Tag: Undo
 
(One intermediate revision by the same user not shown)
(No difference)

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 72.92% 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