Module:Lang: Difference between revisions
Jump to navigation
Jump to search
no edit summary
(Created page with "--[=[ Lua support for the {{lang}}, {{lang-xx}}, and {{transliteration}} templates and replacement of various supporting templates. ]=] require('strict'); local getArgs = require ('Module:Arguments').getArgs; local unicode = require ("Module:Unicode data"); -- for is_latin() and is_rtl() local yesno = require ('Module:Yesno'); local lang_data = mw.loadData ('Module:Lang/data'); -- language name override and transliteration tool-tip tables local lang_n...") |
>Trappist the monk No edit summary |
||
Line 1: | Line 1: | ||
--[=[ | --[=[ | ||
Lua support for the {{lang}}, {{lang- | Lua support for the {{lang}}, {{langx}}, {{lang-??}}, and {{transliteration}} templates and replacement of various supporting templates. | ||
]=] | ]=] | ||
Line 104: | Line 104: | ||
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 transl function to set args.italic according to the template's | ||
defined default ('inherit' for {{lang}}, 'inherit' or 'italic' for {{lang- | 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}} | ||
and {{lang- | and {{lang-??}} only). | ||
Accepted values and the values that this function returns are are: | Accepted values and the values that this function returns are are: | ||
Line 138: | Line 138: | ||
--[=[--------------------------< 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- | Default behavior of the {{lang}} and {{lang-??}} templates is to add categorization when the templates are used in mainspace. | ||
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 245: | Line 245: | ||
the synonymous three-character codes in the lang= attribute. | the synonymous three-character codes in the lang= attribute. | ||
For {{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=) | ||
Line 714: | Line 714: | ||
local close_tag; | local close_tag; | ||
if is_set (code) then -- when a language code is provided (always with {{lang- | 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 | ||
Line 1,083: | Line 1,083: | ||
--[[--------------------------< L A N G _ X X > | --[[--------------------------< _ L A N G _ X X >-------------------------------------------------------------- | ||
For the {{lang- | For the {{lang-??}} templates, the only parameter required to be set in the template is the language code. All | ||
other parameters can, usually should, be written in the template call. For {{lang- | other parameters can, usually should, be written in the template call. For {{lang-??}} templates for languages | ||
that can have multiple writing systems, it may be appropriate to set |script= as well. | that can have multiple writing systems, it may be appropriate to set |script= as well. | ||
For each {{lang- | For each {{lang-??}} template choose the appropriate entry-point function so that this function knows the default | ||
styling that should be applied to text. | styling that should be applied to text. | ||
Line 1,114: | Line 1,114: | ||
any other text replaces language-name label - automatic wikilinking disabled | any other text replaces language-name label - automatic wikilinking disabled | ||
for those {{lang- | for those {{lang-??}} templates that support transliteration (those templates where |text= is not entirely latn script): | ||
|translit = text that is a transliteration of text | |translit = text that is a transliteration of text | ||
|translit-std = the standard that applies to the transliteration | |translit-std = the standard that applies to the transliteration | ||
|translit-script = ISO 15924 script name; falls back to code | |translit-script = ISO 15924 script name; falls back to code | ||
For {{lang- | For {{lang-??}}, the positional parameters are: | ||
{{{1}}} text | {{{1}}} text | ||
{{{2}}} transliterated text | {{{2}}} transliterated text | ||
Line 1,127: | Line 1,127: | ||
]] | ]] | ||
local function _lang_xx (args) | 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,139: | Line 1,139: | ||
local msg; -- for error messages | local msg; -- for error messages | ||
local tag = 'span'; -- initial value for make_text_html() | local tag = 'span'; -- initial value for make_text_html() | ||
local template = args.template or | local template = args.template or base_template; | ||
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 | ||
maint_msgs = {}; | maint_msgs = {}; | ||
if args[ | local text_idx = ('Langx' == base_template) and 2 or 1; -- for {{langx}} 'text' positional parameter is '2' | ||
return make_error_msg ('conflicting: {{{ | local translit_idx = ('Langx' == base_template) and 3 or 2; | ||
local xlate_idx = ('Langx' == base_template) and 4 or 3; | |||
if args[text_idx] and args.text then | |||
return make_error_msg ('conflicting: {{{' .. text_idx .. '}}} and |text=', args, template); | |||
else | else | ||
args.text = args[ | args.text = args[text_idx] or args.text; -- prefer positional 'text' parameter | ||
end | end | ||
msg = validate_text (template, args); -- ensure that |text= is set, does not contain italic markup and is protected from improper bolding | msg = validate_text (template, args); -- ensure that |text= is set, does not contain italic markup and is protected from improper bolding | ||
if is_set (msg) then | if is_set (msg) then | ||
Line 1,157: | Line 1,161: | ||
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[ | if args[translit_idx] and args.translit then | ||
return make_error_msg ('conflicting: {{{ | return make_error_msg ('conflicting: {{{'.. translit_idx .. '}}} and |translit=', args, template); | ||
else | else | ||
args.translit = args[ | args.translit = args[translit_idx] or args.translit -- prefer positional 'translit' parameter | ||
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 = lang_data.engvar_sel_t[args.engvar] or 'us_t'; -- either 'gb_t' or 'us_t' when |engvar= valid; 'us_t' else | ||
if args[ | if args[xlate_idx] and (args.translation or args.lit) then | ||
return make_error_msg ('conflicting: {{{ | return make_error_msg ('conflicting: {{{'.. xlate_idx ..'}}} and |lit= or |translation=', args, template); | ||
elseif args.translation and args.lit then | elseif args.translation and args.lit then | ||
return make_error_msg ('conflicting: |lit= and |translation=', args, template); | return make_error_msg ('conflicting: |lit= and |translation=', args, template); | ||
else | else | ||
args.translation = args[ | args.translation = args[xlate_idx] or args.translation or args.lit; -- prefer positional 'translation' parameter | ||
end | end | ||
Line 1,188: | Line 1,192: | ||
return make_error_msg (msg, args, template); | return make_error_msg (msg, args, template); | ||
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, template); | return make_error_msg (msg, args, template); | ||
end | end | ||
-- TODO: this appears to work so we might in future dispense inherit_t in Module:Lang/langx | |||
-- retain existing system until {{langx}} has replaced {{lang-??}} | |||
-- 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)) 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 | |||
-- args.italic = 'inherit'; -- italic not set; script not latn; inherit current style | |||
-- end | |||
-- end | |||
-- TODO: retain this system until {{langx}} has replaced {{lang-??}} | |||
if nil == args.italic then -- args.italic controls | if nil == args.italic then -- args.italic controls | ||
if is_set (subtags.script) then | if is_set (subtags.script) then | ||
Line 1,299: | Line 1,314: | ||
returns table of args | returns table of args | ||
text positional parameters are not trimmed here but are selectively trimmed at html_tag_select() | |||
]] | ]] | ||
local function lang_xx_args_get (frame) | local function lang_xx_args_get (frame, base_template) | ||
local | local args_t = getArgs(frame, | ||
{ | { | ||
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 1 == key then | if (('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' | 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 | ||
value = mw.text.trim (value); -- trim whitespace | value = mw.text.trim (value); -- trim whitespace | ||
Line 1,319: | Line 1,336: | ||
}); | }); | ||
return | return args_t; | ||
end | end | ||
Line 1,325: | Line 1,342: | ||
--[[--------------------------< 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- | 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); | local args = lang_xx_args_get (frame, 'lang-xx'); | ||
initial_style_state = 'italic'; | initial_style_state = 'italic'; | ||
return _lang_xx (args); | return _lang_xx (args, 'Lang-xx'); | ||
end | end | ||
Line 1,339: | Line 1,356: | ||
--[[--------------------------< _ 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 | Entry point from another module. Sets the initial style state to italic. | ||
]] | ]] | ||
Line 1,345: | Line 1,362: | ||
local function _lang_xx_italic (args) | local function _lang_xx_italic (args) | ||
initial_style_state = 'italic'; | initial_style_state = 'italic'; | ||
return _lang_xx (args); | return _lang_xx (args, 'Lang-xx'); | ||
end | end | ||
Line 1,351: | Line 1,368: | ||
--[[--------------------------< 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- | 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); | local args = lang_xx_args_get (frame, 'lang-xx'); | ||
initial_style_state = 'inherit'; | initial_style_state = 'inherit'; | ||
return _lang_xx (args); | return _lang_xx (args, 'Lang-xx'); | ||
end | end | ||
Line 1,371: | Line 1,388: | ||
local function _lang_xx_inherit (args) | local function _lang_xx_inherit (args) | ||
initial_style_state = 'inherit'; | initial_style_state = 'inherit'; | ||
return _lang_xx (args); | return _lang_xx (args, 'Lang-xx'); | ||
end | |||
--[[--------------------------< _ L A N G X >------------------------------------------------------------------ | |||
Entry point from another module. | |||
]] | |||
local function _langx (args_t) | |||
local langx_data = mw.loadData ('Module:Lang/langx'); -- get necessary data | |||
local inherit_t = langx_data.inherit_t; -- get list of language tags extracted from {{lang-??}} template names for languages that are rendered in upright font | |||
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> | |||
args_t.code = args_t[1] or args_t.code; -- get the language tag; must be {{{1}}} or |code= | |||
args_t.rtl = args_t.rtl or (rtl_t[args_t.code] and 'yes'); -- 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 | |||
args_t[1] = nil; -- unset to mimic {{lang-??}} templates which set |code=xx | |||
initial_style_state = inherit_t[args_t.code:lower()] and 'inherit' or 'italic'; -- if listed in inherit_t, set as 'inherit'; 'italic' else | |||
return _lang_xx (args_t, 'Langx'); | |||
end | |||
--[[--------------------------< L A N G X >-------------------------------------------------------------------- | |||
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> |<transl> |<xlate> }} | |||
{{langx |<tag> |<text> |<transl> |<xlate> }} | |||
The calls to lang_xx_args_get() and _lang_xx() use 'Langx' as a flag for those functions to select the proper | |||
positional parameters. | |||
{{lang-??}} depends on the calling template to select 'inherit' or 'italic' as the default renderer. | |||
{{langx}} can't do that so, intead, relies on the list of tags scraped from the {{lang-??}} templates that call | |||
lang_xx_inherit(). | |||
]] | |||
local function langx (frame) | |||
local args_t = lang_xx_args_get (frame, 'Langx'); -- get the arguments; 'Langx' is the <base_template> used to decide which positional param is 'text', 'translit', 'lit' | |||
return _langx (args_t); | |||
end | end | ||
Line 1,379: | Line 1,451: | ||
Returns true when a language name associated with IETF language tag exists; nil else. IETF language tag must be valid. | Returns true when a language name associated with IETF language tag exists; nil else. IETF language tag must be valid. | ||
All code combinations supported by {{lang}} and the {{lang- | 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,421: | Line 1,493: | ||
Returns language name associated with IETF language tag if valid; error message else. | Returns language name associated with IETF language tag if valid; error message else. | ||
All code combinations supported by {{lang}} and the {{lang- | All code combinations supported by {{lang}} and the {{lang-??}} templates are supported by this function. | ||
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. | ||
Line 1,627: | Line 1,699: | ||
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- | 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,677: | Line 1,749: | ||
category_from_tag = category_from_tag, | category_from_tag = category_from_tag, | ||
lang = lang, -- entry point for {{lang}} | lang = lang, -- entry point for {{lang}} | ||
langx = langx, -- entry point for {{langx}} | |||
lang_xx_inherit = lang_xx_inherit, -- entry points for {{lang-??}} | lang_xx_inherit = lang_xx_inherit, -- entry points for {{lang-??}} | ||
lang_xx_italic = lang_xx_italic, | lang_xx_italic = lang_xx_italic, | ||
Line 1,688: | Line 1,761: | ||
_category_from_tag = _category_from_tag, -- entry points when this module is require()d into other modules | _category_from_tag = _category_from_tag, -- entry points when this module is require()d into other modules | ||
_lang = _lang, | _lang = _lang, | ||
_langx = _langx, | |||
_lang_xx_inherit = _lang_xx_inherit, | _lang_xx_inherit = _lang_xx_inherit, | ||
_lang_xx_italic = _lang_xx_italic, | _lang_xx_italic = _lang_xx_italic, |