Jump to content

Module:Msg

From translatewiki.net

See Template:Msg-meta for the more detailed explanation.

Tests

Multi-line messages:

  • Movepagetext-noredirectfixer ("Using the form below will rename a page, moving all of its history to the new name. The old title will become a redirect page to the new title. Be sure to check for [[Special:DoubleRedirects|double]] or [[Special:BrokenRedirects|broken redirects]]. You are responsible for making sure that links continue to point where they are supposed to go. Note that the page will <strong>not</strong> be moved if there is already a page at the new title, unless it is a redirect and has no past edit history. This means that you can rename a page back to where it was renamed from if you make a mistake, and you cannot overwrite an existing page. <strong>Note:</strong> This can be a drastic and unexpected change for a popular page; please be sure you understand the consequences of this before proceeding.")
  • Abusefilter-warning ("'''Warning:''' This action has been automatically identified as harmful. Unconstructive actions will be quickly reverted, and egregious or repeated unconstructive editing will result in your account or IP address being blocked. If you believe this action to be constructive, you may submit it again to confirm it. A brief description of the abuse rule which your action matched is: $1")

With keywords:

  • Rollback-success ("Reverted edits by {{GENDER:$3|$1}}; changed back to last revision by {{GENDER:$4|$2}}.")
  • Pagecategories ("{{PLURAL:$1|Category|Categories}}")

Optional messages:


--
-- Port [[Template:Msg-meta]] to Lua
-- <nowiki>
require( 'strict' )
local p = {}

-- [[Module:Msg/styles.css]]
local stylesPage = 'Module:Msg/styles.css'

-- See [[:Category:Message maintenance]]
local errorCat = '[[Category:Message maintenance/msg-%s]]'

local function isEmpty( str )
	return str == nil or str == ''
end

local function getMessage( name, lang )
	return mw.message.new( name ):inLanguage( lang )
end

local function wrapString( str, lang, name )
	if isEmpty( name ) then
		name = 'Parentheses'
	end
	if isEmpty( lang ) then
		lang = 'en'
	end
	return getMessage( name, lang ):params( str ):plain()
end

local function getComment( comment, lang )
	if isEmpty( comment ) then
		return ''
	end
	
	return ' ' .. wrapString( comment, lang, 'Brackets' )
end

-- Accepts mw.title object
local function getLink( title, name, doesNotExist )
	local linkText = name
	if doesNotExist then
		return string.format(
			'[[:%s|%s]]',
			title.fullText,
			name
		)
	end
	
	return string.format(
		'[%s %s]',
		title:fullUrl( 'action=edit' ),
		linkText
	)
end

local function getText( title, lang )
	local text = title:getContent()

	if isEmpty( text ) then
		return false
	end
	
	text = mw.text.trim( text )
	text = mw.text.nowiki( mw.text.unstripNoWiki( text ) )
	
	-- Replace line breaks to render multi-line messages
	text = mw.ustring.gsub( text, '\n\n', '<br>' )
	text = mw.ustring.gsub( text, '\n', ' ' )
	
	local result = mw.html.create( 'code' )
		:attr( 'lang', lang )
		:wikitext( text )
	result = wrapString( tostring( result ), lang, 'Quotes' )
		
	return ' ' .. wrapString( result, lang )
end
p.getText = getText

function p._main( name, ns, comment, lang, config )
	local function getTitleObj( name, lang, ns )
		return mw.title.makeTitle( ns, string.format( '%s/%s', name, lang ) )
	end
	local title = getTitleObj( name, lang, ns )
	local class = not isEmpty( config.meta ) and 'msg-' .. config.meta or ''
	local result = mw.html.create( 'span' )
		:addClass( 'plainlinks ts-msg ' .. class )
	
	-- Optionally check for existence of English message
	local doesNotExist = false
	if config.checkIfExists then
		local origTitle = getTitleObj( name, 'en', ns )
		doesNotExist = not origTitle.exists
		
		if doesNotExist then
			-- Missing links link to English version for log access
			title = origTitle
			
			result:addClass( 'ts-msg-missing' )
			result:wikitext( string.format( errorCat, config.meta or 'meta' ) )
			
			mw.addWarning(
				'[[Module:Msg]]: ' ..
				getMessage( 'Red-link-title', lang ):params( string.format( '[[%s]]', title.fullText ) ):plain()
			)
		end
	end
	
	local link = mw.html.create( 'var' )
		:attr( 'lang', 'en' )
	local linkText = getLink( title, name, doesNotExist )
	
	local text = ''
	if not config.noText then
		text = getText( title, lang )
		if text == false then
			linkText = getLink( title, name, true )
			text = getMessage( 'Red-link-title', lang ):params( '' ):plain()
		end
	end
	
	link:wikitext( linkText )
	result
		:node( link )
		:wikitext( getComment( comment ) )
		:wikitext( text )
	
	local templatestyles = ( config.frame or mw.getCurrentFrame() ):extensionTag{
		name = 'templatestyles',
		args = { src = stylesPage },
	}
	return templatestyles .. tostring( result )
end

function p.main( frame )
	local args = require('Module:Arguments').getArgs( frame )
	local name = args[ 1 ]
	local comment = args[ 2 ]
	local ns = args[ 'namespace' ]
	local lang = args[ 'lang' ]
	local config = {
		frame = frame,
		meta = args[ 'meta' ],
		checkIfExists = isEmpty( args[ 'exist' ] ),
		noText = not isEmpty( args[ 'notext' ] ),
	}
	
	if isEmpty( name ) then
		return error( 'Please write the message key' )
	end
	
	-- Calls [[Template:RelevantLanguage]] if no language was provided
	if isEmpty( lang ) then
		lang = frame:expandTemplate{
			title = 'RelevantLanguage',
		}
	end
	
	return p._main( name, ns, comment, lang, config )
end

return p
-- </nowiki>