Module:Lang: Difference between revisions
no edit summary
Nazarzadeh (talk | contribs) m (1 revision imported) |
No edit summary |
||
Line 33: | Line 33: | ||
local maint_cats = {}; -- maintenance categories go here | local maint_cats = {}; -- maintenance categories go here | ||
local maint_msgs = {}; -- and their messages go here | local maint_msgs = {}; -- and their messages go here | ||
local templates = { | |||
lang = 'Lang', | |||
langx = 'Langx', | |||
langxx = 'Lang-xx', | |||
} | |||
--[[--------------------------< C O M M O N _ P A R A M S >---------------------------------------------------- | |||
]] | |||
local known_params_t = { | |||
['common_params_all_t'] = { -- these parameters common to {{lang}}, {{langx}}, and {{lang-xx}} | |||
['code'] = true, | |||
['text'] = true, | |||
['rtl'] = true, | |||
['italic'] = true, | |||
['italics'] = true, | |||
['i'] = true, | |||
['size'] = true, | |||
['proto'] = true, | |||
['nocat'] = true, | |||
['cat'] = true, | |||
['template'] = true, -- supplied by external templates to provide template name for error messaging ({{nihongo}}, etc) | |||
}, | |||
['params_lang_t'] = { -- unique to {{lang}} | |||
[1] = true, -- alias of |code= | |||
[2] = true, -- alias of |text= | |||
}, | |||
['params_x_t'] = { -- common to {{langx}} and {{lang-xx}} | |||
['translit'] = true, | |||
['translit-std'] = true, | |||
['translit-script'] = true, | |||
['translation'] = true, | |||
['lit='] = true, | |||
['label'] = true, | |||
['link'] = true, | |||
['links'] = true, | |||
['lit'] = true, | |||
['engvar'] = true, | |||
}, | |||
['params_langx_t'] = { -- unique to {{langx}} | |||
[1] = true, -- alias of |code= | |||
[2] = true, -- alias of |text= | |||
[3] = true, -- alias of |translit= | |||
[4] = true, -- alias of |translation= | |||
}, | |||
['params_lang_xx_t'] = { -- unique to {{lang-xx}} | |||
[1] = true, -- alias of |text= | |||
[2] = true, -- alias of |translit= | |||
[3] = true, -- alias of |translation= | |||
['script'] = true, -- these needed to override default params in {{lang-??}} templates | |||
['region'] = true, | |||
['variant'] = true, | |||
}, | |||
} | |||
--[[--------------------------< P A R A M E T E R _ V A L I D A T E >------------------------------------------ | |||
]] | |||
local function parameter_validate (args_t, template) | |||
local err_msg = 'invalid parameter: |%s='; | |||
if templates.lang == template then -- for {{lang}} | |||
for param, _ in pairs (args_t) do | |||
if not known_params_t.params_lang_t[param] and -- unique {{lang}} parameters | |||
not known_params_t.common_params_all_t[param] then -- common to all | |||
return string.format (err_msg, param); -- <param> not found so abandon | |||
end | |||
end | |||
elseif templates.langx == template then -- for {{langx}} | |||
for param, _ in pairs (args_t) do | |||
if not known_params_t.params_langx_t[param] and -- unique {{langx}} parameters | |||
not known_params_t.params_x_t[param] and -- common to {{langx}} and {{lang-xx}} | |||
not known_params_t.common_params_all_t[param] then -- common to all | |||
return string.format (err_msg, param); -- <param> not found so abandon | |||
end | |||
end | |||
elseif templates.langxx == template then -- for {{lang-xx}} | |||
for param, _ in pairs (args_t) do | |||
if not known_params_t.params_lang_xx_t[param] and -- unique {{lang-xx}} parameters | |||
not known_params_t.params_x_t[param] and -- common to {{langx}} and {{lang-xx}} | |||
not known_params_t.common_params_all_t[param] then -- common to all | |||
return string.format (err_msg, param); -- <param> not found so abandon | |||
end | |||
end | |||
end | |||
end | |||
Line 424: | Line 521: | ||
local function make_error_msg (msg, args_t, template) | local function make_error_msg (msg, args_t, template) | ||
local category; | local category; | ||
local text; -- handle the oddity that is {{langx}} | |||
if templates.langxx == template then | |||
text = args_t.text or args_t[1]; -- for {{lang-xx}} | |||
else | |||
text = args_t.text or args_t[2]; -- for {{lang}}, {{langx}}, and {{transliteration}} | |||
end | |||
if 'Transliteration' == template then | if 'Transliteration' == template then | ||
category = 'Transliteration'; | category = 'Transliteration'; | ||
Line 434: | Line 537: | ||
return substitute ('[$1] <span style="color:#d33">Error: {{$2}}: $3 ([[:Category:$4 template errors|help]])</span>$5', | return substitute ('[$1] <span style="color:#d33">Error: {{$2}}: $3 ([[:Category:$4 template errors|help]])</span>$5', | ||
{ | { | ||
text or 'undefined', | |||
template, | template, | ||
msg, | msg, | ||
Line 778: | Line 881: | ||
]] | ]] | ||
local function render_maint(nocat) | local function render_maint (nocat) | ||
local maint = {}; | local maint = {}; | ||
Line 928: | Line 1,031: | ||
return name; -- name from data tables or nil | return name; -- name from data tables or nil | ||
end | |||
--[[--------------------------< T E X T _ S C R I P T _ M A T C H _ T E X T >---------------------------------- | |||
IETF script subtag should match the script of the <text>. This module does not attempt to know all scripts and | |||
what they look like. It does know what Latn script look like so when <text> is written using other than the Latn | |||
script, the IETF script, if present, should not be Latn. | |||
Conversely, when <text> is written using the Latn script, the IETF script, if present, should be Latn. | |||
returns an error message when mismatch detected; nil else | |||
]] | |||
local function text_script_match_test (script, is_latn_text) | |||
--error (script) | |||
if is_set (script) then -- don't bother with the rest of this if <script> is nil or empty string | |||
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 | |||
return 'Latn text/non-Latn script subtag mismatch'; -- emit an error message | |||
end | |||
else -- when text is not wholly Latn script | |||
if ('Latn' == script or 'latn' == script) then -- but Latn script is specified | |||
return 'Non-latn text/Latn script subtag mismatch'; -- emit an error message | |||
end | |||
end | |||
end | |||
end | end | ||
Line 950: | Line 1,082: | ||
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 templates.lang; | ||
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 968: | Line 1,100: | ||
args.text = args[2] or args.text; -- prefer args.text | args.text = args[2] or args.text; -- prefer args.text | ||
end | end | ||
-- msg = parameter_validate (args, templates.lang); | |||
-- if msg then | |||
-- return make_error_msg (msg, args, templates.lang); | |||
-- end | |||
msg = validate_text (template, args); -- ensure that |text= is set | msg = validate_text (template, args); -- ensure that |text= is set | ||
if is_set (msg) then -- msg is an already-formatted error message | if is_set (msg) then -- msg is an already-formatted error message | ||
Line 987: | Line 1,124: | ||
if msg then | if msg then | ||
return make_error_msg ( msg, args, template); | return make_error_msg ( msg, args, template); | ||
end | |||
local is_latn_text = unicode.is_Latin (args.text); -- make a boolean | |||
msg = text_script_match_test (subtags.script, is_latn_text) | |||
if msg then -- if an error detected then there is an error message | |||
return make_error_msg (msg, args, template); | |||
end | end | ||
Line 996: | Line 1,140: | ||
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 | ||
(this_wiki_lang_tag ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and | (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 | args.italic = 'italic'; -- DEFAULT for {{lang}} templates is upright; but if latn script set for font-style:italic | ||
else | else | ||
Line 1,022: | Line 1,166: | ||
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)); | ||
table.insert (out, make_category (code, language_name, args.nocat)); | table.insert (out, make_category (code, language_name, args.nocat)); | ||
table.insert (out, render_maint(args.nocat)); -- maintenance messages and categories | table.insert (out, render_maint (args.nocat)); -- maintenance messages and categories | ||
return table.concat (out); -- put it all together and done | return table.concat (out); -- put it all together and done | ||
Line 1,040: | Line 1,184: | ||
local function lang (frame) | local function lang (frame) | ||
local | 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,054: | Line 1,198: | ||
}); | }); | ||
return _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-??}} | |||
if msg then | |||
return make_error_msg (msg, args_t, templates.lang); -- when template has unsupported params, abandon with error message | |||
end | |||
return _lang (args_t); | |||
end | end | ||
Line 1,144: | Line 1,295: | ||
maint_msgs = {}; | maint_msgs = {}; | ||
local text_idx = ( | local text_idx = (templates.langx == base_template) and 2 or 1; -- for {{langx}} 'text' positional parameter is '2' | ||
local translit_idx = ( | local translit_idx = (templates.langx == base_template) and 3 or 2; | ||
local xlate_idx = ( | local xlate_idx = (templates.langx == base_template) and 4 or 3; | ||
if args[text_idx] and args.text then | if args[text_idx] and args.text then | ||
Line 1,192: | Line 1,343: | ||
return make_error_msg (msg, args, template); | return make_error_msg (msg, args, template); | ||
end | end | ||
local is_latn_text = unicode.is_Latin (args.text); -- make a boolean | |||
msg = text_script_match_test (subtags.script, is_latn_text) | |||
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); | 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 | ||
-- | |||
if templates.langx == base_template then -- auto-italics for {{langx}} templates; adapted from {{lang}} (no support for poem tag) | |||
if nil == args.italic then -- nil when |italic= absent or not set or |italic=default; args.italic controls | |||
if ('latn' == subtags.script) or -- script is latn | |||
(this_wiki_lang_tag ~= code and not is_set (subtags.script) and is_latn_text) then -- text is not this wiki's language, no script specified and is wholly latn script (auto-italics) | |||
args.italic = 'italic'; -- set font-style:italic | |||
else | |||
args.italic = 'inherit'; -- italic not set; script not latn; inherit current style | |||
end | |||
end | |||
else -- {{lang-xx}} does not do auto italics; retained for those wikis that don't support {{langx}} | |||
if nil == args.italic then -- args.italic controls | |||
if is_set (subtags.script) then | |||
if 'latn' == subtags.script then | |||
args.italic = 'italic'; -- |script=Latn; set for font-style:italic | |||
else | |||
args.italic = initial_style_state; -- italic not set; script is not latn; set for font-style:<initial_style_state> | |||
end | |||
else | else | ||
args.italic = initial_style_state; -- italic not set | args.italic = initial_style_state; -- here when |italic= and |script= not set; set for font-style:<initial_style_state> | ||
end | end | ||
end | end | ||
end | end | ||
Line 1,269: | Line 1,428: | ||
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, ('none' == args.label) and language_name or nil)) | table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, ('none' == args.label) and language_name or nil)) | ||
if is_set (args.translit) and not | if is_set (args.translit) and not is_latn_text then -- transliteration; not supported when args.text is wholly latn text (this is an imperfect test) | ||
table.insert (out, ', '); -- comma to separate text from translit | table.insert (out, ', '); -- comma to separate text from translit | ||
if 'none' ~= args.label then | if 'none' ~= args.label then | ||
Line 1,324: | Line 1,483: | ||
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 (( | 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 | ||
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,347: | Line 1,506: | ||
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, templates.langxx); | ||
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_italic|...}} | |||
local msg = parameter_validate (args, templates.langxx); -- verify that all supplied parameters are supported by {{lang-??}} | |||
if msg then | |||
return make_error_msg (msg, args, templates.langxx); -- when template has unsupported params, abandon with error message | |||
end | |||
initial_style_state = 'italic'; | initial_style_state = 'italic'; | ||
return _lang_xx (args, | return _lang_xx (args, templates.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category | ||
end | end | ||
Line 1,362: | Line 1,527: | ||
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, templates.langxx); | ||
end | end | ||
Line 1,373: | Line 1,538: | ||
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, templates.langxx); | ||
args.fn = nil; -- unset because not supported but this function might have been called by {{lang|fn=lang_xx_inherit|...}} | |||
local msg = parameter_validate (args, templates.langxx); -- verify that all supplied parameters are supported by {{lang-??}} | |||
if msg then | |||
return make_error_msg (msg, args, templates.langxx); -- when template has unsupported params, abandon with error message | |||
end | |||
initial_style_state = 'inherit'; | initial_style_state = 'inherit'; | ||
return _lang_xx (args, | return _lang_xx (args, templates.langxx) .. '[[Category:Pages using Lang-xx templates]]'; -- temporary category | ||
end | end | ||
Line 1,388: | Line 1,559: | ||
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, templates.langxx); | ||
end | end | ||
Line 1,400: | Line 1,571: | ||
local function _langx (args_t) | local function _langx (args_t) | ||
local langx_data = mw.loadData ('Module:Lang/langx'); -- get necessary data | local langx_data = mw.loadData ('Module:Lang/langx'); -- get necessary data | ||
local rtl_t = langx_data.rtl_t; -- get list of language tags for languages that are rendered right-to-left | local 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>; disabled because causes deprecated param message | |||
local link_t = langx_data.link_t; -- get list of language tags for {{lang-??}} templates that set |link=<something> | local link_t = langx_data.link_t; -- get list of language tags for {{lang-??}} templates that set |link=<something> | ||
local size_t = langx_data.size_t; -- get list of language tags for {{lang-??}} templates that set |size=<something> | local 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); | |||
if msg then | |||
return make_error_msg (msg, args_t, templates.langx); | |||
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 ('missing language tag', args_t, | return make_error_msg ('missing language tag', args_t, templates.langx); | ||
end | end | ||
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.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; disabled because causes deprecated param message | |||
args_t.link = args_t.link or link_t[args_t.code]; -- prefer |link= in template call, use link_t felse | 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.size = args_t.size or size_t[args_t.code]; -- prefer |size= in template call, use size_t else | ||
Line 1,419: | Line 1,594: | ||
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, | |||
end | end | ||
Line 1,432: | Line 1,605: | ||
but {{langx}} has four: | but {{langx}} has four: | ||
| 1 | 2 | 3 | 4 | | 1 | 2 | 3 | 4 | ||
{{lang-xx |<text> |<transl> |<xlate> }} | {{lang-xx |<text> |<transl> |<xlate> }} | ||
{{langx |<tag> |<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 | 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. | ||
{{lang-??}} depends on the calling template to select 'inherit' or 'italic' | {{lang-??}} depends on the calling template to select 'inherit' or 'italic' to establish the default rendering. | ||
{{langx}} can't do that | |||
lang_xx_inherit(). | {{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 | |||
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, | 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' | ||
return _langx (args_t); | return _langx (args_t); |