Jump to content

Module:Lang: Difference between revisions

m 1 revision imported
m 1 revision imported
 
(5 intermediate revisions by 3 users not shown)
Line 8: Line 8:


local getArgs = require ('Module:Arguments').getArgs;
local getArgs = require ('Module:Arguments').getArgs;
local unicode = require ("Module:Unicode data"); -- for is_latin() and is_rtl()
local unicode = require ("Module:Unicode data"); -- for is_Latin() and is_rtl()
local yesno = require ('Module:Yesno');
local yesno = require ('Module:Yesno');


Line 23: Line 23:
local synonym_table = mw.loadData ('Module:Lang/ISO 639 synonyms'); -- ISO 639-2/639-2T code translation to 639-1 code
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'); -- for internationalization
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
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization
Line 36: Line 39:
local maint_msgs = {}; -- and their messages go here
local maint_msgs = {}; -- and their messages go here


local templates = {
 
lang = 'Lang',
--[[--------------------------< M A K E _ E R R O R _ S P A N >--------------------------------------------------
langx = 'Langx',
 
langxx = 'Lang-xx',
]]
}
 
local function make_error_span (template, msg)
return table.concat ({'<span style="color:#d33">', cfg.misc_text_t.error, ': ', template, msg, '</span>'})
end




--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
--[[--------------------------< 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
Substitutes $1, $2, etc in <message> with data from <data_t>. Returns plain-text substituted string when
<data_t> not nil; returns <message> else
<data_t> not nil; returns <message> else.


]]
]]
Line 57: Line 63:
--[[--------------------------< M A K E _ E R R O R _ M S G >--------------------------------------------------
--[[--------------------------< 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.
Assembles an error message from template name, message text, help link, and error category.


]]
]]
Line 64: Line 70:
local category;
local category;
local text; -- handle the oddity that is {{langx}}
local text; -- handle the oddity that is {{langx}}
if templates.langxx == template then
if cfg.templates_t.langxx == template then
text = args_t.text or args_t[1]; -- for {{lang-xx}}
text = args_t.text or args_t[1]; -- for {{lang-xx}}
else
else
Line 70: Line 76:
end
end


if 'Transliteration' == template then
if cfg.templates_t.transliteration == template then
category = cfg.make_error_msg_t.xlit_err_cat;
category = cfg.make_error_msg_t.xlit_err_cat;
else
else
Line 76: Line 82:
end
end
local category_link = ((0 == namespace or 10 == namespace) and not args_t.nocat) and substitute ('[[Category:$1 $2]]', {category, cfg.make_error_msg_t.template_errs}) or '';
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: {{$3}}: $4 ([[:Category:$5 template errors|help]])</span>$6',
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,
text or cfg.make_error_msg_t.undefined,
Line 84: Line 90:
msg,
msg,
category,
category,
cfg.misc_text_t.help,
category_link
category_link
})
})
Line 90: Line 97:


--[[--------------------------< P A R A M E T E R _ V A L I D A T E >------------------------------------------
--[[--------------------------< P A R A M E T E R _ V A L I D A T E >------------------------------------------
]]
]]


Line 95: Line 103:
local err_msg = cfg.parameter_validate_t.invalid_param;
local err_msg = cfg.parameter_validate_t.invalid_param;


if templates.lang == template then -- for {{lang}}
if cfg.templates_t.lang == template then -- for {{lang}}
for param, _ in pairs (args_t) do
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_lang_t[param] and -- unique {{lang}} parameters
if not cfg.known_params_t.params_lang_t[param] and -- unique {{lang}} parameters
Line 102: Line 110:
end
end
end
end
elseif templates.langx == template then -- for {{langx}}
elseif cfg.templates_t.langx == template then -- for {{langx}}
for param, _ in pairs (args_t) do
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_langx_t[param] and -- unique {{langx}} parameters
if not cfg.known_params_t.params_langx_t[param] and -- unique {{langx}} parameters
Line 110: Line 118:
end
end
end
end
elseif templates.langxx == template then -- for {{lang-xx}}
elseif cfg.templates_t.langxx == template then -- for {{lang-xx}}
for param, _ in pairs (args_t) do
for param, _ in pairs (args_t) do
if not cfg.known_params_t.params_lang_xx_t[param] and -- unique {{lang-xx}} parameters
if not cfg.known_params_t.params_lang_xx_t[param] and -- unique {{lang-xx}} parameters
Line 129: Line 137:
]]
]]


local function is_set( var )
local function is_set (var)
return not (var == nil or var == '');
return not (var == nil or var == '');
end
end
Line 138: Line 146:
This function attempts to invert the italic markup a args.text by adding/removing leading/trailing italic markup
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
in args.text.  Like |italic=unset, |italic=invert disables automatic italic markup.  Individual leading/trailing
apostrophes are converted to their html numeric entity equivalent so that the new italic markup doesn't become
apostrophes are converted to their HTML numeric entity equivalent so that the new italic markup doesn't become
bold markup inadvertently.
bold markup inadvertently.


Line 182: Line 190:
--[[--------------------------< V A L I D A T E _ I T A L I C >------------------------------------------------
--[[--------------------------< V A L I D A T E _ I T A L I C >------------------------------------------------


validates |italic= or |italics= assigned values.
Validates |italic= or |italics= assigned values.


When |italic= is set and has an acceptible assigned value, return the matching css font-style property value or,
When |italic= is set and has an acceptable assigned value, return the matching CSS font-style property value or,
for the special case 'default', return nil.
for the special case 'default', return nil.


When |italic= is not set, or has an unacceptible assigned value, return nil and a nil error message.
When |italic= is not set, or has an unacceptable assigned value, return nil and a nil error message.


When both |italic= and |italics= are set, returns nil and a 'conflicting' 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 transl function to set args.italic according to the template's
The return value nil causes the calling lang, lang_xx, or xlit function to set args.italic according to the template's
defined default ('inherit' for {{lang}}, 'inherit' or 'italic' for {{lang-??}} depending on
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}}
the individual template's requirements, 'italic' for {{transliteration}}) or to the value appropriate to |script=, if set ({{lang}}
Line 207: Line 215:


local function validate_italic (args)
local function validate_italic (args)
local properties = {[cfg.keywords_t.affirmative] = 'italic', [cfg.keywords_t.negative] = 'normal', ['unset'] = 'inherit', ['invert'] = 'invert', ['default'] = nil};
local properties = {[cfg.keywords_t.affirmative] = 'italic', [cfg.keywords_t.negative] = 'normal', [cfg.keywords_t.unset] = 'inherit', [cfg.keywords_t.invert] = 'invert', [cfg.keywords_t.default] = nil};
local count = 0
local count = 0
Line 226: Line 234:
--[=[--------------------------< V A L I D A T E _ C A T _ A R G S >----------------------------------------------------------
--[=[--------------------------< 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 mainspace.
Default behavior of the {{lang}} and {{lang-??}} templates is to add categorization when the templates are used in main space.
This default functionality may be suppressed by setting |nocat=yes or |cat=no.  This function selects one of these two parameters
This default functionality may be suppressed by setting |nocat=yes or |cat=no.  This function selects one of these two parameters
to control categorization.
to control categorization.
Line 258: Line 266:
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------


Whether needle is in haystack
Whether needle is in haystack.


]]
]]


local function in_array ( needle, haystack )
local function in_array (needle, haystack)
if needle == nil then
if needle == nil then
return false;
return false;
end
end
for n,v in ipairs( haystack ) do
for n,v in ipairs (haystack) do
if v == needle then
if v == needle then
return n;
return n;
Line 277: Line 285:
--[[--------------------------< F O R M A T _ I E T F _ T A G >------------------------------------------------
--[[--------------------------< F O R M A T _ I E T F _ T A G >------------------------------------------------


prettify ietf tags to use recommended subtag formats:
Prettify IETF tags to use recommended subtag formats:
code: lower case
code: lower case
script: sentence case
script: sentence case
Line 313: Line 321:
--[[--------------------------< G E T _ I E T F _ P A R T S >--------------------------------------------------
--[[--------------------------< G E T _ I E T F _ P A R T S >--------------------------------------------------


extracts and returns IETF language tag parts:
Extracts and returns IETF language tag parts:
primary language subtag (required) - 2 or 3 character IANA language code
primary language subtag (required) - 2 or 3 character IANA language code
script subtag - four character IANA script code
script subtag - four character IANA script code
Line 327: Line 335:
lang-x-private
lang-x-private
each of lang, script, region, variant, and private, when used, must be valid
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
Languages with both two- and three-character code synonyms are promoted to the two-character synonym because
Line 334: Line 342:


For {{lang-??}} templates, the parameters |script=, |region=, and |variant= are supported (not supported in {{lang}}
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=)
because those parameters are superfluous to the IETF subtags in |code=).


returns six  values; all lower case.  Valid parts are returned as themselves; omitted parts are returned as empty strings, invalid
Returns 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.
parts are returned as nil; the sixth returned item is an error message (if an error detected) or nil.


see http://www.rfc-editor.org/rfc/bcp/bcp47.txt section 2.1
See 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 function get_ietf_parts (source, args_script, args_region, args_variant)
local code, script, region, variant, private; -- ietf tag parts
local code, script, region, variant, private; -- IETF tag parts


if not is_set (source) then
if not is_set (source) then
Line 350: Line 358:
end
end


local pattern = { -- table of tables holding acceptibe ietf tag patterns and short names of the ietf part captured by the pattern
local pattern = { -- table of tables holding acceptable IETF tag patterns and short names of the IETF 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)%-(%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)
{'^(%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 380: Line 388:
}
}


local t = {}; -- table of captures; serves as a translator between captured ietf tag parts and named variables
local t = {}; -- table of captures; serves as a translator between captured IETF tag parts and named variables


for i, v in ipairs (pattern) do -- spin through the pattern table looking for a match
for i, v in ipairs (pattern) do -- spin through the pattern table looking for a match
Line 394: Line 402:
};
};
script = t.s or ''; -- translate table contents to named variables;
script = t.s or ''; -- translate table contents to named variables;
region = t.r or ''; -- absent table entries are nil so set named ietf parts to empty string for concatenation
region = t.r or ''; -- absent table entries are nil so set named IETF parts to empty string for concatenation
variant= t.v or '';
variant= t.v or '';
private = t.p or '';
private = t.p or '';
Line 428: Line 436:
script = script:lower(); -- ensure that we use and return lower case version of this
script = script:lower(); -- ensure that we use and return lower case version of this
if not script_table[script] then
if not script_table[script] then
return code, nil, nil, nil, nil, substitute (cfg.get_ietf_parts_t.unrecog_scr_code, {script, code}); -- language code ok, invalid script, don't know about the others (don't care?)
return code, nil, nil, nil, nil, substitute (cfg.get_ietf_parts_t.unrecog_scr_code, {script, code}); -- language code OK, invalid script, don't know about the others (don't care?)
end
end
end
end
if suppressed_table[script] then -- ensure that code-script does not use a suppressed script
if suppressed_table[script] then -- ensure that code-script does not use a suppressed script
if in_array (code, suppressed_table[script]) then
if in_array (code, suppressed_table[script]) then
return code, nil, nil, nil, nil, substitute (cfg.get_ietf_parts_t.script_code, {script, code}); -- language code ok, script is suppressed for this code
return code, nil, nil, nil, nil, substitute (cfg.get_ietf_parts_t.script_code, {script, code}); -- language code OK, script is suppressed for this code
end
end
end
end
Line 515: Line 523:
--[[--------------------------< D I V _ M A R K U P _ A D D >--------------------------------------------------
--[[--------------------------< D I V _ M A R K U P _ A D D >--------------------------------------------------


adds <i> and </i> tags to list-item text or to implied <p>..</p> text. mixed not supported
Adds <i> and </i> tags to list-item text or to implied <p>..</p> text. Mixed not supported.


]]
]]


local function div_markup_add (text, style)
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 text:find ('^\n[%*:;#]') then -- look for list markup; list markup must begin at start of text
if 'italic' == style then
if 'italic' == style then
Line 531: Line 537:


if text:find ('\n+') then -- look for any number of \n characters in text
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 mediawiki
text = text:gsub ('([^\n])\n([^\n])', '%1 %2'); -- replace single newline characters with a space character which mimics MediaWiki


if 'italic' == style then
if 'italic' == style then
text = text:gsub('[^\n]+', '<p><i>%1</i></p>'); -- insert p and italic markup tags at each impled p (two or more consecutive '\n\n' sequences)
text = text:gsub('[^\n]+', '<p><i>%1</i></p>'); -- insert p and italic markup tags at each implied p (two or more consecutive '\n\n' sequences)
else
else
text = text:gsub ('[^\n]+', '<p>%1</p>'); -- insert p markup at each impled p
text = text:gsub ('[^\n]+', '<p>%1</p>'); -- insert p markup at each implied p
text = text:gsub ('\n', ''); -- strip newline characters
text = text:gsub ('\n', ''); -- strip newline characters
end
end
Line 547: Line 553:
--[[--------------------------< T I T L E _ W R A P P E R _ M A K E >------------------------------------------
--[[--------------------------< T I T L E _ W R A P P E R _ M A K E >------------------------------------------


makes a <span title="<title text>"><content_text></span> or <div title="<title text>"><content_text></div> where
Makes 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
<title text> is in the tool-tip in the wiki's local language and <content_text> is non-local-language text in
html markup.  This because the lang= attibute applies to the content of its enclosing tag.
HTML markup.  This because the lang= attribute 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 570: Line 577:
--[[--------------------------< M A K E _ T E X T _ H T M L >--------------------------------------------------
--[[--------------------------< M A K E _ T E X T _ H T M L >--------------------------------------------------


Add the html markup to text according to the type of content that it is: <span> or <i> tags for inline content or
Add the HTML 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
<div> tags for block content


Line 593: Line 600:
end
end


if 'span' == tag then -- default html tag for inline content
if 'span' == tag then -- default HTML tag for inline content
if 'italic' == style then -- but if italic
if 'italic' == style then -- but if italic
tag = 'i'; -- change to <i> tags
tag = 'i'; -- change to <i> tags
Line 601: Line 608:
end
end


table.insert (html_t, table.concat ({'<', tag})); -- open the <i>, <span>, or <div> html tag
table.insert (html_t, table.concat ({'<', tag})); -- open the <i>, <span>, or <div> HTML tag
code = code:gsub ('%-x%-.*', ''); -- strip private use subtag from code tag because meaningless outside of wikipedia
code = code:gsub ('%-x%-.*', ''); -- strip private use subtag from code tag because meaningless outside of Wikipedia
table.insert (html_t, table.concat ({' lang="', code, '\"'})); -- add language attribute
table.insert (html_t, table.concat ({' lang="', code, '\"'})); -- add language attribute


Line 625: Line 632:
end
end


table.insert (html_t, table.concat ({style_added, '>'})); -- close the opening html tag
table.insert (html_t, table.concat ({style_added, '>'})); -- close the opening HTML tag
table.insert (html_t, text); -- insert the text
table.insert (html_t, text); -- insert the text


table.insert (html_t, table.concat ({'</', tag, '>'})); -- close the 'text' <i>, <span>, or <div> html tag
table.insert (html_t, table.concat ({'</', tag, '>'})); -- close the 'text' <i>, <span>, or <div> HTML tag


if is_set (language) then -- create a <title_text> string for the title= attribute in a wrapper span or div
if is_set (language) then -- create a <title_text> string for the title= attribute in a wrapper span or div
Line 652: Line 659:
[[Category:Articles containing <language>-language text]]
[[Category:Articles containing <language>-language text]]


for English:
For English:
[[Category:Articles containing explicitly cited English-language text]]
[[Category:Articles containing explicitly cited English-language text]]
for ISO 639-2 collective languages (and for 639-1 bh):
For ISO 639-2 collective languages (and for 639-1 bh):
[[Category:Articles with text in <language> languages]]
[[Category:Articles with text in <language> languages]]


Line 661: Line 668:


local function make_category (code, language_name, nocat, name_get)
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
if ((0 ~= namespace) or nocat) and not name_get then -- only categorize in article space
return ''; -- return empty string for concatenation
return ''; -- return empty string for concatenation
Line 691: Line 695:
--[[--------------------------< M A K E _ T R A N S L I T >----------------------------------------------------
--[[--------------------------< M A K E _ T R A N S L I T >----------------------------------------------------


return translit <i lang=xx-Latn>...</i> where xx is the language code; else return empty string
Return 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
The value |script= is not used in {{transliteration}} for this purpose; instead it uses |code.  Because language scripts
Line 698: Line 702:


To avoid confusion, in this module and the templates that use it, the transliteration script parameter is renamed
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)
to be |translit-script= (in this function, tscript).


This function is used by both lang_xx() and transl()
This function is used by both lang_xx() and xlit()
lang_xx() always provides code, language_name, and translit; may provide tscript; never provides style
lang_xx() always provides code, language_name, and translit; may provide tscript; never provides style
transl() always provides language_name, translit, and one of code or tscript, never both; always provides style
xlit() 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 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_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
local title_text = ''; -- tool tip text for title= attribute
Line 720: Line 723:
title_text = substitute ('$1-$2', {title_text, cfg.misc_text_t.language}); -- skip this text (individual and macro languages only)
title_text = substitute ('$1-$2', {title_text, cfg.misc_text_t.language}); -- skip this text (individual and macro languages only)
end
end
title_text = substitute ('$1 $2', {title_text, lang_data.engvar_t[engvar]['romanisz_lc']}); -- finish the tool tip; use romanization when neither script nor standard supplied
title_text = substitute ('$1 $2', {title_text, mw.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
elseif is_set (std) and is_set (tscript) then -- when both are specified
Line 746: Line 749:
if not title_t[std] then return ''; end -- invalid standard, setup for error message
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 (transl may not provide a language code)
if title_t[std][code] then -- if language code is in the table (xlit may not provide a language code)
title_text = substitute ('$1$2 ($3 $4) $5', { -- add the appropriate text to the tool tip
title_text = substitute ('$1$2 ($3 $4) $5', { -- add the appropriate text to the tool tip
title_text,
title_text,
title_t[std][code:lower()],
title_t[std][code:lower()],
Line 792: Line 795:
end
end


local close_tag;
local tag
if is_set (code) then -- when a language code is provided (always with {{lang-??}} templates, not always with {{transliteration}})
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
code = code:match ('^(%a%a%a?)'); -- strip all subtags leaving only the language subtag
if not style then -- nil for the default italic style
if not style then -- nil for the default italic style
table.insert (out_t, "<i lang=\""); -- so use <i> tag
tag = '<i lang="%s-Latn">%s</i>'
close_tag = '</i>'; -- tag to be used when closing
else
else
table.insert (out_t, table.concat ({'<span style=\"font-style: ', style, '\" lang=\"'})); -- non-standard style, construct a span tag for it
tag = '<span style="font-style: %s" lang="%s-Latn">%s</span>' -- non-standard style, construct a span tag for it
close_tag = '</span>'; -- tag to be used when closing
tag = string.format(tag, style, "%s", "%s")
end
end
table.insert (out_t, code);
tag = string.format(tag, code, "%s")
table.insert (out_t, "-Latn\">"); -- transliterations are always Latin script
else
else
table.insert (out_t, "<span>"); -- when no language code: no lang= attribute, not italic ({{transliteration}} only)
tag = '<span>%s</span>' -- when no language code: no lang= attribute, not italic ({{transliteration}} only)
close_tag = '</span>';
end
end
 
table.insert (out_t, translit); -- add the translit text
tag = string.format(tag, 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
if '' == title_text then -- when there is no need for a tool-tip
return table.concat (out_t); -- make a string and done
return tag; -- done
else
else
title_text = 'gb_t' == engvar and title_text:gsub ('([Rr]omani)z', '%1s') or title_text; -- gb eng when engvar specifies gb eng; us eng else
title_text = 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), 'span'); -- wrap with a tool-tip span and done
return title_wrapper_make (title_text, tag, 'span'); -- wrap with a tool-tip span and done
end
end
end
end
Line 825: Line 822:


This function checks the content of args.text and returns empty string if nothing is amiss else it returns an
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.
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
Italic rendering is controlled by the |italic= template parameter so italic markup should never appear in args.text
Line 834: Line 831:
local function validate_text (template, args)
local function validate_text (template, args)
if not is_set (args.text) then
if not is_set (args.text) then
return make_error_msg ('no text', args, template);
return make_error_msg (cfg.validate_text_t.no_text, args, template);
end
end


Line 853: Line 850:
--[[--------------------------< R E N D E R _ M A I N T >------------------------------------------------------
--[[--------------------------< R E N D E R _ M A I N T >------------------------------------------------------


render mainenance messages and categories
Render mainenance messages and categories.


]]
]]
Line 880: Line 877:
--[[--------------------------< P R O T O _ P R E F I X >------------------------------------------------------
--[[--------------------------< P R O T O _ P R E F I X >------------------------------------------------------


for proto languages, text is prefixed with a splat. We do that here as a flag for make_text_html() so that a splat
For 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
will be rendered outside of italic markup (if used). If the first character in text here is already a splat, we
do nothing
do nothing.


proto_param is boolean or nil; true adds splat prefix regardless of language name; false removes and / or inhibits
proto_param is boolean or nil; true adds splat prefix regardless of language name; false removes and inhibits
regardless of language name; nil does nothing; presumes that the value in text is correct but removes extra splac
regardless of language name; nil does nothing; presumes that the value in text is correct but removes extra splac.


]]
]]
Line 902: Line 899:
--[[--------------------------< H A S _ P O E M _ T A G >------------------------------------------------------
--[[--------------------------< H A S _ P O E M _ T A G >------------------------------------------------------


looks for a poem strip marker in text; returns true when found; false else
Looks for a poem strip marker in text; returns true when found; false else.


auto-italic detection disabled when text has poem stripmarker because it is not possible for this code to know
Auto-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.
the content that will replace the stripmarker.


Line 916: Line 913:
--[[--------------------------< H T M L _ T A G _ S E L E C T >------------------------------------------------
--[[--------------------------< 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 html tag for text.
Inspects content of and selectively trims text.  Returns text and the name of an appropriate HTML tag for text.


If text contains:
If text contains:
Line 959: Line 956:
--[[--------------------------< V A L I D A T E _ P R O T O >--------------------------------------------------
--[[--------------------------< V A L I D A T E _ P R O T O >--------------------------------------------------


validates value assigned to |proto=; permitted values are yes and no; yes returns as true, no returns as false,
Validates 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 nil with <proto_param> as second
empty string (or parameter omitted) returns as nil; any other value returns nil with <proto_param> as second
return value for use in error message.
return value for use in error message.
Line 980: Line 977:
--[[--------------------------< L A N G U A G E _ N A M E _ G E T >--------------------------------------------
--[[--------------------------< L A N G U A G E _ N A M E _ G E T >--------------------------------------------


common function to return language name from the data set according to IETF tag
Common function to return language name from the data set according to IETF tag.


returns language name if found in data tables; nil else
Returns language name if found in data tables; nil else.


]]
]]
Line 995: Line 992:
elseif override_table[code] then -- not there so try basic language tag
elseif override_table[code] then -- not there so try basic language tag
name = override_table[code];
name = override_table[code];
elseif lang_table[code] then -- shift to iana active tag/name table
elseif lang_table[code] then -- shift to IANA active tag/name table
name = lang_table[code];
name = lang_table[code];
elseif lang_dep_table[code] then -- try the iana deprecated tag/name table
elseif lang_dep_table[code] then -- try the IANA deprecated tag/name table
name = lang_dep_table[code];
name = lang_dep_table[code];
end
end
Line 1,018: Line 1,015:
Conversely, when <text> is written using the Latn script, the IETF script subtag, if present, should be Latn.
Conversely, when <text> is written using the Latn script, the IETF script subtag, if present, should be Latn.


returns an error message when mismatch detected; nil else
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)
 
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
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 is_latn_text then -- when text is wholly Latn script
if ('Latn' ~= script and 'latn' ~= script) then -- but a non-Latn script is specified
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
return cfg.text_script_match_test_t.latn_txt_mismatch; -- emit an error message
end
end
else -- when text is not wholly Latn script
else -- when text is not wholly Latn script
if ('Latn' == script or 'latn' == script) then -- but Latn script is specified
if scripts_t[script] then -- but a Latn script is specified
return cfg.text_script_match_test_t.latn_scr_mismatch; -- emit an error message
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
Line 1,036: Line 1,039:




--[[--------------------------< _ L A N G >--------------------------------------------------------------------
--[[--------------------------< B I N A R Y _ S E A R C H >---------------------------------------------------


entry point for {{lang}}
conducts a binary search of <ranges_t> for a sub-range that holds <target>.


there should be no reason to set parameters in the {{lang}} {{#invoke:}}
returns boolean true if a sub-range holding <target> is found; boolean false else.
<includeonly>{{#invoke:lang|lang}}</includeonly>
 
parameters are received from the template's frame (parent frame)


]]
]]


local function _lang (args)
local function binary_search (target, ranges_t)
local out = {};
local idx_bot = 1; -- initialize to index of first key
local language_name; -- used to make category names
local idx_top = sizeof_ranges_t; -- initialize to index of last key (number of keys)
local category_name; -- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set
local subtags = {}; -- IETF subtags script, region, variant, and private
local code; -- the language code
local msg; -- for error messages
local tag = 'span'; -- initial value for make_text_html()
local template = args.template or templates.lang;


maint_cats = {}; -- initialize because when this module required into another module, these only declared once so only initialzed once
if (target < ranges_t[idx_bot][1]) or (target > ranges_t[idx_top][2]) then -- invalid; target out of range
maint_msgs = {};
return; -- abandon
validate_cat_args (args); -- determine if categorization should be suppressed
 
if args[1] and args.code then
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {'1', 'code'}), args, template);
else
args.code = args[1] or args.code; -- prefer args.code
end
end


if args[2] and args.text then
local idx_mid; -- calculated index of range midway between top index and bottom index
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {'2', 'text'}), args, template);
local flag = false; -- flag to tell us when we've evaluated last (highest) range in <ranges_t>
else
args.text = args[2] or args.text; -- prefer args.text
end


msg = validate_text (template, args); -- ensure that |text= is set
while 1 do
if is_set (msg) then -- msg is an already-formatted error message
idx_mid = math.ceil ((idx_bot + idx_top) / 2); -- get the mid-point in the <ranges_t> sequence
return msg;
if (target >= ranges_t[idx_mid][1]) and (target <= ranges_t[idx_mid][2]) then -- indexed range low value <= target <= indexed range high value
end
return true; -- we found the range that holds the <target> character; return true
args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate html tag with text trimmed accordingly
elseif (target > ranges_t[idx_mid][2]) then -- is <target> > indexed range high value?
idx_bot = idx_mid; -- adjust <idx_bot> up


args.rtl = args.rtl == cfg.keywords_t.affirmative; -- convert to boolean: 'yes' -> true, other values -> false
else -- here when <target> less than indexed range low value
idx_top = idx_mid - 1; -- adjust <idx_top> down
end


args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag
if flag then
if msg then
break; -- here when we just evaluated the last range and <target> not found
return make_error_msg (substitute (cfg.lang_t.invalid_proto, {msg}), args, template);
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
end


code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code); -- |script=, |region=, |variant= not supported because they should be part of args.code ({{{1}}} in {{lang}})


if msg then
--[[--------------------------< I S _ L A T I N >--------------------------------------------------------------
return make_error_msg ( msg, args, template);
 
end
compare <text> as codepoints to lists of known codepoints accepted as Latn script
 
 
local is_latn_text = unicode.is_Latin (args.text); -- make a boolean
returns boolean true and modified <text> when <text> is wrapped in accept-as-written markup
 
 
msg = text_script_match_test (subtags.script, is_latn_text)
returns boolean true and <text> when codepoint is known
if msg then -- if an error detected then there is an error message
 
return make_error_msg (msg, args, template);
returns boolean false, <text>, non-Latn codepoint position in <text> (left to right), and the codepoint character
end
when codepoint is not known
 
 
args.italic, msg = validate_italic (args);
TODO: when text has accept-as-written markup, return a non-boolean value to indicate that <text> is not wholly
if msg then
latn script?  Use that return value to create non-Latn HTML lang= attribute because <text> isn't really
return make_error_msg (msg, args, template);
latn so lang=und (undetermined)? or instead, omit the -Latn subtag? (without -Latn need to force |italic=yes)
end
 
 
]]
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
local function is_latin (text, tag)
(this_wiki_lang_tag ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and 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)
local count;
args.italic = 'italic'; -- DEFAULT for {{lang}} templates is upright; but if latn script set for font-style:italic
text, count = text:gsub ('^%(%((.+)%)%)$', '%1'); -- remove accept-as-written markup if present
else
if 0 ~= count then
args.italic = 'inherit'; -- italic not set; script not latn; inherit current style
return true, text; -- markup present so assume that <text> is Latn-script
end
end
end
 
local pos = 0; -- position counter for error messaging
if is_set (subtags.script) then -- if script set, override rtl setting
for codepoint in mw.ustring.gcodepoint (text) do -- fetch each code point
if in_array (subtags.script, lang_data.rtl_scripts) then
pos = pos + 1; -- bump the position counter
args.rtl = true; -- script is an rtl script
 
else
if not is_latn_data.singles_t[codepoint] and -- codepoint not found in the singles list?
args.rtl = false; -- script is not an rtl script
not binary_search (codepoint, is_latn_data.ranges_t) and -- codepoint not a member of a listed range?
end
not (tag and is_latn_data.specials_t[codepoint] and is_latn_data.specials_t[codepoint][tag]) then -- not a language-specific codepoint?
end
return false, text, pos, mw.ustring.char (codepoint); -- codepoint not known; return false with codepoint position and character representation
 
end
args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles
end
language_name = language_name_get (args.code, code, true); -- get language name; try ietf tag first, then code w/o variant then code w/ variant
return true, text; -- is known; return <text>
 
end
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
--[[--------------------------< _ L A N G >--------------------------------------------------------------------
 
 
args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat
Entry point for {{lang}}.
 
There should be no reason to set parameters in the {{lang}} {{#invoke:}}
<includeonly>{{#invoke:lang|lang}}</includeonly>
 
Parameters are received from the template's frame (parent frame).
 
]]
 
local function _lang (args)
local out = {};
local language_name; -- used to make category names
local category_name; -- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set
local subtags = {}; -- IETF subtags script, region, variant, and private
local code; -- the language code
local msg; -- for error messages
local tag = 'span'; -- initial value for make_text_html()
local template = args.template or cfg.templates_t.lang;
 
maint_cats = {}; -- initialize because when this module required into another module, these only declared once so only initialzed once
maint_msgs = {};
validate_cat_args (args); -- determine if categorization should be suppressed
 
if args[1] and args.code then
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {'1', cfg.lang_t.conflict_n_param_types.code}), args, template);
else
args.code = args[1] or args.code; -- prefer args.code
end
 
if args[2] and args.text then
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {'2', cfg.lang_t.conflict_n_param_types.text}), args, template);
else
args.text = args[2] or args.text; -- prefer args.text
end
 
msg = validate_text (template, args); -- ensure that |text= is set
if is_set (msg) then -- msg is an already-formatted error message
return msg;
end
args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate HTML tag with text trimmed accordingly
 
args.rtl = args.rtl == 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 (substitute (cfg.lang_t.invalid_proto, {msg}), args, template);
end
 
code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code); -- |script=, |region=, |variant= not supported because they should be part of args.code ({{{1}}} in {{lang}})
 
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
 
args.italic, msg = validate_italic (args);
if msg then
return make_error_msg (msg, args, template);
end
 
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 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
args.italic = 'inherit'; -- italic not set; script not latn; inherit current style
end
end
if is_set (subtags.script) then -- if script set, override rtl setting
if in_array (subtags.script, lang_data.rtl_scripts) then
args.rtl = true; -- script is an rtl script
else
args.rtl = false; -- script is not an rtl script
end
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 IETF 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
 
args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat


table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, language_name));
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, language_name));
Line 1,142: Line 1,226:
--[[--------------------------< L A N G >----------------------------------------------------------------------
--[[--------------------------< L A N G >----------------------------------------------------------------------


entry point for {{lang}}
Entry point for {{lang}}.


there should be no reason to set parameters in the {{lang}} {{#invoke:}}
There should be no reason to set parameters in the {{lang}} {{#invoke:}}
<includeonly>{{#invoke:lang|lang}}</includeonly>
<includeonly>{{#invoke:lang|lang}}</includeonly>


parameters are received from the template's frame (parent frame)
Parameters are received from the template's frame (parent frame).


]]
]]


local function lang (frame)
local function lang (frame)
local args_t = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text
local args_t = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text
valueFunc = function (key, value)
valueFunc = function (key, value)
if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space
if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space
Line 1,168: Line 1,252:
args_t.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang|...}}
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, templates.lang); -- verify that all supplied parameters are supported by {{lang-??}}
local msg = parameter_validate (args_t, cfg.templates_t.lang); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
if msg then
return make_error_msg (msg, args_t, templates.lang); -- when template has unsupported params, abandon with error message
return make_error_msg (msg, args_t, cfg.templates_t.lang); -- when template has unsupported params, abandon with error message
end
end


Line 1,179: Line 1,263:
--[[--------------------------< T R A N S L A T I O N _ M A K E >----------------------------------------------
--[[--------------------------< T R A N S L A T I O N _ M A K E >----------------------------------------------


stand-alone function to create literal translation of main text
Stand-alone function to create literal translation of main text.


Also used by {{lang-x2}}
Also used by {{lang-x2}}.


]]
]]
Line 1,189: Line 1,273:


if 'none' ~= args_t.label then -- if we want a label
if 'none' ~= args_t.label then -- if we want a label
table.insert (translation_t, '<small>'); -- open the <small> html tag
table.insert (translation_t, '<small>'); -- open the <small> HTML tag
if cfg.keywords_t.negative == args_t.link then
if cfg.keywords_t.negative == args_t.link then
table.insert (translation_t, substitute ('<abbr title="$1">$2</abbr>', {cfg.translation_make_t.lit_xlation, cfg.translation_make_t.lit_abbr})); -- unlinked form
table.insert (translation_t, substitute ('<abbr title="$1">$2</abbr>', {cfg.translation_make_t.lit_xlation, cfg.translation_make_t.lit_abbr})); -- unlinked form
Line 1,195: Line 1,279:
table.insert (translation_t, make_wikilink (cfg.translation_make_t.lit_xlation, cfg.translation_make_t.lit_abbr)); -- linked form
table.insert (translation_t, make_wikilink (cfg.translation_make_t.lit_xlation, cfg.translation_make_t.lit_abbr)); -- linked form
end
end
table.insert (translation_t, "&thinsp;</small>"); -- close the <small> html tag
table.insert (translation_t, "&thinsp;</small>"); -- close the <small> HTML tag
end
end
table.insert (translation_t, table.concat ({'&#39;', args_t.translation, '&#39;'})); -- use html entities to avoid wiki markup confusion
table.insert (translation_t, table.concat ({'&#39;', args_t.translation, '&#39;'})); -- use HTML entities to avoid wiki markup confusion
return table.concat (translation_t); -- make a big string and done
return table.concat (translation_t); -- make a big string and done
end
end
Line 1,246: Line 1,330:
]]
]]


local function _lang_xx (args, base_template) -- base_template will be either of 'Langx' or 'Lang-xx'
local function _lang_xx (args, base_template) -- base_template will be either of 'langx' or 'lang-xx'
local out = {};
local out = {};
local language_name; -- used to make display text, article links
local language_name; -- used to make display text, article links
Line 1,262: Line 1,346:
maint_msgs = {};
maint_msgs = {};
local text_idx = (templates.langx == base_template) and 2 or 1; -- for {{langx}} 'text' positional parameter is '2'
local text_idx = (cfg.templates_t.langx == base_template) and 2 or 1; -- for {{langx}} 'text' positional parameter is '2'
local translit_idx = (templates.langx == base_template) and 3 or 2;
local translit_idx = (cfg.templates_t.langx == base_template) and 3 or 2;
local xlate_idx = (templates.langx == base_template) and 4 or 3;
local xlate_idx = (cfg.templates_t.langx == base_template) and 4 or 3;


if args[text_idx] and args.text then
if args[text_idx] and args.text then
return make_error_msg (substitute (cfg.lang_xx_t.conflict_n_param, {translit_idx, 'text'}), args, template);
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {translit_idx, cfg.lang_t.conflict_n_param_types.text}), args, template);
else
else
args.text = args[text_idx] or args.text; -- prefer positional 'text' parameter
args.text = args[text_idx] or args.text; -- prefer positional 'text' parameter
Line 1,277: Line 1,361:
end
end


args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate html tag with text trimmed accordingly
args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate HTML tag with text trimmed accordingly


if args[translit_idx] and args.translit then
if args[translit_idx] and args.translit then
return make_error_msg (substitute (cfg.lang_xx_t.conflict_n_param, {translit_idx, 'translit'}), args, template);
return make_error_msg (substitute (cfg.lang_t.conflict_n_param, {translit_idx, cfg.lang_t.conflict_n_param_types.translit}), args, template);


else
else
Line 1,286: Line 1,370:
end
end


args.engvar = lang_data.engvar_sel_t[args.engvar] or 'us_t'; -- either 'gb_t' or 'us_t' when |engvar= valid; 'us_t' else
args.engvar = cfg.engvar_sel_t[args.engvar] or cfg.default_engvar; -- |engvar= when valid; cfg.default_engvar else


if args[xlate_idx] and (args.translation or args.lit) then
if args[xlate_idx] and (args.translation or args.lit) then
return make_error_msg (substitute (cfg.lang_xx_t.conflict_n_lit, {translit_idx, '&#124;lit= or &#124;translation='}), args, template);
return make_error_msg (substitute (cfg.lang_xx_t.conflict_n_lit, {translit_idx}), args, template);
elseif args.translation and args.lit then
elseif args.translation and args.lit then
return make_error_msg (cfg.lang_xx_t.conflict_lit, args, template);
return make_error_msg (cfg.lang_xx_t.conflict_lit, args, template);
Line 1,304: Line 1,388:
validate_cat_args (args); -- determine if categorization should be suppressed
validate_cat_args (args); -- determine if categorization should be suppressed


args.rtl = args.rtl == cfg.keywords_t.affirmative; -- convert to boolean: 'yes' -> true, other values -> false
args.rtl = args.rtl == 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
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,312: Line 1,396:
end
end
local is_latn_text = unicode.is_Latin (args.text); -- make a boolean
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)
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
if msg then -- if an error detected then there is an error message
return make_error_msg (msg, args, template);
return make_error_msg (msg, args, template);
Line 1,324: Line 1,417:
end
end
if templates.langx == base_template then -- auto-italics  for {{langx}} templates; adapted from {{lang}} (no support for poem tag)  
if cfg.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 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
if ('latn' == subtags.script) or -- script is latn
Line 1,358: Line 1,451:
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag
if msg then
if msg then
return make_error_msg (table.concat ({'invalid &#124;proto=: ', args.proto}), args, template);
return make_error_msg (substitute (cfg.lang_t.invalid_proto, {args.proto}), args, template);
end
end


args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles
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 ietf tag first, then code w/o variant then code w/ variant
language_name = language_name_get (args.code, code, true); -- get language name; try IETF tag first, then code w/o variant then code w/ variant


category_name = language_name; -- category names retain IANA parenthetical diambiguators (if any)
category_name = language_name; -- category names retain IANA parenthetical diambiguators (if any)
Line 1,377: Line 1,470:
if mw.ustring.find (language_name, 'languages', 1, true) then
if mw.ustring.find (language_name, 'languages', 1, true) then
table.insert (out, make_wikilink (language_name)); -- collective language name uses simple wikilink
table.insert (out, make_wikilink (language_name)); -- collective language name uses simple wikilink
elseif lang_data.article_name[args.code:lower()] then -- is ietf tag in article name over ride?
elseif lang_data.article_name[args.code:lower()] then -- is IETF 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
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
elseif lang_data.article_name[code] then -- is language tag in article override
Line 1,409: Line 1,502:
if translit_title_obj.exists and (cfg.keywords_t.negative ~= args.link) then
if translit_title_obj.exists and (cfg.keywords_t.negative ~= args.link) then
table.insert (out, make_wikilink (substitute ('$1 $2', {cfg.lang_xx_t.romanization, 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
else
table.insert (out, substitute ('$1:', {lang_data.engvar_t[args.engvar]['romanisz_pt']})); -- else plain text per engvar
table.insert (out, substitute ('$1:', {cfg.engvar_t[args.engvar]['romanisz_pt']})); -- else plain text per engvar


end
end
Line 1,424: Line 1,517:
return make_error_msg (substitute ('$1: $2', {cfg.lang_xx_t.invalid_xlit_std, args['translit-std'] or '[missing]'}), args, template);
return make_error_msg (substitute ('$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
end
 
if is_set (args.translation) then -- translation (not supported in {{lang}})
if is_set (args.translation) then -- translation (not supported in {{lang}})
table.insert (out, translation_make (args));
table.insert (out, translation_make (args));
Line 1,439: Line 1,535:
--[[--------------------------< L A N G _ X X _ A R G S _ G E T >----------------------------------------------
--[[--------------------------< L A N G _ X X _ A R G S _ G E T >----------------------------------------------


common function to get args table from {{lang-??}} templates
Common function to get args table from {{lang-??}} templates.


returns table of args
Returns table of args.


text positional parameters are not trimmed here but are selectively trimmed at html_tag_select()
Text positional parameters are not trimmed here but are selectively trimmed at html_tag_select().


]]
]]
Line 1,452: Line 1,548:
parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}}
parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}}
valueFunc = function (key, value)
valueFunc = function (key, value)
if ((templates.langx == base_template) and 2 or 1) == key then -- the 'text' positional parameter; 1 for {{lang-??}}, 2 for {{langx}}; do not trim wite space
if ((cfg.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
return value; -- return untrimmed 'text' positional parameter
elseif value then -- all other values: if the value is not nil
elseif value then -- all other values: if the value is not nil
Line 1,470: Line 1,566:
--[[--------------------------< L A N G _ X X _ I T A L I C >--------------------------------------------------
--[[--------------------------< 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.
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 function lang_xx_italic (frame)
local args = lang_xx_args_get (frame, templates.langxx);
local args = lang_xx_args_get (frame, cfg.templates_t.langxx);
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_italic|...}}
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); -- verify that all supplied parameters are supported by {{lang-??}}
local msg = parameter_validate (args, cfg.templates_t.langxx); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
if msg then
return make_error_msg (msg, args, templates.langxx); -- when template has unsupported params, abandon with error message
return make_error_msg (msg, args, cfg.templates_t.langxx); -- when template has unsupported params, abandon with error message
end
end


initial_style_state = 'italic';
initial_style_state = 'italic';
return _lang_xx (args, templates.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
return _lang_xx (args, cfg.templates_t.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
end
end


Line 1,490: Line 1,586:
--[[--------------------------< _ L A N G _ X X _ I T A L I C >------------------------------------------------
--[[--------------------------< _ L A N G _ X X _ I T A L I C >------------------------------------------------


Entry point from another module. Sets the initial style state to italic.
Entry point from another module. Sets the initial style state to italic.


]]
]]
Line 1,496: Line 1,592:
local function _lang_xx_italic (args)
local function _lang_xx_italic (args)
initial_style_state = 'italic';
initial_style_state = 'italic';
return _lang_xx (args, templates.langxx);
return _lang_xx (args, cfg.templates_t.langxx);
end
end


Line 1,502: Line 1,598:
--[[--------------------------< L A N G _ X X _ I N H E R I T >------------------------------------------------
--[[--------------------------< 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.
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 function lang_xx_inherit (frame)
local args = lang_xx_args_get (frame, templates.langxx);
local args = lang_xx_args_get (frame, cfg.templates_t.langxx);
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_inherit|...}}
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, templates.langxx); -- verify that all supplied parameters are supported by {{lang-??}}
local msg = parameter_validate (args, cfg.templates_t.langxx); -- verify that all supplied parameters are supported by {{lang-??}}
if msg then
if msg then
return make_error_msg (msg, args, templates.langxx); -- when template has unsupported params, abandon with error message
return make_error_msg (msg, args, cfg.templates_t.langxx); -- when template has unsupported params, abandon with error message
end
end


initial_style_state = 'inherit';
initial_style_state = 'inherit';
return _lang_xx (args, templates.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
return _lang_xx (args, cfg.templates_t.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category
end
end


Line 1,522: Line 1,618:
--[[--------------------------< _ L A N G _ X X _ I N H E R I T >----------------------------------------------
--[[--------------------------< _ 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.
Entry point from another module. Sets the initial style state to inherit.


]]
]]
Line 1,528: Line 1,624:
local function _lang_xx_inherit (args)
local function _lang_xx_inherit (args)
initial_style_state = 'inherit';
initial_style_state = 'inherit';
return _lang_xx (args, templates.langxx);
return _lang_xx (args, cfg.templates_t.langxx);
end
end


Line 1,544: Line 1,640:
local size_t = langx_data.size_t; -- get list of language tags for {{lang-??}} templates that set |size=<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, templates.langx);
local msg = parameter_validate (args_t, cfg.templates_t.langx);
if msg then
if msg then
return make_error_msg (msg, args_t, templates.langx);
return make_error_msg (msg, args_t, cfg.templates_t.langx);
end
end


args_t.code = args_t[1] or args_t.code; -- get the language tag; must be {{{1}}} or |code=
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
if not args_t.code then
return make_error_msg (cfg.get_ietf_parts_t.missing_lang_tag, args_t, templates.langx);
return make_error_msg (cfg.get_ietf_parts_t.missing_lang_tag, args_t, cfg.templates_t.langx);
end
end
Line 1,561: Line 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
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, templates.langx);
return _lang_xx (args_t, cfg.templates_t.langx);
end
end


Line 1,569: Line 1,665:
Entry point for {{langx}}.
Entry point for {{langx}}.


this function calls _lang_xx() to render non-English text. The {{lang-??}} templates have three positional paramters
this function calls _lang_xx() to render non-English text. The {{lang-??}} templates have three positional paramters
but {{langx}} has four:
but {{langx}} has four:


|  1 |  2 |  3 |  4
|  1 |  2 |  3 |  4
{{lang-xx |<text> |<transl> |<xlate> }}
{{lang-xx |<text> |<xlit> |<xlat> }}
{{langx |<tag> |<text> |<transl> |<xlate> }}
{{langx |<tag> |<text> |<xlit> |<xlat> }}


The calls to lang_xx_args_get() and _lang_xx() use 'Langx' as a flag for those functions to select the proper
The calls to lang_xx_args_get() and _lang_xx() use 'langx' as a flag for those functions to select the proper
positional parameters.
positional parameters.


Line 1,582: Line 1,678:


{{langx}} can't do that.  The initial version of {{langx}} relied on a list of language tags (inherit_t in ~/langx)
{{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. Langx now
scraped from those {{lang-??}} templates that call lang_xx_inherit() to render text in upright font. langx() now
uses auto-italics code adapted from {{lang}} (doesn't support poem tags)
uses auto-italics code adapted from {{lang}} (doesn't support poem tags).


]]
]]


local function langx (frame)
local function langx (frame)
local args_t = lang_xx_args_get (frame, templates.langx); -- get the arguments; 'Langx' is the <base_template> used to decide which positional param is 'text', 'translit', 'lit'
local args_t = lang_xx_args_get (frame, cfg.templates_t.langx); -- get the arguments; 'langx' is the <base_template> used to decide which positional param is 'text', 'translit', 'lit'
return _langx (args_t);
return _langx (args_t);
Line 1,600: Line 1,696:
All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function.
All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function.


Module entry point from another module
Module entry point from another module.


]]
]]
Line 1,614: Line 1,710:
--[[--------------------------< I S _ I E T F _ T A G >--------------------------------------------------------
--[[--------------------------< I S _ I E T F _ T A G >--------------------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]


local function is_ietf_tag (frame)
local function is_ietf_tag (frame)
return _is_ietf_tag (getArgs (frame)[1]); -- args[1] is the ietf language tag to be tested; also get parent frame
return _is_ietf_tag (getArgs (frame)[1]); -- args[1] is the IETF language tag to be tested; also get parent frame
end
end


Line 1,632: Line 1,728:


local function is_ietf_tag_frame (frame)
local function is_ietf_tag_frame (frame)
return _is_ietf_tag (getArgs (frame, {frameOnly = true,})[1]); -- args[1] is the ietf language tag to be tested; do not get parent frame
return _is_ietf_tag (getArgs (frame, {frameOnly = true,})[1]); -- args[1] is the IETF language tag to be tested; do not get parent frame
end
end


Line 1,644: Line 1,740:
Set invoke's |link= parameter to yes to get wikilinked version of the language name.
Set invoke's |link= parameter to yes to get wikilinked version of the language name.


Module entry point from another module
Module entry point from another module.


]]
]]
Line 1,651: Line 1,747:
local subtags = {}; -- IETF subtags script, region, variant, and private
local subtags = {}; -- IETF subtags script, region, variant, and private
local raw_code = args[1]; -- save a copy of the input IETF subtag
local raw_code = args[1]; -- save a copy of the input IETF subtag
local link = cfg.keywords_t.affirmative == args['link']; -- make a boolean
local link = cfg.keywords_t.affirmative == args['link']; -- make a boolean
local label = args.label;
local label = args.label;
local code; -- the language code
local code; -- the language code
Line 1,660: Line 1,756:
if msg then
if msg then
local template = (args['template'] and table.concat ({'{{', args['template'], '}}: '})) or ''; -- make template name (if provided by the template)
local template = (args['template'] and table.concat ({'{{', args['template'], '}}: '})) or ''; -- make template name (if provided by the template)
return table.concat ({'<span style=\"color:#d33\">Error: ', template, msg, '</span>'});
return make_error_span (template, msg);
end
end


raw_code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles; private omitted because private
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 ietf tag first, then code w/o variant then code w/ variant
language_name = language_name_get (raw_code, code); -- get language name; try IETF tag first, then code w/o variant then code w/ variant


language_name = language_name:gsub ('%s+%b()', ''); -- remove IANA parenthetical disambiguators or qualifiers from names that have them
if 'yes' ~= args.raw then
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 link then -- when |link=yes, wikilink the language name
if mw.ustring.find (language_name, 'languages', 1, true) then
if mw.ustring.find (language_name, 'languages', 1, true) then
language_name = make_wikilink (language_name, label); -- collective language name uses simple wikilink
language_name = make_wikilink (language_name, label); -- collective language name uses simple wikilink
elseif lang_data.article_name[raw_code:lower()] then -- is ietf tag in article name override?
elseif lang_data.article_name[raw_code:lower()] then -- is IETF 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
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?
elseif lang_data.article_name[code] then -- is language tag in article name override?
Line 1,686: Line 1,784:
--[[--------------------------< N A M E _ F R O M _ T A G >----------------------------------------------------
--[[--------------------------< N A M E _ F R O M _ T A G >----------------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]


local function name_from_tag (frame) -- entry point from an {{#invoke:Lang|name_from_tag|<ietf tag>|link=<yes>|template=<template name>}}
local function name_from_tag (frame) -- entry point from an {{#invoke:lang|name_from_tag|<IETF 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
return _name_from_tag (getArgs(frame)) -- pass-on the args table, nothing else; getArgs() so we also get parent frame
end
end
Line 1,697: Line 1,795:
--[[--------------------------< _ T A G _ F R O M _ N A M E >--------------------------------------------------
--[[--------------------------< _ T A G _ F R O M _ N A M E >--------------------------------------------------


Returns the ietf language tag associated with the language name.  Spelling of language name must be correct
Returns the IETF 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,
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).
that disambiguator must be omitted (they are not present in the data name-to-tag tables).


Module entry point from another module
Module entry point from another module.


]]
]]
Line 1,726: Line 1,824:
template = table.concat ({'{{', args['template'], '}}: '}); -- make template name (if provided by the template)
template = table.concat ({'{{', args['template'], '}}: '}); -- make template name (if provided by the template)
end
end
return substitute ('<span style=\"color:#d33\">$1: $2$3</span>', {cfg.misc_text_t.error, template, msg});
return make_error_span (template, msg);


end
end
Line 1,733: Line 1,831:
--[[--------------------------< T A G _ F R O M _ N A M E >----------------------------------------------------
--[[--------------------------< T A G _ F R O M _ N A M E >----------------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]


local function tag_from_name (frame) -- entry point from an {{#invoke:Lang|tag_from_name|<language name>|link=<yes>|template=<template name>}}
local function tag_from_name (frame) -- entry point from an {{#invoke:lang|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()
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;
return result;
Line 1,745: Line 1,843:
--[[--------------------------< I S _ L A N G _ N A M E >------------------------------------------------------
--[[--------------------------< I S _ L A N G _ N A M E >------------------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]
Line 1,755: Line 1,853:




--[[--------------------------< _ T R A N S L >----------------------------------------------------------------
--[[--------------------------< _ X L I T >--------------------------------------------------------------------


Module entry point from another module
Module entry point from another module.


]]
]]


local function _transl (args)
local function _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 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
local language_name; -- language name that matches language code; used for tool tip
Line 1,767: Line 1,865:
local script; -- IANA script
local script; -- IANA script
local msg; -- for when called functions return an error message
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
maint_cats = {}; -- initialize because when this module required into another module, these only declared once so only initialzed once
Line 1,776: Line 1,875:


if not title_table[args.translit_std] then
if not title_table[args.translit_std] then
return make_error_msg (substitute (cfg.transl_t.unrecog_xlit_std, {args.translit_std}), args, 'Transliteration');
return make_error_msg (substitute (cfg.transl_t.unrecog_xlit_std, {args.translit_std}), args, template);
end
end
else
else
Line 1,784: Line 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?
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
args[1]:match ('^%a%a%a?%-x%-')) then -- or is args[1] a private-use tag
return make_error_msg (cfg.transl_t.no_text, args, 'Transliteration'); -- args[1] is a code so we're missing text
return make_error_msg (cfg.transl_t.no_text, args, template); -- args[1] is a code so we're missing text
else
else
args.text = args[1]; -- args[1] is not a code so we're missing that; assign args.text for error message
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 (cfg.transl_t.missing_lang_scr, args, 'Transliteration');
return make_error_msg (cfg.transl_t.missing_lang_scr, args, template);
end
end
end
end
end
end


if is_set (args[1]) then -- IANA language code used for html lang= attribute; or ISO 15924 script code
if is_set (args[1]) then -- IANA language code used for HTML 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?
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
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
else
return make_error_msg (substitute (cfg.transl_t.unrecog_lang_scr, {args[1]}), args, 'Transliteration'); -- invalid language / script code
return make_error_msg (substitute (cfg.transl_t.unrecog_lang_scr, {args[1]}), args, template); -- invalid language / script code
end
end
else
else
return make_error_msg (cfg.transl_t.missing_lang_scr, args, 'Transliteration'); -- missing language / script code so quit
return make_error_msg (cfg.transl_t.missing_lang_scr, args, 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
end


args.italic, msg = validate_italic (args);
args.italic, msg = validate_italic (args);
if msg then
if msg then
return make_error_msg (msg, args, 'Transliteration');
return make_error_msg (msg, args, template);
end
end
Line 1,811: Line 1,917:
end
end


args.engvar = lang_data.engvar_sel_t[args.engvar] or 'us_t'; -- either 'gb_t' or 'us_t' when |engvar= valid; 'us_t' else
args.engvar = cfg.engvar_sel_t[args.engvar] or cfg.default_engvar; -- |engvar= when valid; cfg.default_engvar else


if override_table[args.code] then -- is code a language code defined in the override table?
if override_table[args.code] then -- is code a language code defined in the override table?
Line 1,825: Line 1,931:
args.code = ''; -- unset because not a language code
args.code = ''; -- unset because not a language code
else
else
return make_error_msg (substitute (cfg.transl_t.unrecog_lang_scr, {args.code}), args, 'Transliteration'); -- invalid language / script code
return make_error_msg (substitute (cfg.transl_t.unrecog_lang_scr, {args.code}), args, template); -- invalid language / script code
end
end
-- here only when all parameters passed to make_translit() are valid
-- here only when all parameters passed to make_translit() are valid
Line 1,832: Line 1,938:




--[[--------------------------< T R A N S L >------------------------------------------------------------------
--[[--------------------------< X L I T >----------------------------------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]


local function transl (frame)
local function xlit (frame)
return _transl (getArgs(frame));
return _xlit (getArgs(frame));
end
end


Line 1,845: Line 1,951:
--[[--------------------------< C A T E G O R Y _ F R O M _ T A G >--------------------------------------------
--[[--------------------------< 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
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.
All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function.


Module entry point from another module
Module entry point from another module.


]]
]]
Line 1,865: Line 1,971:
if msg then
if msg then
local template = (args_t.template and table.concat ({'{{', args_t.template, '}}: '})) or ''; -- make template name (if provided by the template)
local template = (args_t.template and table.concat ({'{{', args_t.template, '}}: '})) or ''; -- make template name (if provided by the template)
return table.concat ({'<span style="color:#d33">Error: ', template, msg, '</span>'});
return make_error_span (template, msg);
end
end


raw_code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles; private omitted because private
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 ietf tag first, then code w/o variant then code w/ variant
category_name = language_name_get (raw_code, code); -- get language name; try IETF tag first, then code w/o variant then code w/ variant
category_name = make_category (code, category_name, nil, true):gsub ('[%[%]]', '');
category_name = make_category (code, category_name, nil, true):gsub ('[%[%]]', '');


Line 1,882: Line 1,988:
--[[--------------------------< C A T E G O R Y _ F R O M _ T A G >--------------------------------------------
--[[--------------------------< C A T E G O R Y _ F R O M _ T A G >--------------------------------------------


Module entry point from an {{#invoke:}}
Module entry point from an {{#invoke:}}.


]]
]]


local function category_from_tag (frame) -- entry point from an {{#invoke:Lang|category_from_tag|<ietf tag>|template=<template name>}}
local function category_from_tag (frame) -- entry point from an {{#invoke:lang|category_from_tag|<IETF tag>|template=<template name>}}
return _category_from_tag (getArgs (frame)); -- pass-on the args table, nothing else; getArgs() so we also get parent frame
return _category_from_tag (getArgs (frame)); -- pass-on the args table, nothing else; getArgs() so we also get parent frame
end
end
Line 1,895: Line 2,001:


return {
return {
category_from_tag = category_from_tag,
category_from_tag = category_from_tag, -- frame entry points when this module is #invoke:ed into templates/wikitext
lang = lang, -- entry point for {{lang}}
lang = lang, -- entry point for {{lang}}
langx = langx, -- entry point for {{langx}}
langx = langx, -- entry point for {{langx}}
Line 1,903: Line 2,009:
is_ietf_tag_frame = is_ietf_tag_frame,
is_ietf_tag_frame = is_ietf_tag_frame,
is_lang_name = is_lang_name,
is_lang_name = is_lang_name,
tag_from_name = tag_from_name, -- returns ietf tag associated with language name
tag_from_name = tag_from_name, -- returns IETF tag associated with language name
name_from_tag = name_from_tag, -- used for template documentation; possible use in ISO 639 name from code templates
name_from_tag = name_from_tag, -- used for template documentation; possible use in ISO 639 name from code templates
transl = transl, -- entry point for {{transliteration}}
xlit = xlit, -- entry point for {{transliteration}}
_category_from_tag = _category_from_tag, -- entry points when this module is require()d into other modules
_category_from_tag = _category_from_tag, -- API entry points when this module is require()d into other modules
_lang = _lang,
_lang = _lang,
_langx = _langx,
_langx = _langx,
Line 1,916: Line 2,022:
_tag_from_name = _tag_from_name,  
_tag_from_name = _tag_from_name,  
_name_from_tag = _name_from_tag,
_name_from_tag = _name_from_tag,
_transl = _transl,
_xlit = _xlit,
_translation_make = translation_make,
_translation_make = translation_make,
};
};