Jump to content

Module:Native name

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 15:27, 2 January 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals');

local getArgs = require ('Module:Arguments').getArgs;
local lang_module = require ('Module:Lang');

local defined_values = {
	italic = {['no']='no', ['off']='no'},										-- values accepted by |italic= and |italics=; {{lang}} expects 'no' so 'off' must be translated
	nbsp = {['no']=true, ['omit']=true},										-- values accepted by |nbsp=
	paren = {['no']=true, ['off']=true, ['omit']=true},							-- values accepted by |paren=
	}

local messages = {																-- for {{native name}}
	tag_required = 'Error: {{native name}}: an IETF language tag as parameter {{{1}}} is required',
	name_required = 'Error: {{native name}}: a name as parameter {{{2}}} is required',
																				-- for {{native name list}}
	empty_list = 'Error: {{native name list}}: list is empty',
	positional = 'Error: {{native name list}}: positional parameters not supported',
	}


--[[--------------------------< E R R O R _ M S G >------------------------------------------------------------

returns a formatted error message

]]

local function error_msg (msg)
	return table.concat ({'<span style="color:#d33">', msg, '</span>'});
end


--[[--------------------------< _ N A T I V E _ N A M E >------------------------------------------------------

implements {{native name}}; entry point from a module

<args_t> is a table of parameter name/value pairs.  Parameters that are supported are:
	args_t[1] - IETF language tag (required)
	args_t[2] - the native name (required)
	args_t.italic - accepts string values 'no' or 'off'; {{lang}} expects 'no' so 'off' must be translated
	args_t.italics - alias of |italic=
	args_t.paren - accepts 'omit', 'off', or 'no'
	args_t.icon - alias of paren
	args_t.nbsp - accepts 'omit' or 'no'
	args_t.parensize - 
	args_t.fontsize - deprecated alias of |parensize=
	args_t.nolink - any value inhibits wikilinking of language name

this function calls these functions in Module:lang:
	_is_ietf_tag
	_lang
	_name_from_tag

]]

local function _native_name (args_t)
	if not args_t[1] then
		return error_msg (messages.tag_required)
	end
	if not args_t[2] then
		return error_msg (messages.name_required)
	end

	args_t.italic = args_t.italics or args_t.italic;							-- plural form first in {{native name}} but singular form for {{lang}}
	args_t.italic = defined_values.italic[args_t.italic] or nil;				-- translate assigned value
	args_t.italics = nil;														-- so unset as unneeded

	args_t.paren = args_t.paren or args_t.icon;
	args_t.icon = nil;															-- unset as unneeded

	args_t.parensize = args_t.parensize or args_t.fontsize or '100%';
	args_t.fontsize = nil;														-- unset as unneeded

	local out_t = {};
--error (mw.dumpObject ({args_t[1], args_t[2], ['italic']=args_t.italic, ['template']='native name'}))
	table.insert (out_t, lang_module._lang ({args_t[1], args_t[2], ['italic']=args_t.italic, ['template']='native name'}));
	if not defined_values.paren[args_t.paren] then
		table.insert (out_t, '&nbsp;');
		if not defined_values.italic[args_t.italic] and not defined_values.nbsp[args_t.nbsp] then
			table.insert (out_t, '&nbsp;');
		end

		table.insert (out_t, table.concat ({
			'<span class="languageicon" style="font-size:',
			args_t.parensize,
			'; font-weight:normal">'}));

		if args_t.nolink then
			table.insert (out_t, table.concat ({'(', lang_module._name_from_tag ({args_t[1], ['template']='native name'}), ')'}));
		else
			if lang_module._is_ietf_tag (args_t[1]) then
				table.insert (out_t,  table.concat ({'(', lang_module._name_from_tag ({args_t[1], ['link'] ='yes', ['template']='native name'}), ')'}));
			else
				table.insert (out_t, '(language?)');
			end
		end

		table.insert (out_t, '</span>');

	end

	return table.concat (out_t);
end


--[[--------------------------< N A T I V E _ N A M E >--------------------------------------------------------

implements {{native name}}; entry point from the template

{{#invoke:native name|native_name|<tag>|<name>|italic=|paren=|parensize=|nbsp=|nolink=}}

]]

local function native_name (frame)
	return _native_name (getArgs (frame));
end


--[[--------------------------> _ N A T I V E _ N A M E _ L I S T >--------------------------------------------

implements {{native name}}; entry point from a module

<args_t> is a table of parameter name/value pairs.  Supports enumerated forms of the {{native name}} parameters:
	args_t.tagn - IETF language tag (|tag1= required)
	args_t.namen - the native name (|name1= required)
	args_t.italicn - accepts string values 'no' or 'off'
	args_t.italicsn - alias of |italicn=
	args_t.parenn - accepts 'omit', 'off', or 'no'
	args_t.iconn - alias of paren
	args_t.nbspn - accepts 'omit' or 'no'
	args_t.parensizen - 
	args_t.fontsizen - deprecated alias of |parensizen=
	args_t.nolinkn - any value inhibits wikilinking of language name

also supports:
	args_t.postfixn - wikitext to be appended to list item n (references other appropriate text)

]]

local function _native_name_list (args_t)
	if args_t[1] then
		return error_msg (messages.positional)
	end

	local list_t = {};															-- list of formatted native names goes here
	local n = 1;																-- initialize an enumerator

	while args_t['tag'..n] do
		table.insert (list_t, table.concat ({
			'<li>',																-- open the list item
			_native_name ({														-- go render the native name
				args_t['tag'..n],
				args_t['name'..n],
				['italic'] = args_t['italic'..n],
				['italics'] = args_t['italics'..n],
				['paren'] = args_t['paren'..n],
				['icon'] = args_t['icon'..n],
				['nbsp'] = args_t['nbsp'..n],
				['parensize'] = args_t['parensize'..n],
				['fontsize'] = args_t['fontsize'..n],
				['nolink'] = args_t['nolink'..n]}),
			args_t['postfix'..n] or '',
			'</li>'																-- close the list item
		}));
		n = n + 1;																-- bump the enumerator
	end

	if 0 < #list_t then
		table.insert (list_t, 1, '<div class="plainlist"><ul>');				-- open the div; open the unordered list
		table.insert (list_t, '</ul></div>');									-- close the unordered list; close the div
	else
		return error_msg (messages.empty_list);
	end
	
	return table.concat (list_t);												-- make a big string and done
end


--[[--------------------------< N A T I V E _ N A M E _ L I S T >----------------------------------------------

implements {{native name list}}; entry point from the template

{{#invoke:native name list|native_name_list|tag1=<tag>|name1=<name>|italic1=|paren1=|parensize1=|nbsp1=|nolink1=}}

]]

local function native_name_list (frame)
	return _native_name_list (getArgs (frame));
end


--[[--------------------------< E X P O R T S >----------------------------------------------------------------
]]

return {
	native_name = native_name,													-- template interface
	native_name_list = native_name_list,
	
	_native_name = _native_name,												-- other module interface
	_native_name_list = _native_name_list,
	}