Module:Lang/utilities

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

--[[--&#x3c; E R R O R _ M S G _ M A K E &#x3e;--

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

]]

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

table.insert (out, table.concat ({'&#x3c;span style=\"color:#d33\"&#x3e;Error: : '})); table.insert (out, msg); table.insert (out, table.concat ({' (help)'})); table.insert (out, '&#x3c;/span&#x3e;'); 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 ({''})); end

return table.concat (out); end

--[[--&#x3c; S E P A R A T O R _ G E T &#x3e;

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

]]

local function separator_get (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

--[[--&#x3c; P A R A M _ S E L E C T &#x3e;--

Selects the appropriate enumerated parameters for &#x3c;text1&#x3e; or &#x3c;text2&#x3e; according to &#x3c;swap&#x3e;

&#x3c;args_t&#x3e; - arguments table from frame &#x3c;lang_args_t&#x3e; - arguments table to be supplied to _lang (whichever text renders second) &#x3c;lang_xx_args_t&#x3e; - arguments table to be supplied to _lang_xx_inherit or _lang_xx_italic (whichever text renders first)

]]

local function param_select (args_t, lang_args_t, lang_xx_args_t, swap) local enum_xx = swap and '2' or '1'; local enum = swap and '1' or '2'; lang_xx_args_t.script = args_t['script' .. enum_xx];						-- these for &#x3c;textn&#x3e; that renders first (uses ) lang_xx_args_t.region = args_t['region' .. enum_xx]; lang_xx_args_t.variant = args_t['variant' .. enum_xx]; lang_xx_args_t.italic = args_t['italic' .. enum_xx]; lang_xx_args_t.size = args_t['size' .. enum_xx]; lang_args_t.italic = args_t['italic' .. enum];								-- these for &#x3c;textn&#x3e; that renders second (uses ) lang_args_t.size = args_t['size' .. enum]; end

--[[--&#x3c; I E T F _ T A G _ M A K E &#x3e;

make ietf tag for _lang (second rendered &#x3c;textn&#x3e;)

]]

local function ietf_tag_make (args_t, code, swap) local ietf_tag_t = {code};													-- initialize with &#x3c;code&#x3e; local enum = swap and '1' or '2';											-- when &#x3c;swap&#x3e; is true latin &#x3c;text1&#x3e; is rendered second by _lang for _, param in ipairs ({'script'..enum, 'region'..enum, 'variant'..enum}) do		if args_t[param] then table.insert (ietf_tag_t, args_t[param]) end		-- build &#x3c;ietf_tag&#x3e; from &#x3c;enum&#x3e;erated subtags end

return table.concat (ietf_tag_t, '-');										-- concatenate subtags with hyphen separator and done end

--[[--&#x3c; _ L A N G _ X 2 &#x3e;--

mimics &#x3c;text1&#x3e; except that &#x3c;text2&#x3e; is not a romanization of &#x3c;text1&#x3e; (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?)

&#x3c;code&#x3e; - (required) language code for both of &#x3c;text1&#x3e; and &#x3c;text2&#x3e; &#x3c;text1&#x3e; - (required) Latin-script text (always) &#x3c;text2&#x3e; - (required) non-Latin-script text (always) |swap= - when set to 'yes', swaps the rendered order so that &#x3c;text2&#x3e; renders first followed by &#x3c;text1&#x3e;; default is Latin-script &#x3c;text1&#x3e; 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 and  templates are passed along: |cat= |nocat= parameters supported only by that are not enumerated: |label= |link= enumerated parameters: |script1= - ISO 15924 script tag for &#x3c;text1&#x3e;								-- when &#x3c;text1&#x3e; renders first, these are passed to _lang_xx_... individually |region1= - ISO 3166 region tag for &#x3c;text1&#x3e;									-- when &#x3c;text1&#x3e; renders second, these are combined with &#x3c;code&#x3e; to make an IETF tag for _lang |variant1= - IETF tag for &#x3c;text1&#x3e;

|script2= - ISO 15924 script tag for &#x3c;text2&#x3e;								-- when &#x3c;text2&#x3e; renders first, these are passed to _lang_xx_... individually |region2= - ISO 3166 region tag for &#x3c;text2&#x3e;									-- when &#x3c;text2&#x3e; renders second, these are combined with &#x3c;code&#x3e; to make an IETF tag for _lang |variant2= - IETF tag for &#x3c;text2&#x3e;

|italic1= |size1=

|italic2= |size2=

this function calls: Module:Lang functions _lang_xx_inherit - when non-Latin &#x3c;text2&#x3e; renders first _lang_xx_italic - when Latin &#x3c;text1&#x3e; renders first _lang - to render &#x3c;text2&#x3e; Module:Unicode data function: is_Latin - error checking to make sure that &#x3c;text1&#x3e; 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 error_msg_make ('missing a required argument', 'lang-x2'); end if args_t[1]:find ('-', 1, true) then return error_msg_make ('invalid language code: &#x3c;code style="color: inherit; background: inherit; border: none; padding: inherit;"&#x3e;' .. args_t[1] .. '&#x3c;/code&#x3e;; IETF format not supported', 'lang-x2'); end if not unicode.is_Latin (args_t[2]) then return error_msg_make ('&#x3c;code style="color: inherit; background: inherit; border: none; padding: inherit;"&#x3e;&#x26;lt;text1&#x3e;&#x3c;/code&#x3e; is not Latin script', 'lang-x2'); end if unicode.is_Latin (args_t[3]) then return error_msg_make ('&#x3c;code style="color: inherit; background: inherit; border: none; padding: inherit;"&#x3e;&#x26;lt;text2&#x3e;&#x3c;/code&#x3e; 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 = ietf_tag_make (args_t, code, swap);						-- for calls to 																				-- create base-form arguments tables local lang_xx_args_t = {['code']=code, ['label']=args_t.label, ['link']=args_t.link, ['cat']=args_t.cat, ['nocat']=args_t.nocat};	-- for whichever &#x3c;textn&#x3e; renders first local lang_args_t = {['code']=ietf_tags, ['cat']=args_t.cat, ['nocat']=args_t.nocat};		-- for whichever &#x3c;textn&#x3e; renders second param_select (args_t, lang_args_t, lang_xx_args_t, swap);					-- load &#x3c;lang_args_t&#x3e;, &#x3c;lang_xx_args_t&#x3e; tables with appropriate enumerated parameters from &#x3c;args_t&#x3e; table according to &#x3c;swap&#x3e;

if swap then																-- non-Latin script &#x3c;text2&#x3e; renders first lang_xx_args_t.text = text2; lang_args_t.text = text1; table.insert (out, lang._lang_xx_inherit (lang_xx_args_t));				-- render non-Latin script &#x3c;text2&#x3e; upright else																		-- Latin script &#x3c;text1&#x3e; renders first lang_xx_args_t.text = text1; lang_args_t.text = text2; table.insert (out, lang._lang_xx_italic (lang_xx_args_t));				-- render Latin script &#x3c;text1&#x3e; in italics end

table.insert (out, separator_get (args_t.separator));						-- insert the separator table.insert (out, lang._lang (lang_args_t));								-- and render the other of &#x3c;text1&#x3e; or &#x3c;text2&#x3e; return table.concat (out) end

--[[--&#x3c; L A N G _ X 2 &#x3e;

implements ; template entry point

]]

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

----&

return { lang_x2 = lang_x2, }