Module:Lang: Difference between revisions

Content deleted Content added
sync from sandbox;
No edit summary
 
(37 intermediate revisions by 4 users not shown)
Line 8:
 
local getArgs = require ('Module:Arguments').getArgs;
local unicode = require ("Module:Unicode data"); -- for is_latinis_Latin() and is_rtl()
local yesno = require ('Module:Yesno');
 
Line 22:
 
local synonym_table = mw.loadData ('Module:Lang/ISO 639 synonyms'); -- ISO 639-2/639-2T code translation to 639-1 code
 
local cfg = mw.loadData ('Module:Lang/configuration' .. (mw.getCurrentFrame():getTitle():match ('/sandbox') or '')); -- for internationalization
 
local is_latn_data = mw.loadData ('Module:Lang/data/is latn data');
local sizeof_ranges_t = is_latn_data.sizeof_ranges_t;
 
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization
Line 34 ⟶ 39:
local maint_msgs = {}; -- and their messages go here
 
local templates = {
lang = 'Lang',
langx = 'Langx',
langxx = 'Lang-xx',
}
 
--[[--------------------------< C O M MA OK NE _ PE AR R AO MR _ S P A N >----------------------------------------------------
 
]]
 
local function make_error_span (template, msg)
local known_params_t = {
return table.concat ({'<span style="color:#d33">', cfg.misc_text_t.error, ': ', template, msg, '</span>'})
['common_params_all_t'] = { -- these parameters common to {{lang}}, {{langx}}, and {{lang-xx}}
end
['code'] = true,
['text'] = true,
['rtl'] = true,
['italic'] = true,
['italics'] = true,
['i'] = true,
['size'] = true,
['proto'] = true,
['nocat'] = true,
['cat'] = true,
 
['template'] = true, -- supplied by external templates to provide template name for error messaging ({{nihongo}}, etc)
},
['params_lang_t'] = { -- unique to {{lang}}
[1] = true, -- alias of |code=
[2] = true, -- alias of |text=
},
 
--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
['params_x_t'] = { -- common to {{langx}} and {{lang-xx}}
['translit'] = true,
['translit-std'] = true,
['translit-script'] = true,
['translation'] = true,
['lit='] = true,
['label'] = true,
['link'] = true,
['links'] = true,
['lit'] = true,
['script'] = true, -- deprecated in {{langx}}; will become unique to {{lang-xx}}
['region'] = true, -- deprecated in {{langx}}; will become unique to {{lang-xx}}
['variant'] = true, -- deprecated in {{langx}}; will become unique to {{lang-xx}}
['engvar'] = true,
},
 
Substitutes $1, $2, etc in <message> with data from <data_t>. Returns plain-text substituted string when
['params_langx_t'] = { -- unique to {{langx}}
<data_t> not nil; returns <message> else.
[1] = true, -- alias of |code=
[2] = true, -- alias of |text=
[3] = true, -- alias of |translit=
[4] = true, -- alias of |translation=
},
 
]]
['params_lang_xx_t'] = { -- unique to {{lang-xx}}
[1] = true, -- alias of |text=
[2] = true, -- alias of |translit=
[3] = true, -- alias of |translation=
},
}
 
local function substitute (message, data_t)
return data_t and mw.message.newRawMessage (message, data_t):plain() or message;
end
 
 
--[[--------------------------< 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, args_t, template)
local category;
local text; -- handle the oddity that is {{langx}}
if cfg.templates_t.langxx == template then
text = args_t.text or args_t[1]; -- for {{lang-xx}}
else
text = args_t.text or args_t[2]; -- for {{lang}}, {{langx}}, and {{transliteration}}
end
 
if cfg.templates_t.transliteration == template then
category = cfg.make_error_msg_t.xlit_err_cat;
else
category = cfg.make_error_msg_t.lang_err_cat;
end
local category_link = ((0 == namespace or 10 == namespace) and not args_t.nocat) and substitute ('[[Category:$1]]', {category}) or '';
return substitute ('[$1] <span style="color:#d33">$2: &#x7B;{$3}}: $4 ([[:Category:$5|$6]])</span>$7', -- &#x7B; prevents template name from being treated as a template call in certain situations
{
text or cfg.make_error_msg_t.undefined,
cfg.misc_text_t.error,
template,
msg,
category,
cfg.misc_text_t.help,
category_link
})
end
 
--[[--------------------------< P A R A M E T E R _ V A L I D A T E >------------------------------------------
 
]]
 
local function parameter_validate (args_t, template)
local err_msg = 'invalid parameter: |%s='cfg.parameter_validate_t.invalid_param;
 
if templatescfg.templates_t.lang == template then -- for {{lang}}
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_lang_t[param] and -- unique {{lang}} parameters
not cfg.known_params_t.common_params_all_t[param] then -- common to all
return string.formatsubstitute (err_msg, {param}); -- <param> not found so abandon
end
end
elseif templatescfg.templates_t.langx == template then -- for {{langx}}
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_langx_t[param] and -- unique {{langx}} parameters
not cfg.known_params_t.params_x_t[param] and -- common to {{langx}} and {{lang-xx}}
not cfg.known_params_t.common_params_all_t[param] then -- common to all
return string.formatsubstitute (err_msg, {param}); -- <param> not found so abandon
end
end
elseif templatescfg.templates_t.langxx == template then -- for {{lang-xx}}
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_lang_xx_t[param] and -- unique {{lang-xx}} parameters
not cfg.known_params_t.params_x_t[param] and -- common to {{langx}} and {{lang-xx}}
not cfg.known_params_t.common_params_all_t[param] then -- common to all
return string.formatsubstitute (err_msg, {param}); -- <param> not found so abandon
end
end
Line 130 ⟶ 132:
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
 
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not
an empty string.
 
]]
 
local function is_set ( var )
return not (var == nil or var == '');
end
Line 143 ⟶ 146:
This function attempts to invert the italic markup a args.text by adding/removing leading/trailing italic markup
in args.text. Like |italic=unset, |italic=invert disables automatic italic markup. Individual leading/trailing
apostrophes are converted to their htmlHTML numeric entity equivalent so that the new italic markup doesn't become
bold markup inadvertently.
 
Line 187 ⟶ 190:
--[[--------------------------< V A L I D A T E _ I T A L I C >------------------------------------------------
 
validatesValidates |italic= or |italics= assigned values.
 
When |italic= is set and has an acceptibleacceptable assigned value, return the matching cssCSS font-style property value or,
for the special case 'default', return nil.
 
When |italic= is not set, or has an unacceptibleunacceptable assigned value, return nil and a nil error message.
 
When both |italic= and |italics= are set, returns nil and a 'conflicting' error message.
 
The return value nil causes the calling lang, lang_xx, or translxlit function to set args.italic according to the template's
defined default ('inherit' for {{lang}}, 'inherit' or 'italic' for {{lang-??}} depending on
the individual template's requirements, 'italic' for {{transliteration}}) or to the value appropriate to |script=, if set ({{lang}}
Line 212 ⟶ 215:
 
local function validate_italic (args)
local properties = {['yes'cfg.keywords_t.affirmative] = 'italic', ['no'cfg.keywords_t.negative] = 'normal', ['cfg.keywords_t.unset'] = 'inherit', ['cfg.keywords_t.invert'] = 'invert', ['cfg.keywords_t.default'] = nil};
local count = 0
Line 222 ⟶ 225:
 
if count > 1 then -- return nil and an error message if more than one is set
return nil, 'only one of |italic=, |italics=, or |i= can be specified'cfg.validate_italic_t.multi_italic;
end
Line 231 ⟶ 234:
--[=[--------------------------< V A L I D A T E _ C A T _ A R G S >----------------------------------------------------------
 
Default behavior of the {{lang}} and {{lang-??}} templates is to add categorization when the templates are used in mainspacemain space.
This default functionality may be suppressed by setting |nocat=yes or |cat=no. This function selects one of these two parameters
to control categorization.
Line 263 ⟶ 266:
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------
 
Whether needle is in haystack.
 
]]
 
local function in_array ( needle, haystack )
if needle == nil then
return false;
end
for n,v in ipairs ( haystack ) do
if v == needle then
return n;
Line 282 ⟶ 285:
--[[--------------------------< F O R M A T _ I E T F _ T A G >------------------------------------------------
 
prettifyPrettify ietfIETF tags to use recommended subtag formats:
code: lower case
script: sentence case
Line 318 ⟶ 321:
--[[--------------------------< G E T _ I E T F _ P A R T S >--------------------------------------------------
 
extractsExtracts and returns IETF language tag parts:
primary language subtag (required) - 2 or 3 character IANA language code
script subtag - four character IANA script code
Line 332 ⟶ 335:
lang-x-private
each of lang, script, region, variant, and private, when used, must be valid.
 
Languages with both two- and three-character code synonyms are promoted to the two-character synonym because
Line 339 ⟶ 342:
 
For {{lang-??}} templates, the parameters |script=, |region=, and |variant= are supported (not supported in {{lang}}
because those parameters are superfluous to the IETF subtags in |code=).
 
returnsReturns six values; all lower case. Valid parts are returned as themselves; omitted parts are returned as empty strings, invalid
parts are returned as nil; the sixth returned item is an error message (if an error detected) or nil.
 
seeSee http://www.rfc-editor.org/rfc/bcp/bcp47.txt section 2.1.
 
]]
 
local function get_ietf_parts (source, args_script, args_region, args_variant)
local code, script, region, variant, private; -- ietfIETF tag parts
 
if not is_set (source) then
return nil, nil, nil, nil, nil, 'missing language tag'cfg.get_ietf_parts_t.missing_lang_tag;
end
 
local pattern = { -- table of tables holding acceptibeacceptable ietfIETF tag patterns and short names of the ietfIETF part captured by the pattern
{'^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)%-(%d%d%d%d)$', 's', 'r', 'v'}, -- 1 - ll-Ssss-RR-variant (where variant is 4 digits)
{'^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-(%d%d%d%d)$', 's', 'r', 'v'}, -- 2 - ll-Ssss-DDD-variant (where region is 3 digits; variant is 4 digits)
Line 385 ⟶ 388:
}
 
local t = {}; -- table of captures; serves as a translator between captured ietfIETF tag parts and named variables
 
for i, v in ipairs (pattern) do -- spin through the pattern table looking for a match
Line 399 ⟶ 402:
};
script = t.s or ''; -- translate table contents to named variables;
region = t.r or ''; -- absent table entries are nil so set named ietfIETF parts to empty string for concatenation
variant= t.v or '';
private = t.p or '';
Line 407 ⟶ 410:
 
if not code then
return nil, nil, nil, nil, nil, table.concatsubstitute ({'unrecognized language tag: 'cfg.get_ietf_parts_t.unrecog_tag, {source}); -- don't know what we got but it is malformed
end
 
Line 413 ⟶ 416:
if not (override_table[code] or lang_table[code] or synonym_table[code] or lang_dep_table[code]) then
return nil, nil, nil, nil, nil, table.concatsubstitute ({'unrecognized language code: 'cfg.get_ietf_parts_t.unrecog_code, {code}); -- invalid language code, don't know about the others (don't care?)
end
 
if synonym_table[code] then -- if 639-2/639-2T code has a 639-1 synonym
table.insert (maint_cats, table.concatsubstitute ({'Lang and lang-xx code promoted to ISO 639-1|'cfg.get_ietf_parts_t.maint_promo_cat, {code}));
table.insert (maint_msgs, table.concatsubstitute ({'code: 'cfg.get_ietf_parts_t.maint_promo_msg, {code, ' promoted to code: ', synonym_table[code]}));
code = synonym_table[code]; -- use the synonym
end
Line 424 ⟶ 427:
if is_set (script) then
if is_set (args_script) then
return code, nil, nil, nil, nil, 'redundant script tag'cfg.get_ietf_parts_t.redundant_scr; -- both code with script and |script= not allowed
end
else
Line 433 ⟶ 436:
script = script:lower(); -- ensure that we use and return lower case version of this
if not script_table[script] then
return code, nil, nil, nil, nil, table.concatsubstitute ({'unrecognized script: 'cfg.get_ietf_parts_t.unrecog_scr_code, {script, ' for code: ', code}); -- language code okOK, invalid script, don't know about the others (don't care?)
end
end
if suppressed_table[script] then -- ensure that code-script does not use a suppressed script
if in_array (code, suppressed_table[script]) then
return code, nil, nil, nil, nil, table.concatsubstitute ({'script: 'cfg.get_ietf_parts_t.script_code, {script, ' not supported for code: ', code}); -- language code okOK, script is suppressed for this code
end
end
Line 444 ⟶ 447:
if is_set (region) then
if is_set (args_region) then
return code, nil, nil, nil, nil, 'redundant region tag'cfg.get_ietf_parts_t.redundant_reg; -- both code with region and |region= not allowed
end
else
Line 453 ⟶ 456:
region = region:lower(); -- ensure that we use and return lower case version of this
if not region_table[region] then
return code, script, nil, nil, nil, table.concatsubstitute ({'unrecognized region: 'cfg.get_ietf_parts_t.unrecog_reg_code, {region, ' for code: ', code});
end
end
Line 459 ⟶ 462:
if is_set (variant) then
if is_set (args_variant) then
return code, nil, nil, nil, nil, 'redundant variant tag'cfg.get_ietf_parts_t.redundant_var; -- both code with variant and |variant= not allowed
end
else
Line 468 ⟶ 471:
variant = variant:lower(); -- ensure that we use and return lower case version of this
if not variant_table[variant] then -- make sure variant is valid
return code, script, region, nil, nil, table.concatsubstitute ({'unrecognized variant: 'cfg.get_ietf_parts_t.unrecog_var, {variant});
end -- does this duplicate/replace tests in lang() and lang_xx()?
if is_set (script) then -- if script set it must be part of the 'prefix'
if not in_array (table.concat ({code, '-', script}), variant_table[variant]['prefixes']) then
return code, script, region, nil, nil, table.concatsubstitute ({'unrecognized variant: 'cfg.get_ietf_parts_t.unrecog_var_code_scr, {variant, ' for code-script pair: ', code, '-', script});
end
elseif is_set (region) then -- if region set, there are some prefixes that require lang code and region (en-CA-newfound)
if not in_array (code, variant_table[variant]['prefixes']) then -- first see if lang code is all that's required (en-oxendict though en-GB-oxendict is preferred)
if not in_array (table.concat ({code, '-', region}), variant_table[variant]['prefixes']) then -- now try for lang code and region (en-CA-newfound)
return code, script, region, nil, nil, table.concatsubstitute ({'unrecognized variant: 'cfg.get_ietf_parts_t.unrecog_var_code_reg, {variant, ' for code-region pair: ', code, '-', region});
end
end
else -- cheap way to determine if there are prefixes; fonipa and others don't have prefixes; # operator always returns 0
if variant_table[variant]['prefixes'][1] and not in_array (code, variant_table[variant]['prefixes']) then
return code, script, region, nil, nil, table.concatsubstitute ({'unrecognized variant: 'cfg.get_ietf_parts_t.unrecog_var_code, {variant, ' for code: ', code});
end
end
Line 490 ⟶ 493:
private = private:lower(); -- ensure that we use and return lower case version of this
if not override_table[table.concat ({code, '-x-', private})] then -- make sure private tag is valid; note that index
return code, script, region, nil, nil, table.concatsubstitute ({'unrecognized private tag: 'cfg.get_ietf_parts_t.unrecog_pri, {private});
end
end
Line 496 ⟶ 499:
end
 
 
--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
 
substitutes $1, $2, etc in <message> with data from <data_t>. Returns plain-text substituted string when
<data_t> not nil; returns <message> else
 
]]
 
local function substitute (message, data_t)
return data_t and mw.message.newRawMessage (message, data_t):plain() or message;
end
 
 
--[[--------------------------< 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, args_t, template)
local category;
local text; -- handle the oddity that is {{langx}}
if templates.langxx == template then
text = args_t.text or args_t[1]; -- for {{lang-xx}}
else
text = args_t.text or args_t[2]; -- for {{lang}}, {{langx}}, and {{transliteration}}
end
 
if 'Transliteration' == template then
category = 'Transliteration';
else
category = 'Lang and lang-xx'
end
local category_link = ((0 == namespace or 10 == namespace) and not args_t.nocat) and table.concat ({'[[Category:', category, ' template errors]]'}) or '';
return substitute ('[$1] <span style="color:#d33">Error: {{$2}}: $3 ([[:Category:$4 template errors|help]])</span>$5',
{
text or 'undefined',
template,
msg,
category,
category_link
})
end
 
--[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------
Line 565 ⟶ 523:
--[[--------------------------< D I V _ M A R K U P _ A D D >--------------------------------------------------
 
addsAdds <i> and </i> tags to list-item text or to implied <p>..</p> text. mixedMixed not supported.
 
]]
 
local function div_markup_add (text, style)
local implied_p = {};
 
if text:find ('^\n[%*:;#]') then -- look for list markup; list markup must begin at start of text
if 'italic' == style then
Line 581 ⟶ 537:
 
if text:find ('\n+') then -- look for any number of \n characters in text
text = text:gsub ('([^\n])\n([^\n])', '%1 %2'); -- replace single newline characters with a space character which mimics mediawikiMediaWiki
 
if 'italic' == style then
text = text:gsub('[^\n]+', '<p><i>%1</i></p>'); -- insert p and italic markup tags at each impledimplied p (two or more consecutive '\n\n' sequences)
else
text = text:gsub ('[^\n]+', '<p>%1</p>'); -- insert p markup at each impledimplied p
text = text:gsub ('\n', ''); -- strip newline characters
end
Line 597 ⟶ 553:
--[[--------------------------< T I T L E _ W R A P P E R _ M A K E >------------------------------------------
 
makesMakes a <span title="<title text>"><content_text></span> or <div title="<title text>"><content_text></div> where
<title text> is in the tool-tip in the wiki's local language and <content_text> is non-local-language text in
htmlHTML markup. This because the lang= attibuteattribute applies to the content of its enclosing tag.
 
<tag> holds a string 'div' or 'span' used to choose the correct wrapping tag.
 
<tag> holds a string 'div' or 'span' used to choose the correct wrapping tag
]]
 
Line 620 ⟶ 577:
--[[--------------------------< M A K E _ T E X T _ H T M L >--------------------------------------------------
 
Add the htmlHTML markup to text according to the type of content that it is: <span> or <i> tags for inline content or
<div> tags for block content
 
Line 643 ⟶ 600:
end
 
if 'span' == tag then -- default htmlHTML tag for inline content
if 'italic' == style then -- but if italic
tag = 'i'; -- change to <i> tags
Line 651 ⟶ 608:
end
 
table.insert (html_t, table.concat ({'<', tag})); -- open the <i>, <span>, or <div> htmlHTML tag
code = code:gsub ('%-x%-.*', ''); -- strip private use subtag from code tag because meaningless outside of wikipediaWikipedia
table.insert (html_t, table.concat ({' lang="', code, '\"'})); -- add language attribute
 
Line 675 ⟶ 632:
end
 
table.insert (html_t, table.concat ({style_added, '>'})); -- close the opening htmlHTML tag
table.insert (html_t, text); -- insert the text
 
table.insert (html_t, table.concat ({'</', tag, '>'})); -- close the 'text' <i>, <span>, or <div> htmlHTML tag
 
if is_set (language) then -- create a <title_text> string for the title= attribute in a wrapper span or div
local title_text;
if 'zxx' == code then -- special case for this tag 'no linguistic content'
title_text = table.concatsubstitute ('$1 $2', {language, ' text'cfg.make_text_html_t.zxx}); -- not a language so don't use 'language' in title text
elseif mw.ustring.find (language, 'languages', 1, true) then
title_text = table.concatsubstitute ('$1 $2', {language, ' cfg.make_text_html_t.collective text'}); -- for collective languages
else
title_text = table.concatsubstitute ('$1-$2', {language, '-language text'cfg.make_text_html_t.individual}); -- for individual languages
end
 
Line 702 ⟶ 659:
[[Category:Articles containing <language>-language text]]
 
forFor English:
[[Category:Articles containing explicitly cited English-language text]]
forFor ISO 639-2 collective languages (and for 639-1 bh):
[[Category:Articles with text in <language> languages]]
 
Line 711 ⟶ 668:
 
local function make_category (code, language_name, nocat, name_get)
local cat = {};
local retval;
 
if ((0 ~= namespace) or nocat) and not name_get then -- only categorize in article space
return ''; -- return empty string for concatenation
Line 719 ⟶ 673:
 
if mw.ustring.find (language_name, 'languages', 1, true) then
return substitute ('[[$1 $2]]', {cfg.make_category_t.collective_cat, language_name});
return table.concat ({'[[Category:Articles with text in ', language_name, ']]'});
end
table.insert (cat, '[[Category:Articles containing ');
 
if this_wiki_lang_tag == code then
table.insertreturn substitute (cat, 'explicitly[[$1 cited$2 $3-$4]]', .. language_name);{ -- unique category name for the local language
cfg.make_category_t.cat_prefix,
cfg.make_category_t.explicit_cat,
language_name,
cfg.make_category_t.cat_postfix,
});
else
return substitute ('[[$1 $2-$3]]', { -- category for individual languages
table.insert (cat, language_name);
cfg.make_category_t.cat_prefix,
language_name,
cfg.make_category_t.cat_postfix,
});
end
table.insert (cat, '-language text]]');
 
return table.concat (cat);
end
 
Line 738 ⟶ 695:
--[[--------------------------< M A K E _ T R A N S L I T >----------------------------------------------------
 
returnReturn translit <i lang=xx-Latn>...</i> where xx is the language code; else return empty string.
 
The value |script= is not used in {{transliteration}} for this purpose; instead it uses |code. Because language scripts
Line 745 ⟶ 702:
 
To avoid confusion, in this module and the templates that use it, the transliteration script parameter is renamed
to be |translit-script= (in this function, tscript).
 
This function is used by both lang_xx() and translxlit()
lang_xx() always provides code, language_name, and translit; may provide tscript; never provides style
translxlit() always provides language_name, translit, and one of code or tscript, never both; always provides style
 
For {{transliteration}}, style only applies when a language code is provided.
 
For {{transliteration}}, style only applies when a language code is provided
]]
 
local function make_translit (code, language_name, translit, std, tscript, style, engvar)
local title;
local out_t = {};
local title_t = lang_data.translit_title_table; -- table of transliteration standards and the language codes and scripts that apply to those standards
local title_text = ''; -- tool tip text for title= attribute
Line 765 ⟶ 721:
title_text = language_name; -- write a generic tool tip
if not mw.ustring.find (language_name, 'languages', 1, true) then -- collective language names (plural 'languages' is part of the name)
title_text = substitute ('$1-$2', {title_text, cfg.misc_text_t. '-language'}); -- skip this text (individual and macro languages only)
end
title_text = substitute ('$1 $2', {title_text, lang_datamw.ustring.lower (cfg.engvar_t[engvar]['romanisz_lc'])}); -- finish the tool tip; use romanization when neither script nor standard supplied
 
elseif is_set (std) and is_set (tscript) then -- when both are specified
Line 773 ⟶ 729:
if title_t[std][tscript] then -- and if script for that standard is legitimate
if script_table[tscript] then
title_text = title_text .. table.concatsubstitute ({title_t[std][tscript:lower()], '$1$2 (',$3 script_table[tscript],$4) $5', script) transliteration'});{ -- add the appropriate text to the tool tip
title_text,
title_t[std][tscript:lower()],
script_table[tscript],
cfg.make_translit_t.script,
cfg.make_translit_t.transliteration,
});
else
title_text = title_text .. title_t[std]['default']; -- use the default if script not in std table; TODO: maint cat? error message because script not found for this standard?
Line 787 ⟶ 749:
if not title_t[std] then return ''; end -- invalid standard, setup for error message
if title_t[std][code] then -- if language code is in the table (translxlit may not provide a language code)
title_text = title_text .. table.concatsubstitute ({title_t[std][code:lower()], '$1$2 (',$3 language_name,$4) $5', language) transliteration'});{ -- add the appropriate text to the tool tip
title_text,
title_t[std][code:lower()],
language_name,
cfg.misc_text_t.language,
cfg.make_translit_t.transliteration,
});
else -- code doesn't match
title_text = title_text .. title_t[std]['default']; -- so use the standard's default
Line 800 ⟶ 768:
else
if is_set (tscript) then
title_text = title_text .. table.concatsubstitute ({language_name, '$1$2-script$3 transliteration$4'});, { -- write a script tool tip
title_text,
language_name,
cfg.make_translit_t.script,
cfg.make_translit_t.transliteration,
});
 
elseif is_set (code) then
if not mw.ustring.find (language_name, 'languages', 1, true) then -- collective language names (plural 'languages' is part of the name)
title_text = title_text ..substitute ('$1-language$2';, { -- skip this text (individual and macro languages only)
title_text,
cfg.misc_text_t.language,
});
end
title_text = title_textsubstitute ..('$1 $2', transliteration';{ -- finish the tool tip
title_text,
cfg.make_translit_t.transliteration,
});
else
title_text = title_textsubstitute ..('$1 $2', transliteration';{ -- generic tool tip (can we ever get here?)
title_text,
cfg.make_translit_t.transliteration,
});
end
end
end
 
local close_tag;tag
if is_set (code) then -- when a language code is provided (always with {{lang-??}} templates, not always with {{transliteration}})
code = code:match ('^(%a%a%a?)'); -- strip all subtags leaving only the language subtag
 
if not style then -- nil for the default italic style
table.inserttag (out_t,= "'<i lang=\"%s-Latn"); -- so use >%s</i> tag'
close_tag = '</i>'; -- tag to be used when closing
else
table.inserttag (out_t, table.concat= ({'<span style=\"font-style: ', style, '\%s" lang=\"%s-Latn">%s</span>'})); -- non-standard style, construct a span tag for it
tag = string.format(tag, style, "%s", "%s")
close_tag = '</span>'; -- tag to be used when closing
end
table.inserttag = string.format(out_ttag, code, "%s");
table.insert (out_t, "-Latn\">"); -- transliterations are always Latin script
else
table.inserttag (out_t,= "'<span>");%s</span>' -- when no language code: no lang= attribute, not italic ({{transliteration}} only)
close_tag = '</span>';
end
 
table.inserttag = string.format(out_ttag, translit); -- add the translit text
table.insert (out_t, close_tag); -- and add the appropriate </i> or </span>
 
if '' == title_text then -- when there is no need for a tool-tip
return table.concat (out_t)tag; -- make a string and done
else
title_text = 'gb_t'cfg.engvar_sel_t.gb == engvar and title_text:gsub ('([Rr]omani)z', '%1s') or title_text; -- gb eng when engvar specifies gb eng; us eng else
return title_wrapper_make (title_text, table.concat (out_t)tag, 'span'); -- wrap with a tool-tip span and done
end
end
Line 845 ⟶ 822:
 
This function checks the content of args.text and returns empty string if nothing is amiss else it returns an
error message. The tests are for empty or missing text and for improper or disallowed use of apostrophe markup.
 
Italic rendering is controlled by the |italic= template parameter so italic markup should never appear in args.text
Line 854 ⟶ 831:
local function validate_text (template, args)
if not is_set (args.text) then
return make_error_msg ('no text'cfg.validate_text_t.no_text, args, template);
end
 
if args.text:find ("%f[\']\'\'\'\'%f[^\']") or args.text:find ("\'\'\'\'\'[\']+") then -- because we're looking, look for 4 appostrophes or 6+ appostrophes
return make_error_msg ('text has malformed markup'cfg.validate_text_t.malformed_markup, args, template);
end
 
local style = args.italic;
 
if ('cfg.keywords_t.unset' ~= style) and ('cfg.keywords_t.invert' ~=style) then
if args.text:find ("%f[\']\'\'%f[^\']") or args.text:find ("%f[\']\'\'\'\'\'%f[^\']") then -- italic but not bold, or bold italic
return make_error_msg ('text has italic markup'cfg.validate_text_t.italic_markup, args, template);
end
end
Line 873 ⟶ 850:
--[[--------------------------< R E N D E R _ M A I N T >------------------------------------------------------
 
renderRender mainenance messages and categories.
 
]]
Line 900 ⟶ 877:
--[[--------------------------< P R O T O _ P R E F I X >------------------------------------------------------
 
forFor proto languages, text is prefixed with a splat. We do that here as a flag for make_text_html() so that a splat
will be rendered outside of italic markup (if used). If the first character in text here is already a splat, we
do nothing.
 
proto_param is boolean or nil; true adds splat prefix regardless of language name; false removes and / or inhibits
regardless of language name; nil does nothing; presumes that the value in text is correct but removes extra splac.
 
]]
Line 922 ⟶ 899:
--[[--------------------------< H A S _ P O E M _ T A G >------------------------------------------------------
 
looksLooks for a poem strip marker in text; returns true when found; false else.
 
autoAuto-italic detection disabled when text has poem stripmarker because it is not possible for this code to know
the content that will replace the stripmarker.
 
Line 936 ⟶ 913:
--[[--------------------------< H T M L _ T A G _ S E L E C T >------------------------------------------------
 
Inspects content of and selectively trims text. Returns text and the name of an appropriate htmlHTML tag for text.
 
If text contains:
Line 979 ⟶ 956:
--[[--------------------------< V A L I D A T E _ P R O T O >--------------------------------------------------
 
validatesValidates value assigned to |proto=; permitted values are yes and no; yes returns as true, no returns as false,
empty string (or parameter omitted) returns as nil; any other value returns as nil with a<proto_param> as second return value of
return value for use in error message.
true indicating that some other value has been assigned to |proto=
 
]]
 
local function validate_proto (proto_param)
if 'yes'cfg.keywords_t.affirmative == proto_param then
return true;
elseif 'no'cfg.keywords_t.negative == proto_param then
return false;
elseif is_set (proto_param) then
return nil, trueproto_param; -- |proto= something other than 'yes' or 'no'
else
return nil; -- missing or empty
Line 1,000 ⟶ 977:
--[[--------------------------< L A N G U A G E _ N A M E _ G E T >--------------------------------------------
 
commonCommon function to return language name from the data set according to IETF tag.
 
returnsReturns language name if found in data tables; nil else.
 
]]
Line 1,015 ⟶ 992:
elseif override_table[code] then -- not there so try basic language tag
name = override_table[code];
elseif lang_table[code] then -- shift to ianaIANA active tag/name table
name = lang_table[code];
elseif lang_dep_table[code] then -- try the ianaIANA deprecated tag/name table
name = lang_dep_table[code];
end
 
if lang_dep_table[code] and cat then -- because deprecated code may have been overridden to en.wiki preferred name
table.insert (maint_cats, table.concatsubstitute ({'Lang and lang-xx using deprecated ISO 639 codes|'cfg.language_name_get_t.deprecated_cat, {code}));
table.insert (maint_msgs, table.concatsubstitute ({'code: 'cfg.language_name_get_t.deprecated_msg, {code, ' is deprecated'}));
end
 
return name; -- name from data tables or nil
end
 
 
--[[--------------------------< T E X T _ S C R I P T _ M A T C H _ T E S T >----------------------------------
 
IETF script subtag should match the script of the <text>. This module does not attempt to know all scripts and
what they look like. It does know what Latn script looks like so when <text> is written using other than the Latn
script, the IETF script subtag, if present, should not be Latn.
 
Conversely, when <text> is written using the Latn script, the IETF script subtag, if present, should be Latn.
 
For the purposes of this test, Latf (Fraktur) and Latg (Gaelic) are considered to be equivalent to Latn because
unicode treats these two scripts as font-specific variants of Latn.
 
Returns an error message when mismatch detected; nil else.
 
]]
 
local function text_script_match_test (script, is_latn_text, pos, char)
local scripts_t = {['latf'] = true, ['latg'] = true, ['latn'] = true}; -- unicode 'latn' scripts; 'latf' and 'latg' are font variants so there are no Fraktur or Gaelic codepoints
if is_set (script) then -- don't bother with the rest of this if <script> is nil or empty string
script = script:lower(); -- lower case to index into <scripts_t>
if is_latn_text then -- when text is wholly Latn script
if not scripts_t[script] then -- but a non-Latn script is specified
return cfg.text_script_match_test_t.latn_txt_mismatch; -- emit an error message
end
else -- when text is not wholly Latn script
if scripts_t[script] then -- but a Latn script is specified
return substitute (cfg.text_script_match_test_t.latn_scr_mismatch, {pos, char}); -- emit an error message with position of first offending character
end
end
end
end
 
 
--[[--------------------------< B I N A R Y _ S E A R C H >---------------------------------------------------
 
conducts a binary search of <ranges_t> for a sub-range that holds <target>.
 
returns boolean true if a sub-range holding <target> is found; boolean false else.
 
]]
 
local function binary_search (target, ranges_t)
local idx_bot = 1; -- initialize to index of first key
local idx_top = sizeof_ranges_t; -- initialize to index of last key (number of keys)
 
if (target < ranges_t[idx_bot][1]) or (target > ranges_t[idx_top][2]) then -- invalid; target out of range
return; -- abandon
end
 
local idx_mid; -- calculated index of range midway between top index and bottom index
local flag = false; -- flag to tell us when we've evaluated last (highest) range in <ranges_t>
 
while 1 do
idx_mid = math.ceil ((idx_bot + idx_top) / 2); -- get the mid-point in the <ranges_t> sequence
if (target >= ranges_t[idx_mid][1]) and (target <= ranges_t[idx_mid][2]) then -- indexed range low value <= target <= indexed range high value
return true; -- we found the range that holds the <target> character; return true
elseif (target > ranges_t[idx_mid][2]) then -- is <target> > indexed range high value?
idx_bot = idx_mid; -- adjust <idx_bot> up
 
else -- here when <target> less than indexed range low value
idx_top = idx_mid - 1; -- adjust <idx_top> down
end
 
if flag then
break; -- here when we just evaluated the last range and <target> not found
end
if not flag and (idx_bot == idx_top) then -- set true just before we evaluate the last (highest) range in <ranges_t>
flag = true;
end
end
end
 
 
--[[--------------------------< I S _ L A T I N >--------------------------------------------------------------
 
compare <text> as codepoints to lists of known codepoints accepted as Latn script
 
returns boolean true and modified <text> when <text> is wrapped in accept-as-written markup
 
returns boolean true and <text> when codepoint is known
 
returns boolean false, <text>, non-Latn codepoint position in <text> (left to right), and the codepoint character
when codepoint is not known
 
TODO: when text has accept-as-written markup, return a non-boolean value to indicate that <text> is not wholly
latn script? Use that return value to create non-Latn HTML lang= attribute because <text> isn't really
latn so lang=und (undetermined)? or instead, omit the -Latn subtag? (without -Latn need to force |italic=yes)
 
]]
 
local function is_latin (text, tag)
local count;
text, count = text:gsub ('^%(%((.+)%)%)$', '%1'); -- remove accept-as-written markup if present
if 0 ~= count then
return true, text; -- markup present so assume that <text> is Latn-script
end
 
local pos = 0; -- position counter for error messaging
for codepoint in mw.ustring.gcodepoint (text) do -- fetch each code point
pos = pos + 1; -- bump the position counter
 
if not is_latn_data.singles_t[codepoint] and -- codepoint not found in the singles list?
not binary_search (codepoint, is_latn_data.ranges_t) and -- codepoint not a member of a listed range?
not (tag and is_latn_data.specials_t[codepoint] and is_latn_data.specials_t[codepoint][tag]) then -- not a language-specific codepoint?
return false, text, pos, mw.ustring.char (codepoint); -- codepoint not known; return false with codepoint position and character representation
end
end
return true, text; -- is known; return <text>
end
 
Line 1,032 ⟶ 1,121:
--[[--------------------------< _ L A N G >--------------------------------------------------------------------
 
entryEntry point for {{lang}}.
 
thereThere should be no reason to set parameters in the {{lang}} {{#invoke:}}
<includeonly>{{#invoke:lang|lang}}</includeonly>
 
parametersParameters are received from the template's frame (parent frame).
 
]]
Line 1,049 ⟶ 1,138:
local msg; -- for error messages
local tag = 'span'; -- initial value for make_text_html()
local template = args.template or templatescfg.templates_t.lang;
 
maint_cats = {}; -- initialize because when this module required into another module, these only declared once so only initialzed once
Line 1,057 ⟶ 1,146:
 
if args[1] and args.code then
return make_error_msg ('conflicting:substitute (cfg.lang_t.conflict_n_param, {{{'1}}}', and |cfg.lang_t.conflict_n_param_types.code='}), args, template);
else
args.code = args[1] or args.code; -- prefer args.code
Line 1,063 ⟶ 1,152:
 
if args[2] and args.text then
return make_error_msg ('conflicting:substitute (cfg.lang_t.conflict_n_param, {{{'2}}}', and |cfg.lang_t.conflict_n_param_types.text='}), args, template);
else
args.text = args[2] or args.text; -- prefer args.text
end
 
msg = parameter_validate (args, templates.lang);
if msg then
return make_error_msg (msg, args, templates.lang);
end
 
Line 1,078 ⟶ 1,162:
end
args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate htmlHTML tag with text trimmed accordingly
 
args.rtl = args.rtl == 'yes'cfg.keywords_t.affirmative; -- convert to boolean: 'yes' -> true, other values -> false
 
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag
if msg then
return make_error_msg (table.concatsubstitute ({'invalid |proto=: 'cfg.lang_t.invalid_proto, args.proto{msg}), args, template);
end
 
Line 1,090 ⟶ 1,174:
 
if msg then
return make_error_msg ( msg, args, template);
end
 
local is_latn_text, pos, char;
is_latn_text, args.text, pos, char= is_latin (args.text, code); -- make a boolean
 
msg = text_script_match_test (subtags.script, is_latn_text, pos, char)
if msg then -- if an error detected then there is an error message
return make_error_msg (msg, args, template);
end
 
Line 1,100 ⟶ 1,192:
if nil == args.italic then -- nil when |italic= absent or not set or |italic=default; args.italic controls
if ('latn' == subtags.script) or -- script is latn
(this_wiki_lang_tag ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and unicode.is_Latin (args.text)is_latn_text) then -- text not this wiki's language, no script specified and not in poem markup but is wholly latn script (auto-italics)
args.italic = 'italic'; -- DEFAULT for {{lang}} templates is upright; but if latn script set for font-style:italic
else
Line 1,116 ⟶ 1,208:
 
args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles
language_name = language_name_get (args.code, code, true); -- get language name; try ietfIETF tag first, then code w/o variant then code w/ variant
 
if 'cfg.keywords_t.invert' == args.italic and 'span' == tag then -- invert only supported for in-line content
args.text = invert_italics (args.text)
end
Line 1,134 ⟶ 1,226:
--[[--------------------------< L A N G >----------------------------------------------------------------------
 
entryEntry point for {{lang}}.
 
thereThere should be no reason to set parameters in the {{lang}} {{#invoke:}}
<includeonly>{{#invoke:lang|lang}}</includeonly>
 
parametersParameters are received from the template's frame (parent frame).
 
]]
 
local function lang (frame)
local args_t = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text
valueFunc = function (key, value)
if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space
Line 1,157 ⟶ 1,249:
end -- end of valueFunc
});
 
args_t.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang|...}}
 
local msg = parameter_validate (args_t, cfg.templates_t.lang); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
return make_error_msg (msg, args_t, cfg.templates_t.lang); -- when template has unsupported params, abandon with error message
end
 
return _lang (args_t);
Line 1,164 ⟶ 1,263:
--[[--------------------------< T R A N S L A T I O N _ M A K E >----------------------------------------------
 
standStand-alone function to create literal translation of main text.
 
Also used by {{lang-x2}}.
 
]]
Line 1,174 ⟶ 1,273:
 
if 'none' ~= args_t.label then -- if we want a label
table.insert (translation_t, '<small>'); -- open the <small> htmlHTML tag
if 'no'cfg.keywords_t.negative == args_t.link then
table.insert (translation_t, substitute ('<abbr title="literal translation$1">lit.$2</abbr>', {cfg.translation_make_t.lit_xlation, cfg.translation_make_t.lit_abbr})); -- unlinked form
else
table.insert (translation_t, make_wikilink ('Literal translation'cfg.translation_make_t.lit_xlation, 'litcfg.translation_make_t.'lit_abbr)); -- linked form
end
table.insert (translation_t, "&thinsp;</small>"); -- close the <small> htmlHTML tag
end
table.insert (translation_t, table.concat ({'&#39;', args_t.translation, '&#39;'})); -- use htmlHTML entities to avoid wiki markup confusion
return table.concat (translation_t); -- make a big string and done
end
Line 1,231 ⟶ 1,330:
]]
 
local function _lang_xx (args, base_template) -- base_template will be either of 'Langxlangx' or 'Langlang-xx'
local out = {};
local language_name; -- used to make display text, article links
Line 1,240 ⟶ 1,339:
local translit_script_name; -- name associated with IANA (ISO 15924) script code
local translit;
local translit_title;
local msg; -- for error messages
local tag = 'span'; -- initial value for make_text_html()
Line 1,248 ⟶ 1,346:
maint_msgs = {};
local text_idx = (templatescfg.templates_t.langx == base_template) and 2 or 1; -- for {{langx}} 'text' positional parameter is '2'
local translit_idx = (templatescfg.templates_t.langx == base_template) and 3 or 2;
local xlate_idx = (templatescfg.templates_t.langx == base_template) and 4 or 3;
 
if args[text_idx] and args.text then
return make_error_msg ('conflicting: {{{'substitute (cfg.lang_t.conflict_n_param, text_idx{translit_idx, cfg.lang_t. 'conflict_n_param_types.text}}} and |text='), args, template);
else
args.text = args[text_idx] or args.text; -- prefer positional 'text' parameter
Line 1,263 ⟶ 1,361:
end
 
args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate htmlHTML tag with text trimmed accordingly
 
if args[translit_idx] and args.translit then
return make_error_msg ('conflicting:substitute {{{'(cfg.lang_t.conflict_n_param, {translit_idx, cfg.lang_t. 'conflict_n_param_types.translit}}} and |translit='), args, template);
 
else
args.translit = args[translit_idx] or args.translit -- prefer positional 'translit' parameter
end
 
args.engvar = lang_datacfg.engvar_sel_t[args.engvar] or 'us_t'cfg.default_engvar; -- either 'gb_t' or 'us_t' when |engvar= when valid; 'us_t'cfg.default_engvar else
 
if args[xlate_idx] and (args.translation or args.lit) then
return make_error_msg ('conflicting:substitute {{{'(cfg.lang_xx_t.conflict_n_lit, xlate_idx ..'{translit_idx}}} and |lit= or |translation='), args, template);
elseif args.translation and args.lit then
return make_error_msg ('conflicting: |lit= and |translation='cfg.lang_xx_t.conflict_lit, args, template);
else
args.translation = args[xlate_idx] or args.translation or args.lit; -- prefer positional 'translation' parameter
Line 1,282 ⟶ 1,381:
 
if args.links and args.link then
return make_error_msg ('conflicting: |links= and |link='cfg.lang_xx_t.conflict_link, args, template);
else
args.link = args.link or args.links; -- prefer args.link
Line 1,289 ⟶ 1,388:
validate_cat_args (args); -- determine if categorization should be suppressed
 
args.rtl = args.rtl == 'yes'cfg.keywords_t.affirmative; -- convert to boolean: 'yes' -> true, other values -> false
 
code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code, args.script, args.region, args.variant); -- private omitted because private
Line 1,296 ⟶ 1,395:
return make_error_msg (msg, args, template);
end
if args.translit then
local latn, pos, char;
latn, args.translit, pos, char = is_latin (args.translit, (('' ~= subtags.private) and (code .. '-x-' .. subtags.private)) or code);
if not latn then
return make_error_msg (substitute (cfg.lang_xx_t.translit_nonlatn, {pos, char}), args, template);
end
end
 
local is_latn_text, text, pos, char = is_latin (args.text, code); -- make a boolean
args.text = text; -- may have been modified (accept-as-written markup removed)
 
msg = text_script_match_test (subtags.script, is_latn_text, pos, char)
if msg then -- if an error detected then there is an error message
return make_error_msg (msg, args, template);
end
 
args.italic, msg = validate_italic (args);
if msg then
Line 1,302 ⟶ 1,417:
end
if templatescfg.templates_t.langx == base_template then -- auto-italics for {{langx}} templates; adapted from {{lang}} (no support for poem tag)
if nil == args.italic then -- nil when |italic= absent or not set or |italic=default; args.italic controls
if ('latn' == subtags.script) or -- script is latn
(this_wiki_lang_tag ~= code and not is_set (subtags.script) and unicode.is_Latin (args.text)is_latn_text) then -- text is not this wiki's language, no script specified and is wholly latn script (auto-italics)
args.italic = 'italic'; -- set font-style:italic
else
Line 1,336 ⟶ 1,451:
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag
if msg then
return make_error_msg (table.concatsubstitute ({'invalid |proto=: 'cfg.lang_t.invalid_proto, {args.proto}), args, template);
end
 
args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles
language_name = language_name_get (args.code, code, true); -- get language name; try ietfIETF tag first, then code w/o variant then code w/ variant
 
category_name = language_name; -- category names retain IANA parenthetical diambiguators (if any)
Line 1,350 ⟶ 1,465:
end
else
if 'no'cfg.keywords_t.negative == args.link then
table.insert (out, language_name); -- language name without wikilink
else
if mw.ustring.find (language_name, 'languages', 1, true) then
table.insert (out, make_wikilink (language_name)); -- collective language name uses simple wikilink
elseif lang_data.article_name[args.code:lower()] then -- is ietfIETF tag in article name over ride?
table.insert (out, make_wikilink (lang_data.article_name[args.code:lower()], language_name)); -- language name with wikilink from override data
elseif lang_data.article_name[code] then -- is language tag in article override
table.insert (out, make_wikilink (lang_data.article_name[code], language_name)); -- language name with wikilink from override data
else
table.insert (out, make_wikilink (language_namesubstitute ..('$1 $2', {language_name, cfg.misc_text_t.language'}), language_name)); -- language name with wikilink
end
end
Line 1,366 ⟶ 1,481:
end
 
if 'cfg.keywords_t.invert' == args.italic then
args.text = invert_italics (args.text)
end
Line 1,374 ⟶ 1,489:
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, ('none' == args.label) and language_name or nil))
 
if is_set (args.translit) and not unicode.is_Latin (args.text)is_latn_text then -- transliteration (not supported in {{lang}}); not supported when args.text is wholly latn text (this is an imperfect test)
table.insert (out, ', '); -- comma to separate text from translit
if 'none' ~= args.label then
Line 1,383 ⟶ 1,498:
translit_script_name = language_name; -- fall back on language name
end
 
translit_title = mw.title.makeTitle (0, table.concat ({'Romanization of ', translit_script_name})); -- make a title object; no engvar, article titles use US spelling
local translit_title_obj = mw.title.makeTitle (0, substitute ('$1 $2',{cfg.lang_xx_t.romanization, translit_script_name})); -- make a title object; no engvar, article titles use US spelling
if translit_title.exists and ('no' ~= args.link) then
if translit_title_obj.exists and (cfg.keywords_t.negative ~= args.link) then
table.insert (out, make_wikilink ('Romanization of ' .. translit_script_name or language_name,
table.insert (out, make_wikilink (substitute ('$1 $2', {cfg.lang_xx_t.romanization, translit_script_name or language_name}),
substitute ('$1', {lang_data.engvar_t[args.engvar]['romanisz_pt']})) .. ':'); -- make a wikilink if there is an article to link to; engvar the display text
substitute ('$1', {cfg.engvar_t[args.engvar]['romanisz_pt']})) .. ':'); -- make a wikilink if there is an article to link to; engvar the display text
 
else
table.insert (out, substitute ('$1:', {lang_datacfg.engvar_t[args.engvar]['romanisz_pt']})); -- else plain text per engvar
 
end
Line 1,399 ⟶ 1,515:
table.insert (out, translit);
else
return make_error_msg (table.concatsubstitute ({'invalid translit-std$1: \'$2', {cfg.lang_xx_t.invalid_xlit_std, args['translit-std'] or '[missing]'}), args, template);
end
end
elseif is_set (args.translit) and is_latn_text then -- when creating a transliteration of Latn script <text>
return make_error_msg (cfg.lang_xx_t.xlit_of_latn, args, template); -- emit error message and abandon
end
 
if is_set (args.translation) then -- translation (not supported in {{lang}})
table.insert (out, translation_make (args));
Line 1,408 ⟶ 1,527:
table.insert (out, make_category (code, category_name, args.nocat));
if (templates.langx == base_template) and args.script or args.region or args.variant then
table.insert (maint_cats, 'Langx deprecated parameters');
table.insert (maint_msgs, '{{langx}} uses deprecated parameter(s)');
end
table.insert (out, render_maint(args.nocat)); -- maintenance messages and categories
 
Line 1,420 ⟶ 1,535:
--[[--------------------------< L A N G _ X X _ A R G S _ G E T >----------------------------------------------
 
commonCommon function to get args table from {{lang-??}} templates.
 
returnsReturns table of args.
 
textText positional parameters are not trimmed here but are selectively trimmed at html_tag_select().
 
]]
Line 1,433 ⟶ 1,548:
parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}}
valueFunc = function (key, value)
if ((templatescfg.templates_t.langx == base_template) and 2 or 1) == key then -- the 'text' positional parameter; 1 for {{lang-??}}, 2 for {{langx}}; do not trim wite space
return value; -- return untrimmed 'text' positional parameter
elseif value then -- all other values: if the value is not nil
Line 1,451 ⟶ 1,566:
--[[--------------------------< L A N G _ X X _ I T A L I C >--------------------------------------------------
 
Entry point for those {{lang-??}} templates that call lang_xx_italic(). Sets the initial style state to italic.
 
]]
 
local function lang_xx_italic (frame)
local args = lang_xx_args_get (frame, templatescfg.templates_t.langxx);
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_italic|...}}
 
local msg = parameter_validate (args, templates.langxx);
local msg = parameter_validate (args, cfg.templates_t.langxx); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
return make_error_msg (msg, args, templatescfg.templates_t.langxx); -- when template has unsupported params, abandon with error message
end
 
initial_style_state = 'italic';
return _lang_xx (args, templatescfg.templates_t.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
end
 
Line 1,470 ⟶ 1,586:
--[[--------------------------< _ L A N G _ X X _ I T A L I C >------------------------------------------------
 
Entry point from another module. Sets the initial style state to italic.
 
]]
Line 1,476 ⟶ 1,592:
local function _lang_xx_italic (args)
initial_style_state = 'italic';
return _lang_xx (args, templatescfg.templates_t.langxx);
end
 
Line 1,482 ⟶ 1,598:
--[[--------------------------< L A N G _ X X _ I N H E R I T >------------------------------------------------
 
Entry point for those {{lang-??}} templates that call lang_xx_inherit(). Sets the initial style state to inherit.
 
]]
 
local function lang_xx_inherit (frame)
local args = lang_xx_args_get (frame, templatescfg.templates_t.langxx);
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_inherit|...}}
 
local msg = parameter_validate (args, templatescfg.templates_t.langxx); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
return make_error_msg (msg, args, templatescfg.templates_t.langxx); -- when template has unsupported params, abandon with error message
end
 
initial_style_state = 'inherit';
return _lang_xx (args, templatescfg.templates_t.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
end
 
Line 1,501 ⟶ 1,618:
--[[--------------------------< _ L A N G _ X X _ I N H E R I T >----------------------------------------------
 
Entry point from another module. Sets the initial style state to inherit.
 
]]
Line 1,507 ⟶ 1,624:
local function _lang_xx_inherit (args)
initial_style_state = 'inherit';
return _lang_xx (args, templatescfg.templates_t.langxx);
end
 
Line 1,520 ⟶ 1,637:
local langx_data = mw.loadData ('Module:Lang/langx'); -- get necessary data
local rtl_t = langx_data.rtl_t; -- get list of language tags for languages that are rendered right-to-left
local script_t = langx_data.script_t; -- get list of language tags for {{lang-??}} templates that set |script=<something>
local link_t = langx_data.link_t; -- get list of language tags for {{lang-??}} templates that set |link=<something>
local size_t = langx_data.size_t; -- get list of language tags for {{lang-??}} templates that set |size=<something>
 
local msg = parameter_validate (args_t, templatescfg.templates_t.langx);
if msg then
return make_error_msg (msg, args_t, templatescfg.templates_t.langx);
end
 
args_t.code = args_t[1] or args_t.code; -- get the language tag; must be {{{1}}} or |code=
if not args_t.code then
return make_error_msg ('missing language tag'cfg.get_ietf_parts_t.missing_lang_tag, args_t, templatescfg.templates_t.langx);
end
args_t.rtl = args_t.rtl or (rtl_t[args_t.code] and 'yes'cfg.keywords_t.affirmative); -- prefer |rtl= in template call, use rtl_t else
args_t.script = args_t.script or script_t[args_t.code]; -- prefer |script= in template call, use script_t else
args_t.link = args_t.link or link_t[args_t.code]; -- prefer |link= in template call, use link_t felse
args_t.size = args_t.size or size_t[args_t.code]; -- prefer |size= in template call, use size_t else
Line 1,542 ⟶ 1,657:
 
local lang_subtag = args_t.code; -- use only the base language subtag for unsupported tag test; some args_t.code are modified by |script= etc
return _lang_xx (args_t, cfg.templates_t.langx);
return _lang_xx (args_t, templates.langx) .. ((langx_data.unsupported_t[lang_subtag:lower()] and '[[Category:Langx uses unsupported language tag|'.. lang_subtag .. ']]') or ''); -- temporary category for unsupported language tags
-- return _lang_xx (args_t, templates.langx);
end
 
Line 1,551 ⟶ 1,665:
Entry point for {{langx}}.
 
this function calls _lang_xx() to render non-English text. The {{lang-??}} templates have three positional paramters
but {{langx}} has four:
 
| 1 | 2 | 3 | 4
{{lang-xx |<text> |<translxlit> |<xlatexlat> }}
{{langx |<tag> |<text> |<translxlit> |<xlatexlat> }}
 
The calls to lang_xx_args_get() and _lang_xx() use 'Langxlangx' as a flag for those functions to select the proper
positional parameters.
 
Line 1,564 ⟶ 1,678:
 
{{langx}} can't do that. The initial version of {{langx}} relied on a list of language tags (inherit_t in ~/langx)
scraped from those {{lang-??}} templates that call lang_xx_inherit() to render text in upright font. Langxlangx() now
uses auto-italics code adapted from {{lang}} (doesn't support poem tags).
 
]]
 
local function langx (frame)
local args_t = lang_xx_args_get (frame, templatescfg.templates_t.langx); -- get the arguments; 'Langxlangx' is the <base_template> used to decide which positional param is 'text', 'translit', 'lit'
return _langx (args_t);
Line 1,582 ⟶ 1,696:
All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function.
 
Module entry point from another module.
 
]]
Line 1,596 ⟶ 1,710:
--[[--------------------------< I S _ I E T F _ T A G >--------------------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
 
local function is_ietf_tag (frame)
return _is_ietf_tag (getArgs (frame)[1]); -- args[1] is the ietfIETF language tag to be tested; also get parent frame
end
 
Line 1,614 ⟶ 1,728:
 
local function is_ietf_tag_frame (frame)
return _is_ietf_tag (getArgs (frame, {frameOnly = true,})[1]); -- args[1] is the ietfIETF language tag to be tested; do not get parent frame
end
 
Line 1,626 ⟶ 1,740:
Set invoke's |link= parameter to yes to get wikilinked version of the language name.
 
Module entry point from another module.
 
]]
Line 1,633 ⟶ 1,747:
local subtags = {}; -- IETF subtags script, region, variant, and private
local raw_code = args[1]; -- save a copy of the input IETF subtag
local link = 'yes'cfg.keywords_t.affirmative == args['link']; -- make a boolean
local label = args.label;
local code; -- the language code
Line 1,642 ⟶ 1,756:
if msg then
local template = (args['template'] and table.concat ({'{{', args['template'], '}}: '})) or ''; -- make template name (if provided by the template)
return table.concatmake_error_span ({'<span style=\"color:#d33\">Error: ', template, msg, '</span>'});
end
 
raw_code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles; private omitted because private
language_name = language_name_get (raw_code, code); -- get language name; try ietfIETF tag first, then code w/o variant then code w/ variant
 
if 'yes' ~= args.raw then
language_name = language_name:gsub ('%s+%b()', ''); -- remove IANA parenthetical disambiguators or qualifiers from names that have them
language_name = language_name:gsub ('%s+%b()', ''); -- remove IANA parenthetical disambiguators or qualifiers from names that have them
end
 
if link then -- when |link=yes, wikilink the language name
if mw.ustring.find (language_name, 'languages', 1, true) then
language_name = make_wikilink (language_name, label); -- collective language name uses simple wikilink
elseif lang_data.article_name[raw_code:lower()] then -- is ietfIETF tag in article name override?
language_name = make_wikilink (lang_data.article_name[raw_code:lower()], label or language_name); -- language name with wikilink from override data
elseif lang_data.article_name[code] then -- is language tag in article name override?
language_name = make_wikilink (lang_data.article_name[code], label or language_name); -- language name with wikilink from override data
else
language_name = make_wikilink (language_namesubstitute ..('$1 $2', {language_name, cfg.misc_text_t.language'}), label or language_name); -- language name with wikilink
end
end
Line 1,668 ⟶ 1,784:
--[[--------------------------< N A M E _ F R O M _ T A G >----------------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
 
local function name_from_tag (frame) -- entry point from an {{#invoke:Langlang|name_from_tag|<ietfIETF tag>|link=<yes>|template=<template name>}}
return _name_from_tag (getArgs(frame)) -- pass-on the args table, nothing else; getArgs() so we also get parent frame
end
Line 1,679 ⟶ 1,795:
--[[--------------------------< _ T A G _ F R O M _ N A M E >--------------------------------------------------
 
Returns the ietfIETF language tag associated with the language name. Spelling of language name must be correct
according to the spelling in the source tables. When a standard language name has a parenthetical disambiguator,
that disambiguator must be omitted (they are not present in the data name-to-tag tables).
 
Module entry point from another module.
 
]]
Line 1,698 ⟶ 1,814:
return tag, true; -- language name found so return tag and done; second return used by is_lang_name()
else
msg = 'language: 'substitute (cfg.tag_from_name_t.lang_not_found, {args[1] .. ' not found' }); -- language name not found, error message
end
else
msg = 'missing language name' cfg.tag_from_name_t.missing_lang_name; -- language name not providedfound, error message
end
 
Line 1,708 ⟶ 1,824:
template = table.concat ({'{{', args['template'], '}}: '}); -- make template name (if provided by the template)
end
return table.concatmake_error_span ({'<span style=\"color:#d33\">Error: ', template, msg, '</span>'});
 
end
 
Line 1,714 ⟶ 1,831:
--[[--------------------------< T A G _ F R O M _ N A M E >----------------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
 
local function tag_from_name (frame) -- entry point from an {{#invoke:Langlang|tag_from_name|<language name>|link=<yes>|template=<template name>}}
local result, _ = _tag_from_name (getArgs(frame)) -- pass-on the args table, nothing else; getArgs() so we also get parent frame; supress second return used by is_lang_name()
return result;
Line 1,726 ⟶ 1,843:
--[[--------------------------< I S _ L A N G _ N A M E >------------------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
Line 1,736 ⟶ 1,853:
 
 
--[[--------------------------< _ TX RL AI N S LT >--------------------------------------------------------------------
 
Module entry point from another module.
 
]]
 
local function _transl_xlit (args)
local title_table = lang_data.translit_title_table; -- table of transliteration standards and the language codes and scripts that apply to those standards
local language_name; -- language name that matches language code; used for tool tip
Line 1,748 ⟶ 1,865:
local script; -- IANA script
local msg; -- for when called functions return an error message
local template = args.template or cfg.templates_t.transliteration;
 
maint_cats = {}; -- initialize because when this module required into another module, these only declared once so only initialzed once
Line 1,757 ⟶ 1,875:
 
if not title_table[args.translit_std] then
return make_error_msg (table.concatsubstitute ({'unrecognized transliteration standard: 'cfg.transl_t.unrecog_xlit_std, {args.translit_std}), args, 'Transliteration'template);
end
else
Line 1,765 ⟶ 1,883:
if args[1] and (args[1]:match ('^%a%a%a?%a?$') or -- args[2] missing; is args[1] a language or script tag or is it the transliterated text?
args[1]:match ('^%a%a%a?%-x%-')) then -- or is args[1] a private-use tag
return make_error_msg ('no text'cfg.transl_t.no_text, args, 'Transliteration'template); -- args[1] is a code so we're missing text
else
args.text = args[1]; -- args[1] is not a code so we're missing that; assign args.text for error message
return make_error_msg ('missing language / script code'cfg.transl_t.missing_lang_scr, args, 'Transliteration'template);
end
end
end
 
if is_set (args[1]) then -- IANA language code used for htmlHTML lang= attribute; or ISO 15924 script code
if args[1]:match ('^%a%a%a?%a?$') or args[1]:match ('^%a%a%a?%-x%-') then -- args[1] has correct form?
args.code = args[1]:lower(); -- use the language/script code; only (2, 3, or 4 alpha characters) or private-use; lower case because table indexes are lower case
else
return make_error_msg (table.concatsubstitute ({'unrecognized language / script code: 'cfg.transl_t.unrecog_lang_scr, {args[1]}), args, 'Transliteration'template); -- invalid language / script code
end
else
return make_error_msg ('missing language / script code'cfg.transl_t.missing_lang_scr, args, 'Transliteration'template); -- missing language / script code so quit
end
 
local is_latn_text, pos, char;
is_latn_text, args.text, pos, char= is_latin (args.text, args.code); -- is latn text? strip accept-as-written markup
if not is_latn_text then -- when text is not latn
return make_error_msg (substitute (cfg.lang_xx_t.translit_nonlatn, {pos, char}), args, template); -- abandon with error message
end
 
 
args.italic, msg = validate_italic (args);
if msg then
return make_error_msg (msg, args, 'Transliteration'template);
end
Line 1,792 ⟶ 1,917:
end
 
args.engvar = lang_datacfg.engvar_sel_t[args.engvar] or 'us_t'cfg.default_engvar; -- either 'gb_t' or 'us_t' when |engvar= when valid; 'us_t'cfg.default_engvar else
 
if override_table[args.code] then -- is code a language code defined in the override table?
Line 1,806 ⟶ 1,931:
args.code = ''; -- unset because not a language code
else
return make_error_msg (table.concatsubstitute ({'unrecognized language / script code: 'cfg.transl_t.unrecog_lang_scr, {args.code}), args, 'Transliteration'template); -- invalid language / script code
end
-- here only when all parameters passed to make_translit() are valid
Line 1,813 ⟶ 1,938:
 
 
--[[--------------------------< TX RL AI N S LT >----------------------------------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
 
local function translxlit (frame)
return _transl_xlit (getArgs(frame));
end
 
Line 1,826 ⟶ 1,951:
--[[--------------------------< C A T E G O R Y _ F R O M _ T A G >--------------------------------------------
 
Returns category name associated with IETF language tag if valid; error message else.
 
All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function.
 
Module entry point from another module.
 
]]
Line 1,837 ⟶ 1,962:
local subtags = {}; -- IETF subtags script, region, variant, and private
local raw_code = args_t[1]; -- save a copy of the input IETF subtag
local link = 'yes'cfg.keywords_t.affirmative == args_t.link; -- make a boolean
local label = args_t.label;
local code; -- the language code
Line 1,846 ⟶ 1,971:
if msg then
local template = (args_t.template and table.concat ({'{{', args_t.template, '}}: '})) or ''; -- make template name (if provided by the template)
return table.concatmake_error_span ({'<span style="color:#d33">Error: ', template, msg, '</span>'});
end
 
raw_code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles; private omitted because private
category_name = language_name_get (raw_code, code); -- get language name; try ietfIETF tag first, then code w/o variant then code w/ variant
category_name = make_category (code, category_name, nil, true):gsub ('[%[%]]', '');
 
Line 1,863 ⟶ 1,988:
--[[--------------------------< C A T E G O R Y _ F R O M _ T A G >--------------------------------------------
 
Module entry point from an {{#invoke:}}.
 
]]
 
local function category_from_tag (frame) -- entry point from an {{#invoke:Langlang|category_from_tag|<ietfIETF tag>|template=<template name>}}
return _category_from_tag (getArgs (frame)); -- pass-on the args table, nothing else; getArgs() so we also get parent frame
end
Line 1,876 ⟶ 2,001:
 
return {
category_from_tag = category_from_tag, -- frame entry points when this module is #invoke:ed into templates/wikitext
lang = lang, -- entry point for {{lang}}
langx = langx, -- entry point for {{langx}}
Line 1,884 ⟶ 2,009:
is_ietf_tag_frame = is_ietf_tag_frame,
is_lang_name = is_lang_name,
tag_from_name = tag_from_name, -- returns ietfIETF tag associated with language name
name_from_tag = name_from_tag, -- used for template documentation; possible use in ISO 639 name from code templates
translxlit = translxlit, -- entry point for {{transliteration}}
_category_from_tag = _category_from_tag, -- API entry points when this module is require()d into other modules
_lang = _lang,
_langx = _langx,
Line 1,897 ⟶ 2,022:
_tag_from_name = _tag_from_name,
_name_from_tag = _name_from_tag,
_transl_xlit = _transl_xlit,
_translation_make = translation_make,
};