Module:Lang/utilities: Difference between revisions

Content deleted Content added
create (again);
 
No edit summary
 
(17 intermediate revisions by 3 users not shown)
Line 1:
require('Module:No globalsstrict');
 
local getArgs = require ('Module:Arguments').getArgs;
Line 7:
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization
 
local err_texts = { -- because wrapper templates have Latn/non-Latn text ordered according to the wrapper template's name
['lang-sh-cyrl-latn'] = true, -- this template has |1=cyrl script |2=latn script
}
 
--[[--------------------------< M A K E _ E R R O R _ M S G >--------------------------------------------------
 
--[[--------------------------< E R R O R _ M S G _ M A K E >--------------------------------------------------
assembles an error message from template name, message text, help link, and error category.
 
assembles an error message from template name, message text, help link, and error category.
<span style="font-family: monospace, monospace;">{{', template, '}}</span>
]]
 
local function make_error_msgerror_msg_make (msg, template, nocat)
local out = {};
local category = 'Lang and lang-xx';
 
table.insert (out, table.concat ({'<span style=\"color:#d33\">Error: <span style="font-family: monospace, monospace;">{{', template, '}}</span>: '}));
table.insert (out, msg);
table.insert (out, table.concat ({' ([[Template:Category:Lang', andtemplate, lang-xx template'#Error errorsmessages|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
Line 31 ⟶ 35:
 
--[[--------------------------< G E T _ S E P A R A T O R _ G E T >----------------------------------------------------
 
allowed separators are comma, semicolon, virgule, or a quoted string of text
Line 37 ⟶ 41:
]]
 
local function get_separatorseparator_get (sep_param_val)
if not sep_param_val then return ', ' end; -- not specified, use default
if ',' == sep_param_val then return ', ' end;
Line 51 ⟶ 55:
end
return ', '; -- default in case can't extract quoted string
end
 
 
--[[--------------------------< P A R A M _ S E L E C T >------------------------------------------------------
 
Selects the appropriate enumerated parameters for <text1> or <text2> according to <swap>
 
<args_t> - arguments table from frame
<lang_args_t> - arguments table to be supplied to _lang() (whichever text renders second)
<lang_xx_args_t> - 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 <textn> that renders first (uses {{lang-xx}})
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 <textn> that renders second (uses {{lang}})
lang_args_t.size = args_t['size' .. enum];
end
 
 
--[[--------------------------< I E T F _ T A G _ M A K E >----------------------------------------------------
 
make ietf tag for _lang() (second rendered <textn>)
 
]]
 
local function ietf_tag_make (args_t, tag, swap)
local ietf_tag_t = {tag}; -- initialize with <tag>
local enum = swap and '1' or '2'; -- when <swap> is true latin <text1> 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 <ietf_tag> from <enum>erated subtags
end
 
return table.concat (ietf_tag_t, '-'); -- concatenate subtags with hyphen separator and done
end
 
Line 56 ⟶ 103:
--[[--------------------------< _ L A N G _ X 2 >--------------------------------------------------------------
 
mimics {{lang|<codetag>|<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 candidatesa candidate for this; are there others?)
 
{{lang_x2|<codetag>|<text1>|<text2>|firstswap=[1|2]yes|script2=<script>|separator=[,|;|/|<quoted string>]}}
 
<codetag> - (required) language codetag for both of <text1> and <text2>
<text1> - (required) Latin-script text (always)
<text2> - (required) non-Latin-script text (always)
|firstswap= - thewhen assignedset valueto specifies'yes', whichswaps ofthe <text1>rendered order orso that <text2> is renderedrenders first; acceptsfollowed '1'by or '2'<text1>; default '1'is (Latin-script text);<text1> first
|script2= - ISO 15924 script tag for <text2>
|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 <tag> 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 <tag> to make an IETF tag for _lang()
|variant2= - IETF tag for <text2>
 
|italic1=
|size1=
 
|italic2=
|size2=
 
this function calls:
Line 80 ⟶ 149:
 
local function _lang_x2 (args_t)
iflocal nottag (args_t[1] and= args_t[2].tag andor args_t[31]) then;
local text1 = args_t.text1 or args_t[2];
return make_error_msg ('missing a required argument', 'lang-x2');
local text2 = args_t.text2 or args_t[3];
local translation = args_t.translation or args_t[4];
local template_name = args_t.template_name or 'lang-x2';
local nocat = ('yes' == args_t.nocat) or ('no' == args_t.cat); -- boolean
 
if not (tag and text1 and text2) then
return error_msg_make ('missing a required argument', template_name, nocat);
end
if not unicode.is_Latintag:find (args_t[2]'-', 1, true) then
return error_msg_make ('invalid language tag: <span style="font-family: monospace, monospace;">' .. tag .. '</span>; IETF format not supported', template_name, nocat);
return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;">&lt;text1></code> is not Latin script', 'lang-x2');
end
iflocal is_latn, pos = unicode.is_Latin (args_t[3]text1) then;
if not is_latn then
return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;">&lt;text2></code> is Latin script', 'lang-x2');
return error_msg_make ('<span style="font-family: monospace, monospace;">&lt;' .. ((err_texts[template_name:lower()] and 'text2') or 'text1') .. '></span> is not Latin script (pos ' .. pos .. ')', template_name, nocat);
end
is_latn, pos = unicode.is_Latin (text2);
if is_latn then
return error_msg_make ('<span style="font-family: monospace, monospace;">&lt;' .. ((err_texts[template_name:lower()] and 'text1') or 'text2') .. '></span> is Latin script (pos ' .. pos .. ')', template_name, nocat);
end
local swap = 'yes' == args_t.swap; -- boolean
local first = args_t.first or '1';
local out = {};
local code = args_t[1];
local latin = args_t[2];
local text2 = args_t[3];
local script2 = (args_t.script2 and ('-' .. args_t.script2) or '');
 
local ietf_tags = ietf_tag_make (args_t, tag, swap); -- for calls to {{lang}}
if '1' == first then -- Latin script renders first
-- create base-form arguments tables
table.insert (out, lang._lang_xx_italic ({['code']=code, ['text']=latin}));
local lang_xx_args_t = {['code']=tag, ['label']=args_t.label, ['link']=args_t.link, ['cat']=args_t.cat, ['nocat']=args_t.nocat, ['cat']=args_t.cat}; -- for whichever <textn> renders first
table.insert (out, get_separator (args_t.separator));
local lang_args_t = {['code']=ietf_tags, ['cat']=args_t.cat, ['nocat']=args_t.nocat, ['cat']=args_t.cat}; -- for whichever <textn> renders second
table.insert (out, lang._lang ({['code']=code..script2, ['text']=text2}));
else -- non-Latin script renders first
param_select (args_t, lang_args_t, lang_xx_args_t, swap); -- load <lang_args_t>, <lang_xx_args_t> tables with appropriate enumerated parameters from <args_t> table according to <swap>
table.insert (out, lang._lang_xx_inherit ({['code']=code..script2, ['text']=text2}));
 
table.insert (out, get_separator (args_t.separator));
if swap then -- non-Latin script <text2> renders first
table.insert (out, lang._lang ({['code']=code, ['text']=latin}));
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 <text2> upright
else -- Latin script <text1> 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 <text1> 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 <text1> or <text2>
if translation then -- if positional parameter 4 (translation of <text1> and <text2>)
table.insert (out, lang._translation_make ({['translation'] = translation, ['label']=args_t.label, ['link']=args_t.link})); -- add translation to rendering
end
 
return table.concat (out)
end
Line 113 ⟶ 204:
--[[--------------------------< L A N G _ X 2 >----------------------------------------------------------------
 
implements {{lang-x2}}; template entry point
 
]]