Module:Lang: Difference between revisions

2,940 bytes added ,  16 January 2019
synch from sandbox;
m (1 revision imported)
(synch from sandbox;)
Line 805: Line 805:
]]
]]


local function lang (frame)
local function _lang (args)
local args = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text
valueFunc = function (key, value)
if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space
return value; -- return untrimmed 'text'
elseif value then -- all other values: if the value is not nil
value = mw.text.trim (value); -- trim whitespace
if '' ~= value then -- empty string when value was only whitespace
return value;
end
end
return nil; -- value was empty or contained only whitespace
end -- end of valueFunc
});
 
local out = {};
local out = {};
local language_name; -- used to make category names
local language_name; -- used to make category names
Line 902: Line 888:


return table.concat (out); -- put it all together and done
return table.concat (out); -- put it all together and done
end
--[[--------------------------< L A N G >----------------------------------------------------------------------
entry point for {{lang}}
there should be no reason to set parameters in the {{lang}} {{#invoke:}}
<includeonly>{{#invoke:lang|lang}}</includeonly>
parameters are recieved from the template's frame (parent frame)
]]
local function lang (frame)
local args = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text
valueFunc = function (key, value)
if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space
return value; -- return untrimmed 'text'
elseif value then -- all other values: if the value is not nil
value = mw.text.trim (value); -- trim whitespace
if '' ~= value then -- empty string when value was only whitespace
return value;
end
end
return nil; -- value was empty or contained only whitespace
end -- end of valueFunc
});
return _lang (args);
end
end


Line 949: Line 965:
]]
]]


local function _lang_xx (frame)
local function _lang_xx (args)
local args = getArgs(frame,
{
parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}}
valueFunc = function (key, value)
if 1 == key then -- the 'text' parameter; do not trim wite space
return value; -- return untrimmed 'text'
elseif value then -- all other values: if the value is not nil
value = mw.text.trim (value); -- trim whitespace
if '' ~= value then -- empty string when value was only whitespace
return value;
end
end
return nil; -- value was empty or contained only whitespace
end -- end of valueFunc
});
 
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,079: Line 1,079:
if language_name:find ('languages') then
if language_name:find ('languages') 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[code] then
table.insert (out, make_wikilink (lang_data.article_name[code][1], language_name)); -- language name with wikilink from override data
else
else
table.insert (out, make_wikilink (language_name .. ' language', language_name)); -- language name with wikilink
table.insert (out, make_wikilink (language_name .. ' language', language_name)); -- language name with wikilink
Line 1,138: Line 1,140:


return table.concat (out); -- put it all together and done
return table.concat (out); -- put it all together and done
end
--[[--------------------------< L A N G _ X X _ A R G S _ G E T >----------------------------------------------
common function to get args table from {{lang-??}} templates
returns table of args
]]
local function lang_xx_args_get (frame)
local args = getArgs(frame,
{
parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}}
valueFunc = function (key, value)
if 1 == key then -- the 'text' parameter; do not trim wite space
return value; -- return untrimmed 'text'
elseif value then -- all other values: if the value is not nil
value = mw.text.trim (value); -- trim whitespace
if '' ~= value then -- empty string when value was only whitespace
return value;
end
end
return nil; -- value was empty or contained only whitespace
end -- end of valueFunc
});
return args;
end
end


Line 1,148: Line 1,179:


local function lang_xx_italic (frame)
local function lang_xx_italic (frame)
local args = lang_xx_args_get (frame);
initial_style_state = 'italic';
initial_style_state = 'italic';
return _lang_xx (frame);
return _lang_xx (args);
end
 
 
--[[--------------------------< _ L A N G _ X X _ I T A L I C >------------------------------------------------
 
Entry point ffrom another module.  Sets the initial style state to italic.
 
]]
 
local function _lang_xx_italic (args)
initial_style_state = 'italic';
return _lang_xx (args);
end
end


Line 1,160: Line 1,205:


local function lang_xx_inherit (frame)
local function lang_xx_inherit (frame)
local args = lang_xx_args_get (frame);
initial_style_state = 'inherit';
return _lang_xx (args);
end
--[[--------------------------< _ 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.
]]
local function _lang_xx_inherit (args)
initial_style_state = 'inherit';
initial_style_state = 'inherit';
return _lang_xx (frame);
return _lang_xx (args);
end
end


Line 1,173: Line 1,232:
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|name_from_code|<code>}}
templates that are better served by using {{#invoke:lang|name_from_code|<code>}}
Module entry point from another module


]]
]]


local function is_ietf_code (frame)
local function _is_ietf_code (code) -- entry point when this module is require()d into another
return get_ietf_parts (frame.args[1]) and true;
return get_ietf_parts (code) and true;
end
end




--[[--------------------------< N A M E _ F R O M _ C O D E >--------------------------------------------------
--[[--------------------------< I S _ I E T F _ C O D E >------------------------------------------------------
 
Module entry point from an {{#invoke:}}
 
]]
 
local function is_ietf_code (frame) -- entry point from an {{#invoke:Lang|is_ietf_code|<ietf code>}}
return _is_ietf_code (frame.args[1]);
end
 
 
--[[--------------------------< _ N A M E _ F R O M _ C O D E >------------------------------------------------


Returns language name associated with IETF language tag if valid; empty string else.
Returns language name associated with IETF language tag if valid; empty string else.
Line 1,188: Line 1,260:


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


]]
]]


local function name_from_code (frame)
local function _name_from_code (args)
local subtags = {}; -- IETF subtags script, region, variant, and private
local subtags = {}; -- IETF subtags script, region, variant, and private
local raw_code = frame.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 = 'yes' == frame.args['link']; -- save a copy of the link-enable positional parameter (value can be anything)
local link = 'yes' == args['link']; -- make a boolean
local code; -- the language code
local code; -- the language code
local msg; -- gets an error message if IETF language tag is malformed or invalid
local msg; -- gets an error message if IETF language tag is malformed or invalid
Line 1,201: Line 1,275:
code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
if msg then
if msg then
local template = (frame.args['template'] and table.concat ({'{{', frame.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=\"font-size:100%; font-style:normal;\" class=\"error\">error: ', template, msg, '</span>'});
return table.concat ({'<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: ', template, msg, '</span>'});
end
end
Line 1,224: Line 1,298:
if language_name:find ('languages') then
if language_name:find ('languages') then
return make_wikilink (language_name); -- collective language name uses simple wikilink
return make_wikilink (language_name); -- collective language name uses simple wikilink
elseif lang_data.article_name[code] then
return make_wikilink (lang_data.article_name[code][1], language_name); -- language name with wikilink from override data
else
else
return make_wikilink (language_name .. ' language', language_name); -- language name with wikilink
return make_wikilink (language_name .. ' language', language_name); -- language name with wikilink
Line 1,231: Line 1,307:
return language_name;
return language_name;


end
--[[--------------------------< N A M E _ F R O M _ C O D E >--------------------------------------------------
Module entry point from an {{#invoke:}}
]]
local function name_from_code (frame) -- entry point from an {{#invoke:Lang|is_ietf_code|<ietf code>|link=<yes>|template=<template name>}}
return _name_from_code (frame.args); -- pass-on the args table, nothing else
end
end


Line 1,236: Line 1,323:
--[[--------------------------< T R A N S L >------------------------------------------------------------------
--[[--------------------------< T R A N S L >------------------------------------------------------------------


Entry point for {{transl}}
Module entry point from another module


]]
]]


local function transl (frame)
local function _transl (args)
local args = getArgs(frame); -- no {{#invoke:}} parameters
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,300: Line 1,386:
-- here only when all parameters passed to make_translit() are valid
-- here only when all parameters passed to make_translit() are valid
return make_translit (args.code, language_name, args.text, args.translit_std, script, args.italic);
return make_translit (args.code, language_name, args.text, args.translit_std, script, args.italic);
end
--[[--------------------------< T R A N S L >------------------------------------------------------------------
Module entry point from an {{#invoke:}}
]]
local function transl (frame)
local args = getArgs(frame); -- no {{#invoke:}} parameters
return _transl (args);
end
end


Line 1,308: Line 1,406:
return {
return {
lang = lang, -- entry point for {{lang}}
lang = lang, -- entry point for {{lang}}
is_ietf_code = is_ietf_code,
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_code = is_ietf_code,
name_from_code = name_from_code, -- used for template documentation; possible use in ISO 639 name from code templates
name_from_code = name_from_code, -- 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}}
_lang = _lang, -- entry points when this module is require()d into other modules
_lang_xx_inherit = _lang_xx_inherit,
_lang_xx_italic = _lang_xx_italic,
_is_ietf_code = _is_ietf_code,
_name_from_code = _name_from_code,
_transl = _transl,
};
};
Anonymous user