Module:Lang: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
m (1 revision imported) |
(remove deprecated entry points;) |
||
Line 20: | Line 20: | ||
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization |
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization |
||
local this_wiki_lang = mw.language.getContentLanguage().code; -- get this wiki's language |
|||
local maint_cats = {}; -- maintenance categories go here |
local maint_cats = {}; -- maintenance categories go here |
||
Line 75: | Line 77: | ||
end |
end |
||
seg[1] = seg[1] |
seg[1] = invert_pattern_table[seg[1]] or seg[1]; -- replace leading markup according to pattern table |
||
seg[3] = seg[3] |
seg[3] = invert_pattern_table[seg[3]] or seg[3]; -- replace leading markup according to pattern table |
||
return table.concat (seg); -- put it all back together and done |
return table.concat (seg); -- put it all back together and done |
||
Line 478: | Line 480: | ||
if text:match ('^%*') then |
if text:match ('^%*') then |
||
table.insert (html, '*'); -- move proto language text prefix outside of italic markup if any; use numeric entity because |
table.insert (html, '*'); -- move proto language text prefix outside of italic markup if any; use numeric entity because plain splat confuses MediaWiki |
||
text = text:gsub ('^%*', ''); -- remove the splat from the text |
text = text:gsub ('^%*', ''); -- remove the splat from the text |
||
end |
end |
||
Line 730: | Line 732: | ||
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 |
|||
regardless of language name; nil does nothing; presumes that the value in text is correct but removes extra splac |
|||
]] |
]] |
||
local function proto_prefix (text, language_name) |
local function proto_prefix (text, language_name, proto_param) |
||
if false == proto_param then -- when forced by |proto=no |
|||
if language_name:find ('^Proto%-') and not text:find ('^*') then -- language is a proto and text does not already have leading splat |
|||
return |
return text:gsub ('^%**', ''); -- return text without splat prefix regardless of language name or existing splat prefix in text |
||
elseif (language_name:find ('^Proto%-') or (true == proto_param)) then -- language is a proto or forced by |proto=yes |
|||
return text:gsub ('^%**', '*'); -- prefix proto-language text with a splat; also removes duplicate prefixing splats |
|||
end |
end |
||
return text:gsub ('^%*+', '*'); -- return text unmolested except multiple splats reduced to one splat |
|||
return text; |
|||
end |
end |
||
Line 791: | Line 798: | ||
return text, tag; |
return text, tag; |
||
end |
|||
--[[--------------------------< 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, |
|||
empty string (or parameter omitted) returns as nil; any other value returns as nil with a second return value of |
|||
true indicating that some other value has been assigned to |proto= |
|||
]] |
|||
local function validate_proto (proto_param) |
|||
if 'yes' == proto_param then |
|||
return true; |
|||
elseif 'no' == proto_param then |
|||
return false; |
|||
elseif is_set (proto_param) then |
|||
return nil, true; -- |proto= something other than 'yes' or 'no' |
|||
else |
|||
return nil; -- missing or empty |
|||
end |
|||
end |
end |
||
Line 826: | Line 854: | ||
end |
end |
||
msg = validate_text ( |
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 |
||
return msg; |
return msg; |
||
Line 836: | Line 864: | ||
args.rtl = args.rtl == 'yes'; -- convert to boolean: 'yes' -> true, other values -> false |
args.rtl = args.rtl == 'yes'; -- convert to boolean: 'yes' -> true, other values -> false |
||
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag |
|||
if msg then |
|||
return make_error_msg (table.concat ({'invalid |proto=: ', args.proto}), 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}}) |
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}}) |
||
Line 850: | Line 883: | ||
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 ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and unicode.is_Latin (args.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 880: | Line 913: | ||
args.text = invert_italics (args.text) |
args.text = invert_italics (args.text) |
||
end |
end |
||
args.text = proto_prefix (args.text, language_name); |
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 977: | Line 1,010: | ||
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 'lang-xx'; |
|||
if args[1] and args.text then |
if args[1] and args.text then |
||
return make_error_msg ('conflicting: {{{1}}} and |text=', args, |
return make_error_msg ('conflicting: {{{1}}} and |text=', args, template); |
||
else |
else |
||
args.text = args[1] or args.text; -- prefer args.text |
args.text = args[1] or args.text; -- prefer args.text |
||
end |
end |
||
msg = validate_text ( |
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 |
||
return msg; |
return msg; |
||
Line 992: | Line 1,026: | ||
if args[2] and args.translit then |
if args[2] and args.translit then |
||
return make_error_msg ('conflicting: {{{2}}} and |translit=', args, |
return make_error_msg ('conflicting: {{{2}}} and |translit=', args, template); |
||
else |
else |
||
args.translit = args[2] or args.translit -- prefer args.translit |
args.translit = args[2] or args.translit -- prefer args.translit |
||
Line 998: | Line 1,032: | ||
if args[3] and (args.translation or args.lit) then |
if args[3] and (args.translation or args.lit) then |
||
return make_error_msg ('conflicting: {{{3}}} and |lit= or |translation=', args, |
return make_error_msg ('conflicting: {{{3}}} 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, |
return make_error_msg ('conflicting: |lit= and |translation=', args, template); |
||
else |
else |
||
args.translation = args[3] or args.translation or args.lit; -- prefer args.translation |
args.translation = args[3] or args.translation or args.lit; -- prefer args.translation |
||
Line 1,006: | Line 1,040: | ||
if args.links and args.link then |
if args.links and args.link then |
||
return make_error_msg ('conflicting: |links= and |link=', args, |
return make_error_msg ('conflicting: |links= and |link=', args, template); |
||
else |
else |
||
args.link = args.link or args.links; -- prefer args.link |
args.link = args.link or args.links; -- prefer args.link |
||
Line 1,018: | Line 1,052: | ||
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, |
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, |
return make_error_msg (msg, args, template); |
||
end |
end |
||
Line 1,044: | Line 1,078: | ||
args.rtl = false; -- script is not an rtl script |
args.rtl = false; -- script is not an rtl script |
||
end |
end |
||
end |
|||
args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag |
|||
if msg then |
|||
return make_error_msg (table.concat ({'invalid |proto=: ', args.proto}), args, template); |
|||
end |
end |
||
Line 1,092: | Line 1,131: | ||
end |
end |
||
args.text = proto_prefix (args.text, language_name); |
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)) |
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size)) |
||
Line 1,109: | Line 1,148: | ||
table.insert (out, make_wikilink ('Romanization of ' .. translit_script_name or language_name, 'romanized') .. ':'); -- make a wikilink if there is an article to link to |
table.insert (out, make_wikilink ('Romanization of ' .. translit_script_name or language_name, 'romanized') .. ':'); -- make a wikilink if there is an article to link to |
||
else |
else |
||
table.insert (out, 'romanized:'); -- else plain text |
|||
table.insert (out, 'romanized:'); -- else define the abbreviation |
|||
end |
end |
||
table.insert (out, ' </small>'); -- close the small tag |
table.insert (out, ' </small>'); -- close the small tag |
||
Line 1,119: | Line 1,157: | ||
table.insert (out, translit); |
table.insert (out, translit); |
||
else |
else |
||
return make_error_msg (table.concat ({'invalid translit-std: \'', args['translit-std'] or '[missing]'}), args, |
return make_error_msg (table.concat ({'invalid translit-std: \'', args['translit-std'] or '[missing]'}), args, template); |
||
end |
end |
||
end |
end |
||
Line 1,225: | Line 1,263: | ||
--[[--------------------------< I S _ I E T F _ |
--[[--------------------------< _ I S _ I E T F _ T A G >------------------------------------------------------ |
||
Returns true when a language name associated with IETF language tag exists; nil else. |
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-xx}} templates are supported by this function. |
All code combinations supported by {{lang}} and the {{lang-xx}} templates are supported by this function. |
||
The purpose of this function is to replace {{#ifexist:Template:ISO 639 name xx|<exists>|<does not exist>}} in |
The purpose of this function is to replace {{#ifexist:Template:ISO 639 name xx|<exists>|<does not exist>}} in |
||
templates that are better served by using {{#invoke:lang| |
templates that are better served by using {{#invoke:lang|name_from_tag|<code>}} |
||
Module entry point from another module |
Module entry point from another module |
||
Line 1,238: | Line 1,276: | ||
]] |
]] |
||
local function |
local function _is_ietf_tag (tag) -- entry point when this module is require()d into another |
||
local c, s, r, v, p, err; -- code, script, region, private, error message |
|||
return get_ietf_parts (code) and true; |
|||
c, s, r, v, p, err = get_ietf_parts (tag); -- disassemble tag into constituent part and validate |
|||
return ((c and not err) and true) or nil; -- return true when code portion has a value without error message; nil else |
|||
end |
end |
||
--[[--------------------------< I S _ I E T F _ |
--[[--------------------------< I S _ I E T F _ T A G >-------------------------------------------------------- |
||
Module entry point from an {{#invoke:}} |
Module entry point from an {{#invoke:}} |
||
Line 1,249: | Line 1,290: | ||
]] |
]] |
||
local function |
local function is_ietf_tag (frame) -- entry point from an {{#invoke:Lang|is_ietf_tag|<ietf tag>}} |
||
return |
return _is_ietf_tag (frame.args[1]); -- frame.args[1] is the ietf language tag |
||
end |
end |
||
Line 1,266: | Line 1,307: | ||
]] |
]] |
||
local function |
local function _name_from_tag (args) |
||
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 |
||
Line 1,317: | Line 1,358: | ||
]] |
]] |
||
local function |
local function name_from_tag (frame) -- entry point from an {{#invoke:Lang|name_from_tag|<ietf tag>|link=<yes>|template=<template name>}} |
||
return |
return _name_from_tag (frame.args); -- pass-on the args table, nothing else |
||
end |
end |
||
--[[--------------------------< T R |
--[[--------------------------< _ 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 |
|||
according to the spelling in the source tables. When a standard language name has a parenthetical disambiguator, |
|||
that disambiguator must be omitted (they are not present in the data name-to-tag tables). |
|||
Module entry point from another module |
|||
]] |
|||
local function _tag_from_name (args) -- entry point from another module |
|||
local msg; |
|||
if args[1] and '' ~= args[1] then |
|||
local data = mw.loadData ('Module:Lang/name to tag'); -- get the reversed data tables |
|||
local lang = args[1]:lower(); -- allow any-case for the language name (speeling must till be correct) |
|||
local tag = data.rev_lang_data[lang] or data.rev_lang_name_table[lang];-- get the code; look first in the override then in the standard |
|||
if tag then |
|||
return tag; -- language name found so return tag and done |
|||
else |
|||
msg = 'language: ' .. args[1] .. ' not found' -- language name not found, error message |
|||
end |
|||
else |
|||
msg = 'missing language name' -- language name not provided, error message |
|||
end |
|||
local template = ''; |
|||
if args.template and '' ~= args.template then |
|||
template = table.concat ({'{{', args['template'], '}}: '}); -- make template name (if provided by the template) |
|||
end |
|||
return table.concat ({'<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: ', template, msg, '</span>'}); |
|||
end |
|||
--[[--------------------------< T A G _ F R O M _ N A M E >---------------------------------------------------- |
|||
Module entry point from an {{#invoke:}} |
|||
]] |
|||
local function tag_from_name (frame) -- entry point from an {{#invoke:Lang|tag_from_name|<language name>|link=<yes>|template=<template name>}} |
|||
return _tag_from_name (frame.args); -- pass-on the args table, nothing else |
|||
end |
|||
--[[--------------------------< _ T R A N S L >---------------------------------------------------------------- |
|||
Module entry point from another module |
Module entry point from another module |
||
Line 1,409: | Line 1,496: | ||
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, |
||
is_ietf_tag = is_ietf_tag, |
|||
is_ietf_code = is_ietf_code, |
|||
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 |
|||
transl = transl, -- entry point for {{transl}} |
transl = transl, -- entry point for {{transl}} |
||
Line 1,416: | Line 1,504: | ||
_lang_xx_inherit = _lang_xx_inherit, |
_lang_xx_inherit = _lang_xx_inherit, |
||
_lang_xx_italic = _lang_xx_italic, |
_lang_xx_italic = _lang_xx_italic, |
||
_is_ietf_tag = _is_ietf_tag, |
|||
_is_ietf_code = _is_ietf_code, |
|||
_tag_from_name = _tag_from_name, |
|||
_name_from_code = _name_from_code, |
|||
_name_from_tag = _name_from_tag, |
|||
_transl = _transl, |
_transl = _transl, |
||
}; |
}; |