Jump to content

Module:Lang/utilities

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 16:51, 4 August 2021. 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 unicode = require ("Module:Unicode data");								-- for is_latin()
local lang = require ('Module:Lang');

local namespace = mw.title.getCurrentTitle().namespace;							-- used for categorization


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

assembles an error message from template name, message text, help link, and error category.

]]

local function make_error_msg (msg, template)
	local out = {};
	local category = 'Lang and lang-xx';

	table.insert (out, table.concat ({'<span style=\"color:#d33\">Error: {{', template, '}}: '}));
	table.insert (out, msg);
	table.insert (out, table.concat ({' ([[:Category:Lang and lang-xx template errors|help]])'}));
	table.insert (out, '</span>');
	
	if (0 == namespace or 10 == namespace) and not args.nocat then				-- categorize in article space (and template space to take care of broken usages)
		table.insert (out, table.concat ({'[[Category:Lang and lang-xx template errors]]'}));
	end

	return table.concat (out);
end
	

--[[--------------------------< G E T _ S E P A R A T O R >----------------------------------------------------

allowed separators are comma, semicolon, virgule, or a quoted string of text

]]

local function get_separator (sep_param_val)
	if not sep_param_val then return ', ' end;									-- not specified, use default
	if ',' == sep_param_val then return ', ' end;
	if ';' == sep_param_val then return '; ' end;
	if '/' == sep_param_val then return '/' end;
	local str;
	if sep_param_val:match ('^%b\"\"$') then									-- if quoted string and uses double quotes
		str = sep_param_val:match ('%b\"\"'):gsub ('^"', ''):gsub ('"$', '');
		return str;																-- return the contents of the quote
	elseif sep_param_val:match ('^%b\'\'$') then								-- if quoted string and uses single quotes
		str = sep_param_val:match ('%b\'\''):gsub ('^\'', ''):gsub ('\'$', '');
		return str;																-- return the contents of the quote
	end
	return ', ';																-- default in case can't extract quoted string
end


--[[--------------------------< _ L A N G _ X 2 >--------------------------------------------------------------

mimics {{lang|<code>|<text1>|<text2>}} except that <text2> is not a romanization of <text1> (which is always the
Latin-script form).  Intended for languages where two scripts are 'official' or 'native and equal in status' (sh
is and sr may be candidates for this; are there others?)

{{lang_x2|<code>|<text1>|<text2>|swap=yes|script2=<script>|separator=[,|;|/|<quoted string>]}}

	<code> - (required) language code for both of <text1> and <text2>
	<text1> - (required) Latin-script text (always)
	<text2> - (required) non-Latin-script text (always)
	|swap= - when set to 'yes', swaps the rendered order so that <text2> renders first followed by <text1>; default is Latin-script <text1> first 
	|separator = when single character ,;/ uses ', ' (default), '; ', '/'; when quoted string (first and last characters must be matching single or double quotes) uses that string

parameters supported by both of {{lang}} and {{lang-??}} templates are passed along:
	|cat=
	|nocat=
	
parameters supported only by {{lang-??}} that are not enumerated:
	|label=
	|link=
	
enumerated parameters:
	|script1= - ISO 15924 script tag for <text1>								-- when <text1> renders first, these are passed to _lang_xx_...() individually
	|region1= - ISO 3166 region tag for <text1>									-- when <text1> renders second, these are combined with <code> to make an IETF tag for _lang()
	|variant1= - IETF tag for <text1>

	|script2= - ISO 15924 script tag for <text2>								-- when <text2> renders first, these are passed to _lang_xx_...() individually
	|region2= - ISO 3166 region tag for <text2>									-- when <text2> renders second, these are combined with <code> to make an IETF tag for _lang()
	|variant2= - IETF tag for <text2>

	|rtl1=																		-- <text1> style
	|italic1=
	|size1=

	|rtl2=																		-- <text2> style
	|italic2=
	|size2=

this function calls:
	Module:Lang functions
		_lang_xx_inherit - when non-Latin <text2> renders first
		_lang_xx_italic - when Latin <text1> renders first
		_lang - to render <text2>
	Module:Unicode data function:
		is_Latin - error checking to make sure that <text1> is Latin-script text

]]

local function _lang_x2 (args_t)
	if not (args_t[1] and args_t[2] and args_t[3]) then
		return make_error_msg ('missing a required argument', 'lang-x2');
	end
	if not unicode.is_Latin (args_t[2]) then
		return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;">&lt;text1></code> is not Latin script', 'lang-x2');
	end
	if unicode.is_Latin (args_t[3]) then
		return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;">&lt;text2></code> is Latin script', 'lang-x2');
	end
	
	local swap = 'yes' == args_t.swap;											-- boolean
	local out = {};
	local code = args_t[1];
	local text1 = args_t[2];
	local text2 = args_t[3];
	local ietf_tags = {code};													-- for calls to {{lang}}
	
	if swap then																-- non-Latin <text2> renders first
		if args_t.script1 then table.insert (ietf_tags, args_t.script1) end		-- these for <text1> when swapped (<text1> renders second) (uses {{lang}})
		if args_t.region1 then table.insert (ietf_tags, args_t.region1) end
		if args_t.variant1 then table.insert (ietf_tags, args_t.variant1) end
	else																		-- Latin script <text1> renders first
		if args_t.script2 then table.insert (ietf_tags, args_t.script2) end		-- these for <text2> not swapped (<text2> renders second) (uses {{lang}})
		if args_t.region2 then table.insert (ietf_tags, args_t.region2) end
		if args_t.variant2 then table.insert (ietf_tags, args_t.variant2) end
	end
		
	ietf_tags = table.concat (ietf_tags, '-');									-- concatenate and convert to a string

																				-- create base-form arguments tables	
	local lang_xx_args = {['code']=code, ['label']=args_t.label, ['link']=args_t.link, ['cat']=args_t.cat, ['nocat']=args_t.nocat};	-- for whichever <textn> renders first
	local lang_args = {['code']=ietf_tags, ['cat']=args_t.cat, ['nocat']=args_t.nocat};		-- for whichever <textn> renders second
	
	if swap then																-- non-Latin <text2> renders first
		lang_xx_args.script = args_t.script2;									-- these for <text2> when swapped (uses {{lang-xx}})
		lang_xx_args.region = args_t.region2;
		lang_xx_args.variant = args_t.variant2;
		lang_xx_args.rtl = args_t.rtl2;
		lang_xx_args.italic = args_t.italic2;
		lang_xx_args.size = args_t.size2;
		
		lang_args.rtl = args_t.rtl1;											-- these for <text1> when swapped (<text1> renders second) (uses {{lang}})
		lang_args.italic = args_t.italic1;
		lang_args.size = args_t.size1;
	else
		lang_xx_args.script = args_t.script1;									-- these for <text1> when not swapped (<text1> renders first) (uses {{lang-xx}})
		lang_xx_args.region = args_t.region1;
		lang_xx_args.variant = args_t.variant1;
		lang_xx_args.rtl = args_t.rtl1;
		lang_xx_args.italic = args_t.italic1;
		lang_xx_args.size = args_t.size1;
		
		lang_args.rtl = args_t.rtl2;											-- these for <text2> when not swapped (<text2> renders second) (uses {{lang}})
		lang_args.italic = args_t.italic2;
		lang_args.size = args_t.size2;
	end
	
	if swap then																-- non-Latin script <text2> renders first
		lang_xx_args.text = text2;
		lang_args.text = text1;
		table.insert (out, lang._lang_xx_inherit (lang_xx_args));
		table.insert (out, get_separator (args_t.separator));
		table.insert (out, lang._lang (lang_args));
	else																		-- Latin script <text1> renders first
		lang_xx_args.text = text1;
		lang_args.text = text2;
		table.insert (out, lang._lang_xx_italic (lang_xx_args));
		table.insert (out, get_separator (args_t.separator));
		table.insert (out, lang._lang (lang_args));
	end
	
	return table.concat (out)
end


--[[--------------------------< L A N G _ X 2 >----------------------------------------------------------------

template entry point

]]

local function lang_x2 (frame)
	local args_t = getArgs (frame);
	return _lang_x2 (args_t);
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	lang_x2 = lang_x2,
	}