Module:ISO 639 name and Module:ISO 639 name/sandbox: Difference between pages
Appearance
(Difference between pages)
Content deleted Content added
use require('strict') instead of require('Module:No globals') |
use require('strict') instead of require('Module:No globals') |
||
Line 2: | Line 2: | ||
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 deprecated_data = mw.loadData ('Module:Language/data/ISO 639 deprecated'); |
||
local parts = { |
local parts = { |
||
Line 24: | Line 24: | ||
['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 |
||
-- code to name functions and |
-- code to name functions and code_from_name() |
||
['not_found'] = '$1 not found in ISO 639-$2 list', -- $1 is code or language name; $2 is 639 part suffix(es) |
['not_found'] = '$1 not found in ISO 639-$2 list', -- $1 is code or language name; $2 is 639 part suffix(es) |
||
-- |
-- code_from_name() only |
||
['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) |
||
Line 76: | Line 76: | ||
--[=[-------------------------< |
--[=[-------------------------< W I K I L I N K _ M A K E >---------------------------------------------------- |
||
Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [[L|D]]; if only |
Makes a wikilink; when both link and display text is provided, returns a wikilink in the form [[L|D]]; if only |
||
Line 84: | Line 84: | ||
]=] |
]=] |
||
local function |
local function wikilink_make (link, display) |
||
if is_set (link) then |
if is_set (link) then |
||
if is_set (display) then |
if is_set (display) then |
||
Line 116: | Line 116: | ||
if link then -- make a link to the language article? |
if link then -- make a link to the language article? |
||
if name:find ('languages') or name:find ('[Ll]anguage$') then |
if name:find ('languages') or name:find ('[Ll]anguage$') then |
||
name = |
name = wikilink_make (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 = |
name = wikilink_make (override_data.article_name[code][1], label or name); -- language name or label with wikilink from override data |
||
else |
else |
||
name = |
name = wikilink_make (name .. ' language', label or name); -- [[name language|name]] or [[name language|label]] |
||
end |
end |
||
end |
end |
||
Line 175: | Line 175: | ||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D 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 |
this is code that is common to all of the iso_639_code_n_to_name() functions which serve only as template entry |
||
Line 183: | Line 183: | ||
part-specific override data -> standard part data -> part-specific deprecated data |
part-specific override data -> standard part data -> part-specific deprecated data |
||
a second retval used by |
a second retval used by _name_from_code() is true when a code is found; nil else |
||
]] |
]] |
||
local function |
local function name_from_code_common (args, source, part) |
||
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization |
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 nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use) |
||
Line 195: | Line 195: | ||
if not args[1] then -- if code not provided in the template call |
if not args[1] then -- if code not provided in the template call |
||
return error_msg ('required', '-' .. part, hide, nocat); |
return error_msg ('required', '-' .. part, hide, nocat); -- abandon |
||
end |
end |
||
Line 225: | Line 225: | ||
--[[--------------------------< _ |
--[[--------------------------< _ N A M E _ F R O M _ C O D E >------------------------------------------------ |
||
searches through the ISO 639 language tables for a name that matches the supplied code. on success returns first |
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; |
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 |
on failure returns an error message and a second return value of nil. The second return value is a return value |
||
used by |
used by is_code() |
||
looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data |
looks first in the override data and then sequentially in the 639-1, -2, -3, and -5 data |
||
Line 236: | Line 236: | ||
]] |
]] |
||
local function |
local function _name_from_code (frame) |
||
local args = getArgs(frame); |
local args = getArgs(frame); |
||
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization |
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization |
||
Line 249: | Line 249: | ||
for _, part in ipairs (parts) do |
for _, part in ipairs (parts) do |
||
name, found = |
name, found = name_from_code_common (args, part[1], part[2]); |
||
if found then |
if found then |
||
return name, true; -- second retval for |
return name, true; -- second retval for is_name() |
||
end |
end |
||
end |
end |
||
Line 259: | Line 259: | ||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D 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 |
||
Line 266: | Line 266: | ||
]] |
]] |
||
local function |
local function name_from_code (frame) |
||
local ret_val = |
local ret_val = _name_from_code (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 |
--[[--------------------------< I S _ C O D E >---------------------------------------------------------------- |
||
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 639 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:ISO 639 name| |
{{#if:{{#invoke:ISO 639 name|is_code|<code>}}|<exists>|<doesn't exist>}} |
||
on success, returns true; nil else |
on success, returns true; nil else |
||
]] |
]] |
||
local function |
local function is_code (frame) |
||
local _, exists; |
local _, exists; |
||
_, exists = |
_, exists = _name_from_code (frame); -- ignore name/error message return; <exists> is true when name found for code; nil else |
||
return exists; |
return exists; |
||
end |
end |
||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D E _ 1 >---------------------------------------------- |
||
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-1 code from template frame or an error message |
||
Line 295: | Line 295: | ||
]] |
]] |
||
local function |
local function name_from_code_1 (frame) |
||
local args = getArgs (frame); |
local args = getArgs (frame); |
||
local retval = |
local retval = name_from_code_common (args, parts[1][1], parts[1][2]); -- suppress second return value |
||
return retval; |
return retval; |
||
end |
end |
||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D E _ 2 >---------------------------------------------- |
||
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-2 code from template frame or an error message |
||
Line 308: | Line 308: | ||
]] |
]] |
||
local function |
local function name_from_code_2 (frame) |
||
local args = getArgs (frame); |
local args = getArgs (frame); |
||
local retval = |
local retval = name_from_code_common (args, parts[2][1], parts[2][2]); -- suppress second return value |
||
return retval; |
return retval; |
||
end |
end |
||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D E _ 2 B >-------------------------------------------- |
||
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-2 code from template frame or an error message |
||
Line 321: | Line 321: | ||
]] |
]] |
||
local function |
local function name_from_code_2B (frame) |
||
local args = getArgs (frame); |
local args = getArgs (frame); |
||
local retval = |
local retval = name_from_code_common (args, parts[3][1], parts[3][2]); -- suppress second return value |
||
return retval; |
return retval; |
||
end |
end |
||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D E _ 3 >---------------------------------------------- |
||
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-3 code from template frame or an error message |
||
Line 334: | Line 334: | ||
]] |
]] |
||
local function |
local function name_from_code_3 (frame) |
||
local args = getArgs (frame); |
local args = getArgs (frame); |
||
local retval = |
local retval = name_from_code_common (args, parts[4][1], parts[4][2]); -- suppress second return value |
||
return retval; |
return retval; |
||
end |
end |
||
--[[--------------------------< |
--[[--------------------------< N A M E _ F R O M _ C O D E _ 5 >---------------------------------------------- |
||
template entry point; returns first language name that matches ISO 639-5 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 |
||
Line 347: | Line 347: | ||
]] |
]] |
||
local function |
local function name_from_code_5 (frame) |
||
local args = getArgs (frame); |
local args = getArgs (frame); |
||
local retval = |
local retval = name_from_code_common (args, parts[5][1], parts[5][2]); -- index [4] -> part 5 because there is no part 4; suppress second return value |
||
return retval; |
return retval; |
||
end |
end |
||
Line 370: | Line 370: | ||
--[[--------------------------< _ |
--[[--------------------------< _ C O D E _ F R O M _ N A M E >------------------------------------------------ |
||
module entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part |
module entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part |
||
Line 381: | Line 381: | ||
]] |
]] |
||
local function |
local function _code_from_name (args) |
||
local hide = 'yes' == args['hide-err']; -- suppress error messages and error categorization |
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 nocat = 'no' == args.cat; -- suppress error categorization (primarily for demo use) |
||
Line 390: | Line 390: | ||
local name = args[1]; -- used in error messaging |
local name = args[1]; -- used in error messaging |
||
local lc_name = name:gsub(' +', ' ' |
local lc_name = name:gsub(' +', ' '); -- strip extraneous space characters |
||
lc_name = mw.ustring.lower (lc_name); -- lowercase version of name for indexing into the data table |
|||
local part_idx; |
local part_idx; |
||
Line 401: | Line 402: | ||
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; |
local code; |
||
Line 424: | Line 425: | ||
--[[--------------------------< |
--[[--------------------------< C O D E _ F R O M _ N A M E >-------------------------------------------------- |
||
template entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part |
template entry point; returns ISO 639-1, -2, -2B, -3, or -5 code associated with language name according to part |
||
Line 436: | Line 437: | ||
]] |
]] |
||
local function |
local function code_from_name (frame) |
||
local args = getArgs(frame); |
local args = getArgs(frame); |
||
local result, _ = |
local result, _ = _code_from_name (args); -- suppress true/false return used by is_name() |
||
return result; |
return result; |
||
end |
end |
||
--[[--------------------------< I S |
--[[--------------------------< I S _ N A M 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; |
template entry point; returns ISO 639-1, -2, -3, or -5 code associated with language name according to part (1, 2, 3, 5) argument; |
||
Line 455: | Line 456: | ||
]] |
]] |
||
local function |
local function is_name (frame) |
||
local args = getArgs(frame); |
local args = getArgs(frame); |
||
local _, result = |
local _, result = _code_from_name (args); -- suppress code return used by code_from_name() |
||
return result and true or nil; |
return result and true or nil; |
||
end |
end |
||
Line 493: | Line 494: | ||
end |
end |
||
result, found = |
result, found = name_from_code_common (args, parts[part_idx][1], parts[part_idx][2]); -- attempt to find a code match |
||
if found then |
if found then |
||
return result; -- found the code so return the language name |
return result; -- found the code so return the language name |
||
end |
end |
||
result = |
result = _code_from_name (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 |
return result; -- this way to suppress second return |
||
else |
else |
||
for _, part in ipairs (parts) do -- for each of the iso 639 parts |
for _, part in ipairs (parts) do -- for each of the iso 639 parts |
||
result, found = |
result, found = name_from_code_common (args, part[1], part[2]); -- attempt to find a code match |
||
if found then |
if found then |
||
return result; -- found the code so return the language name |
return result; -- found the code so return the language name |
||
Line 510: | Line 511: | ||
end |
end |
||
result = |
result = _code_from_name (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 |
return result; -- this way to suppress second return |
||
end |
end |
||
Line 521: | Line 522: | ||
iso_639 = iso_639, -- returns code when given name; returns name when given code |
iso_639 = iso_639, -- returns code when given name; returns name when given code |
||
is_code = is_code, |
|||
iso_639_code_exists = iso_639_code_exists, |
|||
is_name = is_name, |
|||
iso_639_name_exists = iso_639_name_exists, |
|||
name_from_code = name_from_code, |
|||
iso_639_code_to_name = iso_639_code_to_name, |
|||
name_from_code_1 = name_from_code_1, |
|||
iso_639_code_1_to_name = iso_639_code_1_to_name, |
|||
name_from_code_2 = name_from_code_2, |
|||
iso_639_code_2_to_name = iso_639_code_2_to_name, |
|||
name_from_code_2B = name_from_code_2B, |
|||
iso_639_code_2B_to_name = iso_639_code_2B_to_name, |
|||
name_from_code_3 = name_from_code_3, |
|||
iso_639_code_3_to_name = iso_639_code_3_to_name, |
|||
name_from_code_5 = name_from_code_5, |
|||
iso_639_code_5_to_name = iso_639_code_5_to_name, |
|||
code_from_name = code_from_name, |
|||
iso_639_name_to_code = iso_639_name_to_code, |
|||
iso_639_code_exists = is_code, -- deprecated |
|||
iso_639_name_exists = is_name, -- deprecated |
|||
iso_639_code_to_name = name_from_code, -- deprecated |
|||
iso_639_code_1_to_name = name_from_code_1, -- deprecated |
|||
iso_639_code_2_to_name = name_from_code_2, -- deprecated |
|||
iso_639_code_2B_to_name = name_from_code_2B, -- deprecated |
|||
iso_639_code_3_to_name = name_from_code_3, -- deprecated |
|||
iso_639_code_5_to_name = name_from_code_5, -- deprecated |
|||
iso_639_name_to_code = code_from_name, -- deprecated |
|||
}; |
}; |