మాడ్యూల్:ISO 639 name: కూర్పుల మధ్య తేడాలు

వికీపీడియా నుండి
Jump to navigation Jump to search
Content deleted Content added
చి en:Module:ISO_639_name నుండి కూర్పులను దిగుమతి చేసాం
sync from sandbox;
పంక్తి 3: పంక్తి 3:
local getArgs = require ('Module:Arguments').getArgs;
local getArgs = require ('Module:Arguments').getArgs;
local override_data = mw.loadData ('Module:Language/data/ISO 639 override');
local override_data = mw.loadData ('Module:Language/data/ISO 639 override');
--local override_data = mw.loadData ('Module:Language/data/ISO 639 override/sandbox');
local deprecated_data = mw.loadData ('Module:Language/data/ISO 639 deprecated');
local parts = {
{'Module:Language/data/ISO 639-1', '1'},
{'Module:Language/data/ISO 639-2', '2'},
{'Module:Language/data/ISO 639-2B', '2B'},
{'Module:Language/data/ISO 639-3', '3'},
{'Module:Language/data/ISO 639-5', '5'},
}




--[[--------------------------< E R R O R _ M E S S A G E S >--------------------------------------------------
--[[--------------------------< E R R O R _ M E S S A G E S >--------------------------------------------------

TODO: change to default hiding of error messages? show with with personal css override:
.show_639_err_msgs {display: inline !important;}


]]
]]


local error_messages = {
local error_messages = {
-- ['err_msg'] = '<span style="font-size:100%; display:none" class="error show_639_err_msgs">error: $1</span>[[Category:ISO 639 name template errors]]',
['err_msg'] = '<span style="font-size:100%;" class="error show_639_err_msgs">error: $1 ([[Template:ISO 639 name|help]])</span>',
['err_msg'] = '<span style="font-size:100%;" class="error show_639_err_msgs">error: $1</span>[[Category:ISO 639 name template errors]]',
['err_text'] = { -- error messages used only in the code to name functions
['err_text'] = { -- error messages used only in the code to name functions
['ietf'] = '$1 is an IETF tag', -- $1 is the ietf tag
['ietf'] = '$1 is an IETF tag', -- $1 is the ietf tag
['required'] = 'ISO 639$1 code is required', -- $1 is the 639 '-1', '-2', '-3', '-5' part suffix; may be empty string
['required'] = 'ISO 639$1 code is required', -- $1 is the 639 '-1', '-2', '-3', '-5' part suffix; may be empty string
['not_code'] = '$1 is not an ISO 639$2 code', -- $1 is non-code input; $2 is 639 part suffix; may be empty string


-- code to name functions and iso_639_name_to_code()
-- code to name functions and iso_639_name_to_code()
పంక్తి 26: పంక్తి 30:
['name'] = 'language name required',
['name'] = 'language name required',
['not_part'] = '$1 not an ISO 639 part', -- $1 is invalid 639 suffix (without hyphen)
['not_part'] = '$1 not an ISO 639 part', -- $1 is invalid 639 suffix (without hyphen)

['no_code'] = 'no code in ISO 639-$1 for $2', -- $1 is 639 part suffix; $2 is language name
['code_name'] = 'language code or name required', -- iso_639() only
}
}
}
}

local error_cat = '[[Category:ISO 639 name template errors]]';


--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------

Populates numbered arguments in a message string using an argument table.

]]

local function substitute (msg, args)
return args and mw.message.newRawMessage (msg, args):plain() or msg;
end


--[[--------------------------< E R R O R _ M S G >------------------------------------------------------------

create an error message

]]

local function error_msg (msg, arg, hide, nocat)
local retval = '';
if not hide then
retval = substitute (error_messages.err_msg, substitute (error_messages.err_text[msg], arg));
retval = nocat and retval or (retval .. error_cat);
end
return retval
end




పంక్తి 37: పంక్తి 71:
]]
]]


local function is_set( var )
local function is_set (var)
return not (var == nil or var == '');
return not (var == nil or var == '');
end
end
పంక్తి 60: పంక్తి 94:
return '';
return '';
end
end
end


--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------

Populates numbered arguments in a message string using an argument table.

]]

local function substitute (msg, args)
return args and mw.message.newRawMessage (msg, args):plain() or msg;
end


--[[--------------------------< E R R O R _ M S G >------------------------------------------------------------

create an error message

]]

local function error_msg (msg, arg)
return substitute (error_messages.err_msg, substitute (error_messages.err_text[msg], arg))
end
end


పంక్తి 92: పంక్తి 104:
]]
]]


local function lang_name_get (code, data, link, label)
local function lang_name_get (code, data, link, label, raw)
local name;
local name;
if data[code] then
if data[code] then
name = data[code][1]:gsub ('%s*%b()', ''); -- get the name and strip parenthetical disambiguators if any
-- name = raw and data[code][1] or data[code][1]:gsub ('%s*%b()', ''); -- get the name; strip parenthetical disambiguators if any when <raw> is false
if raw then
name = data[code][1];
else
name = data[code][1]:gsub ('%s*%b()', ''); -- strip parenthetical disambiguators if any
name = name:gsub ('([^,]-), +(.+)', '%2 %1'); -- if inverted, uninvert
end
if link then -- make a link to the language article?
if link then -- make a link to the language article?
if name:find ('languages') then
if name:find ('languages') or name:find ('[Ll]anguage$') then
name = make_wikilink (name, label); -- simple wikilink for collective languages unless there is a label
name = make_wikilink (name, label); -- simple wikilink for collective languages or langauges ending in 'Language' unless there is a label
elseif override_data.article_name[code] then
elseif override_data.article_name[code] then
name = make_wikilink (override_data.article_name[code][1], label or name); -- language name or label with wikilink from override data
name = make_wikilink (override_data.article_name[code][1], label or name); -- language name or label with wikilink from override data
పంక్తి 115: పంక్తి 132:
assembles return-text (language code, language name, or error message) with IETF error message into properly
assembles return-text (language code, language name, or error message) with IETF error message into properly
formatted readable text
formatted readable text

|hide-err=yes suppresses error message and category
|cat=no supresses category


]]
]]


local function add_ietf_error_msg (text, ietf_err)
local function add_ietf_error_msg (text, ietf_err, hide, nocat)
if hide then
return table.concat ({
ietf_err = '';
end

if not nocat then -- |cat= empty or omitted -> nocat=false
nocat = '' == ietf_err; -- spoof; don't add cat when no error
end

return table.concat ({ -- tack on ietf error message if one exists
text, -- code name, language name, or error message
text, -- code name, language name, or error message
'' ~= ietf_err and ' ' or '', -- needs a space when ietf_err is not empty
'' ~= ietf_err and ' ' or '', -- needs a space when ietf_err is not empty
ietf_err,
ietf_err,}); -- tack on ietf error message if one exists
nocat and '' or error_cat, -- add error category when |cat=<aynthing but 'no'>
});
end
end




--[[--------------------------< _ I S O _ 6 3 9 _ N A M E >----------------------------------------------------
--[[--------------------------< G E T _ P A R T _ I N D E X >--------------------------------------------------


gets index suitable for parts{} table from ISO 639-<part> (usually args[2])
searches through the ISO 639 language tables for a name that matches the supplied code. on success returns first
language name that matches code from template frame perhaps with an error message and a second return value of true;
on failure returns the provided input text, and error message and a second return value of nil. The second return
value is a return value used by iso_639_name_exists()


return valid index [1] - [5]; nil else
looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data
1 <- part ['1']
2 <- part ['2'] -- this is part 2T
3 <- part ['2B']
4 <- part ['3']
nil <- part ['4'] -- there is no part 4
5 <- part ['5']


]]
]]


local function _iso_639_name (frame)
local function get_part_index (part)
return ({['1']=1, ['2']=2, ['2B']=3, ['3']=4, ['4']=nil, ['5']=5})[part]
local args = getArgs(frame);
end

if not args[1] then

return error_msg ('required', ''); -- empty string doesn't specify a 639 part (hides $1 in error message)
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ T O _ N A M E _ C O M M O N >------------------------

this is code that is common to all of the iso_639_code_n_to_name() functions which serve only as template entry
points to provide the frame, the name of the appropriate data source, and to identify which 639 part applies.

this function returns a language name or an error message. data is searched in this order:
part-specific override data -> standard part data -> part-specific deprecated data

a second retval used by _iso_639_code_to_name() is true when a code is found; nil else

]]

local function iso_639_code_to_name_common (args, source, part)
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization
local nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use)
local raw = 'yes' == args.raw; -- disable override and dab removal
local data; -- one of the override or part tables
local name; -- holds language name from data

if not args[1] then -- if code not provided in the template call
return error_msg ('required', '-' .. part, hide, nocat); -- abandon
end
end

local code = args[1]; -- used in error messaging
local code; -- used for error messaging
local lc_code; -- holds lowercase version of code for indexing into the data tables
local ietf_err; -- holds an error message when args[1] (language code) is in IETF tag form (may or may not be a valid IETF tag)
local ietf_err; -- holds an error message when args[1] (language code) is in IETF tag form (may or may not be a valid IETF tag)
code, ietf_err = args[1]:gsub('(.-)%-.*', '%1'); -- strip ietf subtags; ietf_err is non-zero when subtags are stripped
local name; -- the retrieved language name
ietf_err = (0 ~= ietf_err) and error_msg ('ietf', args[1], hide, nocat) or ''; -- when tags are stripped create an error message; empty string for concatenation else
local data = {}; -- holds one of the various 639 code to name tables
local link = 'yes' == args.link; -- make a boolean


if not raw then -- when raw is true, fetch name as is from part data; ignore override
code, ietf_err = code:gsub('(.-)%-.*', '%1'); -- strip ietf subtags; ietf_err is non-zero when subtags are stripped
data = override_data['override_' .. part]; -- get override data for this part
lc_code = code:lower();
name = lang_name_get (code:lower(), data, args.link, args.label, raw); -- get override language name if there is one
end
if not name then
data = mw.loadData (source); -- get the data for this ISO 639 part
name = lang_name_get (code:lower(), data, args.link, args.label, raw); -- get language name if there is one
end


if not name then -- TODO: do something special to indicate when a name is fetched from deprecated data?
ietf_err = (0 ~= ietf_err) and error_msg ('ietf', args[1]) or ''; -- when tags are stripped create an error message; empty string for concatenation else
data = deprecated_data['deprecated_' .. part]; -- get deprecated data for this part
name = lang_name_get (code:lower(), data, args.link, args.label, raw); -- get deprecated language name if there is one


if not name then
if 2 > #code or 3 < #code then -- 639 codes are 2 or three characters only
return table.concat ({code, ' ', error_msg ('not_code', {code, ''})}); -- return whatever is in code + an error message; empty string hides $1
return error_msg ('not_found', {code, part}, hide, nocat); -- code not found, return error message
end
end
end
return add_ietf_error_msg (name, ietf_err, hide, nocat), true; -- return language name with ietf error message if any; true because we found a code
end



data = override_data.override; -- first look in the override table
--[[--------------------------< _ I S O _ 6 3 9 _ C O D E _ T O _ N A M E >------------------------------------
name = lang_name_get (lc_code, data, link, args.label);

if name then
searches through the ISO 639 language tables for a name that matches the supplied code. on success returns first
return add_ietf_error_msg (name, ietf_err), true;
language name that matches code from template frame perhaps with an error message and a second return value of true;
on failure returns an error message and a second return value of nil. The second return value is a return value
used by iso_639_code_exists()

looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data

]]

local function _iso_639_code_to_name (frame)
local args = getArgs(frame);
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization
local nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use)
if not args[1] then -- if code not provided in the template call
return error_msg ('required', '', hide, nocat); -- abandon
end
end


local name; -- the retrieved language name and / or error message
if 2 == #lc_code then
local found; -- set to true when language name is found
data = mw.loadData ('Module:Language/data/iana languages'); -- this data used only for ISO 639-1 language codes / names listed there

name = lang_name_get (lc_code, data, link, args.label);
for _, part in ipairs (parts) do
if name then
name, found = iso_639_code_to_name_common (args, part[1], part[2]);
return add_ietf_error_msg (name, ietf_err), true;
if found then
end
return name, true; -- second retval for iso_639_name_exists()
else
for _, source in ipairs ({ -- loop sequentially through the other data tables
'Module:Language/data/ISO 639-2',
'Module:Language/data/ISO 639-3',
'Module:Language/data/ISO 639-5'
}) do
data = mw.loadData (source);
name = lang_name_get (lc_code, data, link, args.label);
if name then
return add_ietf_error_msg (name, ietf_err), true;
end
end
end
end
end

return error_msg ('not_found', {code, '1, -2, -3, -5'}); -- here when code is not found in the data tables
return error_msg ('not_found', {args[1], '1, -2, -2B, -3, -5'}, hide, nocat); -- here when code (args[1]) is not found in the data tables
end
end




--[[--------------------------< I S O _ 6 3 9 _ N A M E >------------------------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ T O _ N A M E >--------------------------------------


template entry point; returns first language name that matches code from template frame or an error message
template entry point; returns first language name that matches code from template frame or an error message
పంక్తి 197: పంక్తి 266:
]]
]]


local function iso_639_name (frame)
local function iso_639_code_to_name (frame)
local ret_val = _iso_639_name (frame); -- ignore second return value
local ret_val = _iso_639_code_to_name (frame); -- ignore second return value
return ret_val; -- return language name and / or error message
return ret_val; -- return language name and / or error message
end
end




--[[--------------------------< I S O _ 6 3 9 _ N A M E _ E X I S T S >----------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ E X I S T S >----------------------------------------


template entry point; returns true if language code maps to a language name; intended as a replacement for:
template entry point; returns true if language code maps to a language name; intended as a replacement for:
{{#exist:Template:ISO 649 name <code>|<exists>|<doesn't exist>}}
{{#exist:Template:ISO 639 name <code>|<exists>|<doesn't exist>}}
Instead of that expensive parser function call use this function:
Instead of that expensive parser function call use this function:
{{#if:{{#invoke:Sandbox/trappist the monk/ISO 639 name|iso_639_name_exists|<code>}}|<exists>|<doesn't exist>}}
{{#if:{{#invoke:ISO 639 name|iso_639_code_exists|<code>}}|<exists>|<doesn't exist>}}
on success, returns true; nil else
on success, returns true; nil else


]]
]]


local function iso_639_name_exists (frame)
local function iso_639_code_exists (frame)
local _, exists;
local _, exists;
_, exists = _iso_639_name (frame); -- ignore name/error message return; exists is true when name found for code; nil else
_, exists = _iso_639_code_to_name (frame); -- ignore name/error message return; <exists> is true when name found for code; nil else
return exists;
return exists;
end
end




--[[--------------------------< I S O _ 6 3 9 _ C O D E _ C O M M O N >----------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 1 _ T O _ N A M E >----------------------------------


template entry point; returns first language name that matches ISO 639-1 code from template frame or an error message
this is code that is common to all of the iso_639_code_n() functions which serve only as template entry points to
provide the frame, the name of the appropriate data source, and to identify which 639 part applies.

this function returns a language name or an error message


]]
]]


local function iso_639_code_common (frame, source, part)
local function iso_639_code_1_to_name (frame)
local args = getArgs(frame);
local args = getArgs (frame);
local retval = iso_639_code_to_name_common (args, parts[1][1], parts[1][2]); -- suppress second return value
return retval;
end


if not args[1] then -- if code not provided in the template call
return error_msg ('required', '-' .. part); -- abandon
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 2 _ T O _ N A M E >----------------------------------
local code; -- used for error messaging
local ietf_err; -- holds an error message when args[1] (language code) is in IETF tag form (may or may not be a valid IETF tag)
code, ietf_err = args[1]:gsub('(.-)%-.*', '%1'); -- strip ietf subtags; ietf_err is non-zero when subtags are stripped
ietf_err = (0 ~= ietf_err) and error_msg ('ietf', args[1]) or ''; -- when tags are stripped create an error message; empty string for concatenation else


template entry point; returns first language name that matches ISO 639-2 code from template frame or an error message
if (1 == part and 2 ~= #code) or (1 < part and 3 ~= #code) then -- 639-1 codes are 2 characters only; all others 3 characters
return error_msg ('not_code', {code, '-' .. part});
end


]]
local data = mw.loadData (source); -- get the data


local function iso_639_code_2_to_name (frame)
return add_ietf_error_msg (lang_name_get (code:lower(), data, args.link) or error_msg ('not_found', {code, part}), ietf_err);
local args = getArgs (frame);
local retval = iso_639_code_to_name_common (args, parts[2][1], parts[2][2]); -- suppress second return value
return retval;
end
end




--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 1 >--------------------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 2 B _ T O _ N A M E >--------------------------------


template entry point; returns first language name that matches ISO 639-1 code from template frame or an error message
template entry point; returns first language name that matches ISO 639-2 code from template frame or an error message


]]
]]


local function iso_639_code_1 (frame)
local function iso_639_code_2B_to_name (frame)
local args = getArgs (frame);
return iso_639_code_common (frame, 'Module:Language/data/iana languages', 1); -- this data used only for ISO 639-1 language codes / names listed there
local retval = iso_639_code_to_name_common (args, parts[3][1], parts[3][2]); -- suppress second return value
return retval;
end
end




--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 2 >--------------------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 3 _ T O _ N A M E >----------------------------------


template entry point; returns first language name that matches ISO 639-2 code from template frame or an error message
template entry point; returns first language name that matches ISO 639-3 code from template frame or an error message


]]
]]


local function iso_639_code_2 (frame)
local function iso_639_code_3_to_name (frame)
local args = getArgs (frame);
return iso_639_code_common (frame, 'Module:Language/data/ISO 639-2', 2); -- ISO 639-2 language codes / names
local retval = iso_639_code_to_name_common (args, parts[4][1], parts[4][2]); -- suppress second return value
return retval;
end
end




--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 3 >--------------------------------------------------
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 5 _ T O _ N A M E >----------------------------------


template entry point; returns first language name that matches ISO 639-3 code from template frame or an error message
template entry point; returns first language name that matches ISO 639-5 code from template frame or an error message


]]
]]


local function iso_639_code_3 (frame)
local function iso_639_code_5_to_name (frame)
local args = getArgs (frame);
return iso_639_code_common (frame, 'Module:Language/data/ISO 639-3', 3); -- ISO 639-3 language codes / names
local retval = iso_639_code_to_name_common (args, parts[5][1], parts[5][2]); -- index [4] -> part 5 because there is no part 4; suppress second return value
return retval;
end
end




--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 5 >--------------------------------------------------
--[[--------------------------< N A M E _ I N _ P A R T _ C O D E _ G E T >------------------------------------


indexes into the <name_data> using <name> and extracts the language code assigned to <part> (1, 2, 2B, 3, 5).
template entry point; returns first language name that matches ISO 639-5 code from template frame or an error message
attempts to index override data first; returns code on success, nil else


]]
]]


local function iso_639_code_5 (frame)
local function name_in_part_code_get (name, part, part_idx, name_data)
return name_data[name] and (
return iso_639_code_common (frame, 'Module:Language/data/ISO 639-5', 5); -- ISO 639-5 language codes / names
name_data[name][part_idx+5] or -- see if the name exists in the part's override table
name_data[name][part_idx] or -- see if the name exists in the part's main table
name_data[name][part_idx+10] -- see if the name exists in the part's deprecated table
);
end
end




--[[--------------------------< I S O _ 6 3 9 _ N A M E _ T O _ C O D E >--------------------------------------------------
--[[--------------------------< _ I S O _ 6 3 9 _ N A M E _ T O _ C O D E >------------------------------------


template entry point; returns ISO 639-1, -2, -3, or -5 code associated with language name according to part (1, 2, 3, 5) argument;
module entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part
when part is not provided scans 1, 2, 3 , 5 and returns first code
(1, 2, 2B, 3, 5) argument; when part is not provided scans 1, 2, 2B, 3, 5 and returns first code

override data are examined first

<args> is frame arguments from getArgs(frame)


]]
]]


local function iso_639_name_to_code (frame)
local function _iso_639_name_to_code (args)
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization
local args = getArgs(frame);
local nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use)

if not args[1] then
if not args[1] then
return error_msg ('name');
return error_msg ('name', '', hide, nocat); -- abandon when language name missing
end
end
local name = args[1]; -- used in error messaging
local name = args[1]; -- used in error messaging
local lc_name = name:lower(); -- holds lowercase version of name for indexing into the data table
local lc_name = name:gsub(' +', ' '):lower(); -- lowercase version of name for indexing into the data table; strip extraneous space characters


local part_idx;
local part = nil or args[2] and tonumber(args[2]);
local part = args[2];
if part then
if part then
part_idx = get_part_index (part);
if not ({'1', '2', '3', nil, '5'})[part] then
if not part_idx then
return error_msg ('not_part', part); -- part is not an ISO 639 part
return error_msg ('not_part', part, hide, nocat); -- abandon; args[2] is not a valid ISO 639 part
end
end
end
end


local name_data = mw.loadData ('Module:Language/data/ISO 639 override').override; -- first check the override data
local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code'); -- ISO 639 language names to code table
-- local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code/sandbox'); -- ISO 639 language names to code table
local code; -- place to hold the code if not a 2-character code

for k, v in pairs (name_data) do -- spin through the override table
local code;
if lc_name == v[1]:lower() then -- if name is found and
if part then -- when part specified
if part then
if 1 == part then -- if looking for part 1 codes
code = name_in_part_code_get (lc_name, part, part_idx, name_data); -- search the specified override table + part table
if 2 == #k then
else
return k; -- return 2 char code; don't care about 3 char codes here
for part_idx, part_tag in ipairs ({'1', '2', '2B', '3', '5'}) do -- no part provided, spin through all parts override first and get the first available code
end
code = name_in_part_code_get (lc_name, part_tag, part_idx, name_data);
else -- parts 2, 3, 5
if code then -- nil when specified <part> does not have code for specified language <name>
if 3 == #k then
return k; -- return 3 char code
break; -- when code is not nil, done
end
end
else -- part not specified
if 2 == #k then
return k; -- always return 2 char code when found
else
code = k; -- save 3 char code in case there isn't a 2 char code
end
end
end
end
end
end
end
if code then
if code then -- has a value if we found a 3-character code
return code;
return code, true;
end
end
return error_msg ('not_found', {name, part or '1, -2, -2B, -3, -5'}, hide, nocat), false;
end


local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code'); -- ISO 639 language names to code table


--[[--------------------------< I S O _ 6 3 9 _ N A M E _ T O _ C O D E >--------------------------------------
if name_data[lc_name] then

if part then
template entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part
if 5 == part then
(1, 2, 2B, 3, 5) argument; when part is not provided scans 1, 2, 2B, 3, 5 and returns first code
part = 4; -- part 5 codes are at name_data[lc_name][4]; there is no part 639-4

end
override data are examined first
if '' ~= name_data[lc_name][part] then

return name_data[lc_name][part];
args[1] is language name
else
args[2] is ISO 639 part
return error_msg ('no_code', {part, name}); -- no code in ISO 639-part for language

end
]]
else

for _, v in ipairs ({1, 2, 3, 5-1}) do -- no part provided, scan through name's list of codes to get the first available code
local function iso_639_name_to_code (frame)
if '' ~= name_data[lc_name][v] then
local args = getArgs(frame);
return name_data[lc_name][v];
local result, _ = _iso_639_name_to_code (args); -- suppress true/false return used by iso_639_name_exists()
end
return result;
end


--[[--------------------------< I S O _ 6 3 9 _ N A M E _ E X I S T S >----------------------------------------

template entry point; returns ISO 639-1, -2, -3, or -5 code associated with language name according to part (1, 2, 3, 5) argument;
when part is not provided scans 1, 2, 3 , 5 and returns first code

override data are examined first

args[1] is language name
args[2] is ISO 639 part

]]

local function iso_639_name_exists (frame)
local args = getArgs(frame);
local _, result = _iso_639_name_to_code (args); -- suppress code return used by iso_639_name_to_code()
return result and true or nil;
end


--[[--------------------------< I S O _ 6 3 9 >----------------------------------------------------------------

template entry point.
returns:
language name if args[1] is valid language code
language code if args[1] is valid language name

this function is constrained to the ISO 639 part specified in args[2] which must be 1, 2, 2B, 3, or 5. When not provided
all parts are tested. The first match is found

]]

local function iso_639 (frame)
local args = getArgs (frame);
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization
local nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use)
local result;
local found; -- set to true when language name is found

if not args[1] then
return error_msg ('code_name', '', hide, nocat);
end

local part = args[2];
if part then -- if ISO 639 part supplied
local part_idx = get_part_index (part); -- map index from <part>; anything else nil

if not part_idx then
return error_msg ('not_part', part, hide, nocat); -- abandon; args[2] is not a valid ISO 639 part
end

result, found = iso_639_code_to_name_common (args, parts[part_idx][1], parts[part_idx][2]); -- attempt to find a code match
if found then
return result; -- found the code so return the language name
end

result = _iso_639_name_to_code (args); -- might be a language name; return code if it is; error message or empty string else
return result; -- this way to suppress second return

else
for _, part in ipairs (parts) do -- for each of the iso 639 parts
result, found = iso_639_code_to_name_common (args, part[1], part[2]); -- attempt to find a code match
if found then
return result; -- found the code so return the language name
end
end
end
end
end
else

return error_msg ('not_found', {name, part or '1, -2, -3, -5'});
result = _iso_639_name_to_code (args); -- might be a language name; return code if it is; error message or empty string else
end
return result; -- this way to suppress second return
end
end


పంక్తి 376: పంక్తి 519:


return {
return {
iso_639 = iso_639, -- returns code when given name; returns name when given code
iso_639_name = iso_639_name,

iso_639_code_exists = iso_639_code_exists,
iso_639_name_exists = iso_639_name_exists,
iso_639_name_exists = iso_639_name_exists,

iso_639_code_1 = iso_639_code_1,
iso_639_code_to_name = iso_639_code_to_name,
iso_639_code_2 = iso_639_code_2,
iso_639_code_1_to_name = iso_639_code_1_to_name,
iso_639_code_3 = iso_639_code_3,
iso_639_code_2_to_name = iso_639_code_2_to_name,
iso_639_code_5 = iso_639_code_5,
iso_639_code_2B_to_name = iso_639_code_2B_to_name,
iso_639_code_3_to_name = iso_639_code_3_to_name,
iso_639_code_5_to_name = iso_639_code_5_to_name,

iso_639_name_to_code = iso_639_name_to_code,
iso_639_name_to_code = iso_639_name_to_code,
};
};

13:06, 5 అక్టోబరు 2020 నాటి కూర్పు

require('Module:No globals');

local getArgs = require ('Module:Arguments').getArgs;
local override_data = mw.loadData ('Module:Language/data/ISO 639 override');
--local override_data = mw.loadData ('Module:Language/data/ISO 639 override/sandbox');
local deprecated_data = mw.loadData ('Module:Language/data/ISO 639 deprecated');
local parts = {
	{'Module:Language/data/ISO 639-1', '1'},
	{'Module:Language/data/ISO 639-2', '2'},
	{'Module:Language/data/ISO 639-2B', '2B'},
	{'Module:Language/data/ISO 639-3', '3'},
	{'Module:Language/data/ISO 639-5', '5'},
	}


--[[--------------------------< E R R O R _ M E S S A G E S >--------------------------------------------------

]]

local error_messages = {
	['err_msg'] = '<span style="font-size:100%;" class="error show_639_err_msgs">error: $1 ([[Template:ISO 639 name|help]])</span>',
	['err_text'] = {															-- error messages used only in the code to name functions
		['ietf'] = '$1 is an IETF tag',											-- $1 is the ietf tag
		['required'] = 'ISO 639$1 code is required',							-- $1 is the 639 '-1', '-2', '-3', '-5' part suffix; may be empty string

																				-- code to name functions and iso_639_name_to_code()
		['not_found'] = '$1 not found in ISO 639-$2 list',						-- $1 is code or language name; $2 is 639 part suffix(es)

																				-- iso_639_name_to_code() only
		['name'] = 'language name required',
		['not_part'] = '$1 not an ISO 639 part',								-- $1 is invalid 639 suffix (without hyphen)

		['code_name'] = 'language code or name required',						-- iso_639() only
		}
	}

local error_cat = '[[Category:ISO 639 name template errors]]';


--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------

Populates numbered arguments in a message string using an argument table.

]]

local function substitute (msg, args)
	return args and mw.message.newRawMessage (msg, args):plain() or msg;
end


--[[--------------------------< E R R O R _ M S G >------------------------------------------------------------

create an error message

]]

local function error_msg (msg, arg, hide, nocat)
	local retval = '';
	if not hide then
		retval = substitute (error_messages.err_msg, substitute (error_messages.err_text[msg], arg));
		retval = nocat and retval or (retval .. error_cat);
	end
	return retval
end


--[[--------------------------< I S _ S E T >------------------------------------------------------------------

Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.

]]

local function is_set (var)
	return not (var == nil or var == '');
end


--[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------

Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [[L|D]]; if only
link is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns an
empty string.

]=]

local function make_wikilink (link, display)
	if is_set (link) then
		if is_set (display) then
			return table.concat ({'[[', link, '|', display, ']]'});
		else
			return table.concat ({'[[', link, ']]'});
		end
	else
		return '';
	end
end


--[[--------------------------< L A N G _ N A M E _ G E T >----------------------------------------------------

returns first listed language name for code from data{} table; strips parenthetical disambiguation; wikilinks to
the language article if link is true; returns nil else

]]

local function lang_name_get (code, data, link, label, raw)
	local name;
	if data[code] then
--		name = raw and data[code][1] or data[code][1]:gsub ('%s*%b()', '');		-- get the name; strip parenthetical disambiguators if any when <raw> is false
		if raw then
			name = data[code][1];
		else
			name = data[code][1]:gsub ('%s*%b()', '');							-- strip parenthetical disambiguators if any
			name = name:gsub ('([^,]-), +(.+)', '%2 %1');						-- if inverted, uninvert
		end
		if link then															-- make a link to the language article?
			if name:find ('languages') or name:find ('[Ll]anguage$') then
				name = make_wikilink (name, label);								-- simple wikilink for collective languages or langauges ending in 'Language' unless there is a label
			elseif override_data.article_name[code] then
				name = make_wikilink (override_data.article_name[code][1], label or name);	-- language name or label with wikilink from override data
			else
				name = make_wikilink (name .. ' language', label or name);		-- [[name language|name]] or [[name language|label]]
			end
		end
		return name;
	end
end


--[[--------------------------< A D D _ I E T F _ E R R O R _ M S G >------------------------------------------

assembles return-text (language code, language name, or error message) with IETF error message into properly
formatted readable text

|hide-err=yes suppresses error message and category
|cat=no supresses category

]]

local function add_ietf_error_msg (text, ietf_err, hide, nocat)
	if hide then
		ietf_err = '';
	end

	if not nocat then															-- |cat= empty or omitted -> nocat=false
		nocat = '' == ietf_err;													-- spoof; don't add cat when no error
	end

	return table.concat ({														-- tack on ietf error message if one exists
		text,																	-- code name, language name, or error message
		'' ~= ietf_err and ' ' or '',											-- needs a space when ietf_err is not empty
		ietf_err,
		nocat and '' or error_cat,												-- add error category when |cat=<aynthing but 'no'>
		});
end


--[[--------------------------< G E T _ P A R T _ I N D E X >--------------------------------------------------

gets index suitable for parts{} table from ISO 639-<part> (usually args[2])

return valid index [1] - [5]; nil else
	1   <- part ['1']
	2   <- part ['2']															-- this is part 2T
	3   <- part ['2B']
	4   <- part ['3']
	nil <- part ['4']															-- there is no part 4
	5   <- part ['5']

]]

local function get_part_index (part)
	return ({['1']=1, ['2']=2, ['2B']=3, ['3']=4, ['4']=nil, ['5']=5})[part]
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ T O _ N A M E _ C O M M O N >------------------------

this is code that is common to all of the iso_639_code_n_to_name() functions which serve only as template entry
points to provide the frame, the name of the appropriate data source, and to identify which 639 part applies.

this function returns a language name or an error message.  data is searched in this order:
	part-specific override data -> standard part data -> part-specific deprecated data

a second retval used by _iso_639_code_to_name() is true when a code is found; nil else

]]

local function iso_639_code_to_name_common (args, source, part)
	local hide = 'yes' == args['hide-err'];										-- suppress error messages and error categorization
	local nocat = 'no' == args.cat;												-- suppress error categorization (primarily for demo use)
	local raw = 'yes' == args.raw;												-- disable override and dab removal
	local data;																	-- one of the override or part tables
	local name;																	-- holds language name from data

	if not args[1] then															-- if code not provided in the template call
		return error_msg ('required', '-' .. part, hide, nocat);						-- abandon
	end

	local code;																	-- used for error messaging
	local ietf_err;																-- holds an error message when args[1] (language code) is in IETF tag form (may or may not be a valid IETF tag)
	code, ietf_err = args[1]:gsub('(.-)%-.*', '%1');							-- strip ietf subtags; ietf_err is non-zero when subtags are stripped
	ietf_err = (0 ~= ietf_err) and error_msg ('ietf', args[1], hide, nocat) or '';		-- when tags are stripped create an error message; empty string for concatenation else

	if not raw then																-- when raw is true, fetch name as is from part data; ignore override
		data = override_data['override_' .. part];								-- get override data for this part
		name = lang_name_get (code:lower(), data, args.link, args.label, raw);	-- get override language name if there is one
	end
	
	if not name then
		data = mw.loadData (source);											-- get the data for this ISO 639 part
		name = lang_name_get (code:lower(), data, args.link, args.label, raw);	-- get language name if there is one
	end

	if not name then															-- TODO: do something special to indicate when a name is fetched from deprecated data?
		data = deprecated_data['deprecated_' .. part];							-- get deprecated data for this part
		name = lang_name_get (code:lower(), data, args.link, args.label, raw);	-- get deprecated language name if there is one

		if not name then
			return error_msg ('not_found', {code, part}, hide, nocat);			-- code not found, return error message
		end
	end		
	return add_ietf_error_msg (name, ietf_err, hide, nocat), true;				-- return language name with ietf error message if any; true because we found a code
end


--[[--------------------------< _ I S O _ 6 3 9 _ C O D E _ T O _ N A M E >------------------------------------

searches through the ISO 639 language tables for a name that matches the supplied code.  on success returns first
language name that matches code from template frame perhaps with an error message and a second return value of true;
on failure returns an error message and a second return value of nil.  The second return value is a return value
used by iso_639_code_exists()

looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data

]]

local function _iso_639_code_to_name (frame)
	local args = getArgs(frame);
	local hide = 'yes' == args['hide-err'];										-- suppress error messages and error categorization
	local nocat = 'no' == args.cat;												-- suppress error categorization (primarily for demo use)
	
	if not args[1] then															-- if code not provided in the template call
		return error_msg ('required', '', hide, nocat);							-- abandon
	end

	local name;																	-- the retrieved language name and / or error message
	local found;																-- set to true when language name is found

	for _, part in ipairs (parts) do
		name, found = iso_639_code_to_name_common (args, part[1],  part[2]);
		if found then
			return name, true;													-- second retval for iso_639_name_exists()
		end
	end

	return error_msg ('not_found', {args[1], '1, -2, -2B, -3, -5'}, hide, nocat);	-- here when code (args[1]) is not found in the data tables
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ T O _ N A M E >--------------------------------------

template entry point; returns first language name that matches code from template frame or an error message
looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data

]]

local function iso_639_code_to_name (frame)
	local ret_val = _iso_639_code_to_name (frame);								-- ignore second return value
	return ret_val;																-- return language name and / or error message
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ E X I S T S >----------------------------------------

template entry point; returns true if language code maps to a language name; intended as a replacement for:
	{{#exist:Template:ISO 639 name <code>|<exists>|<doesn't exist>}}
Instead of that expensive parser function call use this function:
	{{#if:{{#invoke:ISO 639 name|iso_639_code_exists|<code>}}|<exists>|<doesn't exist>}}
on success, returns true; nil else

]]

local function iso_639_code_exists (frame)
	local _, exists;
	 _, exists = _iso_639_code_to_name (frame);									-- ignore name/error message return; <exists> is true when name found for code; nil else
	 return exists;
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 1 _ T O _ N A M E >----------------------------------

template entry point; returns first language name that matches ISO 639-1 code from template frame or an error message

]]

local function iso_639_code_1_to_name (frame)
	local args = getArgs (frame);
	local retval = iso_639_code_to_name_common (args, parts[1][1],  parts[1][2]);	-- suppress second return value
	return retval;
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 2 _ T O _ N A M E >----------------------------------

template entry point; returns first language name that matches ISO 639-2 code from template frame or an error message

]]

local function iso_639_code_2_to_name (frame)
	local args = getArgs (frame);
	local retval = iso_639_code_to_name_common (args, parts[2][1],  parts[2][2]);	-- suppress second return value
	return retval;
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 2 B _ T O _ N A M E >--------------------------------

template entry point; returns first language name that matches ISO 639-2 code from template frame or an error message

]]

local function iso_639_code_2B_to_name (frame)
	local args = getArgs (frame);
	local retval = iso_639_code_to_name_common (args, parts[3][1],  parts[3][2]);	-- suppress second return value
	return retval;
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 3 _ T O _ N A M E >----------------------------------

template entry point; returns first language name that matches ISO 639-3 code from template frame or an error message

]]

local function iso_639_code_3_to_name (frame)
	local args = getArgs (frame);
	local retval = iso_639_code_to_name_common (args, parts[4][1],  parts[4][2]);	-- suppress second return value
	return retval;
end


--[[--------------------------< I S O _ 6 3 9 _ C O D E _ 5 _ T O _ N A M E >----------------------------------

template entry point; returns first language name that matches ISO 639-5 code from template frame or an error message

]]

local function iso_639_code_5_to_name (frame)
	local args = getArgs (frame);
	local retval = iso_639_code_to_name_common (args, parts[5][1],  parts[5][2]);	-- index [4] -> part 5 because there is no part 4; suppress second return value
	return retval;
end


--[[--------------------------< N A M E _ I N _ P A R T _ C O D E _ G E T >------------------------------------

indexes into the <name_data> using <name> and extracts the language code assigned to <part> (1, 2, 2B, 3, 5).
attempts to index override data first; returns code on success, nil else

]]

local function name_in_part_code_get (name, part, part_idx, name_data)
	return name_data[name] and (
		name_data[name][part_idx+5]	or											-- see if the name exists in the part's override table
		name_data[name][part_idx] or											-- see if the name exists in the part's main table
		name_data[name][part_idx+10]											-- see if the name exists in the part's deprecated table
		);
end


--[[--------------------------< _ I S O _ 6 3 9 _ N A M E _ T O _ C O D E >------------------------------------

module entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part
(1, 2, 2B, 3, 5) argument; when part is not provided scans 1, 2, 2B, 3, 5 and returns first code

override data are examined first

<args> is frame arguments from getArgs(frame)

]]

local function _iso_639_name_to_code (args)
	local hide = 'yes' == args['hide-err'];										-- suppress error messages and error categorization
	local nocat = 'no' == args.cat;												-- suppress error categorization (primarily for demo use)

	if not args[1] then
		return error_msg ('name', '', hide, nocat);								-- abandon when language name missing
	end
	
	local name = args[1];														-- used in error messaging
	local lc_name = name:gsub(' +', ' '):lower();								-- lowercase version of name for indexing into the data table; strip extraneous space characters

	local part_idx;
	local part = args[2];
	if part then
		part_idx = get_part_index (part);
		if not part_idx then
			return error_msg ('not_part', part, hide, nocat);					-- abandon; args[2] is not a valid ISO 639 part
		end
	end

	local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code');	-- ISO 639 language names to code table
--	local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code/sandbox');	-- ISO 639 language names to code table

	local code;
	
	if part then
		code = name_in_part_code_get (lc_name, part, part_idx, name_data);		-- search the specified override table + part table
	else
		for part_idx, part_tag in ipairs ({'1', '2', '2B', '3', '5'}) do		-- no part provided, spin through all parts override first and get the first available code
			code = name_in_part_code_get (lc_name, part_tag, part_idx, name_data);
			if code then														-- nil when specified <part> does not have code for specified language <name>
				break;															-- when code is not nil, done
			end
		end
	end
	
	if code then
		return code, true;
	end
	return error_msg ('not_found', {name, part or '1, -2, -2B, -3, -5'}, hide, nocat), false;
end


--[[--------------------------< I S O _ 6 3 9 _ N A M E _ T O _ C O D E >--------------------------------------

template entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part
(1, 2, 2B, 3, 5) argument; when part is not provided scans 1, 2, 2B, 3, 5 and returns first code

override data are examined first

args[1] is language name
args[2] is ISO 639 part

]]

local function iso_639_name_to_code (frame)
	local args = getArgs(frame);
	local result, _ = _iso_639_name_to_code (args);								-- suppress true/false return used by iso_639_name_exists()
	return result;
end


--[[--------------------------< I S O _ 6 3 9 _ N A M E _ E X I S T S >----------------------------------------

template entry point; returns ISO 639-1, -2, -3, or -5 code associated with language name according to part (1, 2, 3, 5) argument;
when part is not provided scans 1, 2, 3 , 5 and returns first code

override data are examined first

args[1] is language name
args[2] is ISO 639 part

]]

local function iso_639_name_exists (frame)
	local args = getArgs(frame);
	local _, result = _iso_639_name_to_code (args);								-- suppress code return used by iso_639_name_to_code()
	return result and true or nil;
end


--[[--------------------------< I S O _ 6 3 9 >----------------------------------------------------------------

template entry point.
returns:
	language name if args[1] is valid language code
	language code if args[1] is valid language name

this function is constrained to the ISO 639 part specified in args[2] which must be 1, 2, 2B, 3, or 5.  When not provided
all parts are tested. The first match is found

]]

local function iso_639 (frame)
	local args = getArgs (frame);
	local hide = 'yes' == args['hide-err'];										-- suppress error messages and error categorization
	local nocat = 'no' == args.cat;												-- suppress error categorization (primarily for demo use)
	local result;
	local found;																-- set to true when language name is found

	if not args[1] then
		return error_msg ('code_name', '', hide, nocat);
	end

	local part = args[2];
	if part then																-- if ISO 639 part supplied
		local part_idx = get_part_index (part);									-- map index from <part>; anything else nil

		if not part_idx then
			return error_msg ('not_part', part, hide, nocat);					-- abandon; args[2] is not a valid ISO 639 part
		end

		result, found = iso_639_code_to_name_common (args, parts[part_idx][1], parts[part_idx][2]);		-- attempt to find a code match
		if found then
			return result;														-- found the code so return the language name
		end

		result = _iso_639_name_to_code (args);									-- might be a language name; return code if it is; error message or empty string else
		return result;															-- this way to suppress second return

	else		
		for _, part in ipairs (parts) do										-- for each of the iso 639 parts
			result, found = iso_639_code_to_name_common (args, part[1], part[2]);	-- attempt to find a code match
			if found then
				return result;													-- found the code so return the language name
			end
		end
	end	

	result = _iso_639_name_to_code (args);										-- might be a language name; return code if it is; error message or empty string else
	return result;																-- this way to suppress second return
end


--[[--------------------------< E X P O R T E D   F U N C T I O N S >------------------------------------------
]]

return {
	iso_639 = iso_639,															-- returns code when given name; returns name when given code

	iso_639_code_exists = iso_639_code_exists,
	iso_639_name_exists = iso_639_name_exists,

	iso_639_code_to_name = iso_639_code_to_name,
	iso_639_code_1_to_name = iso_639_code_1_to_name,
	iso_639_code_2_to_name = iso_639_code_2_to_name,
	iso_639_code_2B_to_name = iso_639_code_2B_to_name,
	iso_639_code_3_to_name = iso_639_code_3_to_name,
	iso_639_code_5_to_name = iso_639_code_5_to_name,

	iso_639_name_to_code = iso_639_name_to_code,
	};