Module:ISO 639 name
![]() | This Lua module is used on approximately 20,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
Module:ISO 639 name implements:
{{ISO 639 name}}
{{Lang2iso}}
Data mapping
For code-to-name mapping, this module uses data from:
- Module:ISO 639 name/ISO 639-1 – from IANA language-subtag-registry file; this data used only for ISO 639-1 language codes / names listed there
- Module:ISO 639 name/ISO 639-2 – from Codes for the Representation of Names of Languages
- Module:ISO 639 name/ISO 639-2B – from Codes for the Representation of Names of Languages
- Module:ISO 639 name/ISO 639-3 – from sil.org UTF-8 Complete Code Tables Set
- Module:ISO 639 name/ISO 639-5 – from Codes for the Representation of Names of Languages Part 5
- Module:ISO 639 name/ISO 639 deprecated – from ISO 639-2/RA Change Notice and ISO 639-5 Change Notice includes:
- Module:ISO 639 name/ISO 639-3 (dep) – from sil.org UTF-8 Complete Code Tables Set (Retirements)
- Module:ISO 639 name/ISO 639 override – code to name mapping where the name is different from the name in the ISO 639 standard
For name-to-code mapping, this module uses data from:
- Module:ISO 639 name/ISO 639 name to code – names and codes extracted from the code-to-name data set above
Testcases
- Module:ISO 639 name/testcases (run)
- code_to_name()
- Module:ISO 639 name/testcases/ISO 639-1 name from code (run)
- Module:ISO 639 name/testcases/ISO 639-2 name from code (run)
- Module:ISO 639 name/testcases/ISO 639-3-1 name from code (A–H) (run)
- Module:ISO 639 name/testcases/ISO 639-3-2 name from code (I–N) (run)
- Module:ISO 639 name/testcases/ISO 639-3-3 name from code (O–Z) (run)
- Module:ISO 639 name/testcases/ISO 639-5 name from code (run)
- Module:ISO 639 name/testcases/ISO 639 deprecated name from code (run)
- name_to_code()
- Module:ISO 639 name/testcases/ISO 639-1 code from name (run)
- Module:ISO 639 name/testcases/ISO 639-2 code from name (run)
- Module:ISO 639 name/testcases/ISO 639-3-1 code from name (A–H) (run)
- Module:ISO 639 name/testcases/ISO 639-3-2 code from name (I–N) (run)
- Module:ISO 639 name/testcases/ISO 639-3-3 code from name (O–Z) (run)
- Module:ISO 639 name/testcases/ISO 639-5 code from name (run)
- Module:ISO 639 name/testcases/ISO 639 deprecated code from name (run)
Tracking categories
iso_639_code_exists
Returns true
when <code>
is found in one of the ISO 639-1, -2, -3, -5, or override tables; returns nil else.
Useage:
{{#invoke:ISO 639 name|iso_639_code_exists|<code>}}
Examples:
- without required
<code>
:{{#invoke:ISO 639 name|iso_639_code_exists}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_exists|nav}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_exists|xx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_to_name
Returns language name associated with <code>
. May return an error message with or without a language name.
Optional parameters:
|hide-err=yes
: suppresses error messages|link=yes
: creates link to en.wiki article about the language|label=<label text>
– requires|link=yes
; label for the language-name link:[[<language name> language|<label text>]]
|raw=yes
: queries part tables only; returns language name as defined in the base data tables without the removal of disambiguation information
Examples:
- without required
<code>
:{{#invoke:ISO 639 name|iso_639_code_to_name}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_to_name|nav}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_to_name|xx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
(IETF language tag):{{#invoke:ISO 639 name|iso_639_code_to_name|nv-Latn}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
, error message suppressed:{{#invoke:ISO 639 name|iso_639_code_to_name|nv-Latn|hide-err=yes}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- linked:
{{#invoke:ISO 639 name|iso_639_code_to_name|nav|link=yes}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- linked with alternate spelling:
{{#invoke:ISO 639 name|iso_639_code_to_name|nav|link=yes|label=Navaho}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- ISO 639-1 code
el
is normally overridden by a setting in Module:ISO 639 name/ISO_639_override:{{#invoke:ISO 639 name|iso_639_code_to_name|el}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- ISO 639-1 code
el
with override suppressed:{{#invoke:ISO 639 name|iso_639_code_to_name|el|raw=yes}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- ISO 639-3 code
ina
name has disambiguation that is normally suppressed:{{#invoke:ISO 639 name|iso_639_code_to_name|ina}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- ISO 639-3 code
ina
with disambiguation:{{#invoke:ISO 639 name|iso_639_code_to_name|ina|raw=yes}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_1_to_name
Same as iso_639_code_to_name
except that <code>
must be an ISO 639-1 code; search is constrained to Module:ISO 639 name/ISO 639 override and Module:ISO 639 name/ISO 639-1.
Example:
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_1_to_name|nv}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_1_to_name|xx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_2_to_name
Same as iso_639_code_to_name
except that <code>
must be an ISO 639-2 code; search is constrained to Module:ISO 639 name/ISO 639 override and Module:ISO 639 name/ISO 639-2.
Example:
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_2_to_name|apa}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_2_to_name|xxx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_2B_to_name
Same as iso_639_code_to_name
except that <code>
must be an ISO 639-2B code; search is constrained to Module:ISO 639 name/ISO 639 override and Module:ISO 639 name/ISO 639-2B.
Example:
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_2B_to_name|ice}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_2B_to_name|xxx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_3_to_name
Same as iso_639_code_to_name
except that <code>
must be an ISO 639-3 code; search is constrained to Module:ISO 639 name/ISO 639 override and Module:ISO 639 name/ISO 639-3.
Example:
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_3_to_name|nav}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_3_to_name|xxx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_code_5_to_name
Same as iso_639_code_to_name
except that <code>
must be an ISO 639-5 code; search is constrained to Module:ISO 639 name/ISO 639 override and Module:ISO 639 name/ISO 639-5.
Example:
- with valid
<code>
:{{#invoke:ISO 639 name|iso_639_code_5_to_name|apa}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<code>
:{{#invoke:ISO 639 name|iso_639_code_5_to_name|xxx}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_name_exists
Returns true
when <language name>
is found in one of the ISO 639-1, -2, -3, -5, or override tables; returns nil else.
Useage:
{{#invoke:ISO 639 name|iso_639_name_exists|<language name>}}
Examples:
- without required
<language name>
:{{#invoke:ISO 639 name|iso_639_name_exists}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with valid
<language name>
:{{#invoke:ISO 639 name|iso_639_name_exists|french}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with invalid
<language name>
:{{#invoke:ISO 639 name|iso_639_name_exists|fr}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639_name_to_code
Returns ISO 639 language code associated with <language name>
. May return an error message with or without a language name.
This function may not be particularly useful because, except for case, <language name>
must exactly match the name in the data tables.
Optional parameters:
<part>
(second positional parameter) – limits return to codes from the specified<part>
(1
,2
,3
,5
), override table first then the appropriate data table; when not supplied, this function searches in order part 1 to part 5. The search stops when a match is found.|hide-err=yes
: suppresses error messages
Examples:
- without
<part>
, with ISO 639-5 name:{{#invoke:ISO 639 name|iso_639_name_to_code|North Germanic languages}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with
<part>
:{{#invoke:ISO 639 name|iso_639_name_to_code|Navajo|2}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
iso_639
When given an ISO 639 language code, return the associated language name; when given a language name, returns the associated ISO 639 language code. May return an error message with or without a language code/name.
This function may not be particularly useful because, except for case, a provided <language name>
must exactly match the name in the data tables.
Optional parameters:
<part>
(second positional parameter) – limits return to codes/names from the specified<part>
(1
,2
,3
,5
), override table first then the appropriate data table; when not supplied, this function searches in order part 1 to part 5. The search stops when a match is found.|hide-err=yes
: suppresses error messages
Examples:
- without
<part>
, with ISO 639-5 code:{{#invoke:ISO 639 name|iso_639|gmq}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- without
<part>
, with ISO 639-5 name:{{#invoke:ISO 639 name|iso_639|North Germanic languages}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with
<part>
and<code>
:{{#invoke:ISO 639 name|iso_639|nav|2}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
- with
<part>
and<name>
:{{#invoke:ISO 639 name|iso_639|Navaho|2}}
→ Lua error in package.lua at line 80: module 'Module:No globals' not found.
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;
--[[--------------------------< 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 = {
-- ['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</span>[[Category:ISO 639 name template errors]]',
['err_text'] = {
['required'] = 'ISO 639 code is required',
['name'] = 'language name required',
['not_code'] = '$1 is not an ISO 639 code',
['not_code1'] = '$1 is not an ISO 639-1 code',
['not_code2'] = '$1 is not an ISO 639-2 code',
['not_code3'] = '$1 is not an ISO 639-3 code',
['not_code5'] = '$1 is not an ISO 639-5 code',
['not_found'] = '$1 not found in ISO 639-$2 list',
['nocode'] = 'no code in ISO 639-$1 for $2',
['ietf'] = '$1 is an IETF tag',
}
}
--[[--------------------------< 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
--[[--------------------------< 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
--[[--------------------------< 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)
local name;
if data[code] then
name = data[code][1]:gsub ('%s*%b()', ''); -- get the name and strip parenthetical disambiguators if any
if link then -- make a link to the language article?
if name:find ('languages') then
name = make_wikilink (name); -- simple wikilink for collective languages
else
name = make_wikilink (name .. ' language', name); -- [[name language|name]]
end
end
return name;
end
end
--[[--------------------------< I S O _ 6 3 9 _ N A M E >------------------------------------------------------
template entry point; returns first language name that matches code from template frame or an error message
]]
local function iso_639_name (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('required');
end
local code = args[1]; -- used in 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 an IETF tag
local name; -- the retrieved language name
local data = {}; -- holds one of the various 639 code to name tables
code, ietf_err = code:gsub('(.-)%-.*', '%1'); -- strip ietf subtags; count is non-zero when subtags are stripped
lc_code = code:lower();
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
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
end
data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name/ISO 639 override'); -- first look in the override table
name = lang_name_get (lc_code, data, 'yes' == args.link);
if name then
return table.concat ({name, ' ', ietf_err});
end
if 2 == #lc_code then
data = mw.loadData ('Module:Language/data/iana languages'); -- used only for ISO 639-1 language codes / names
name = lang_name_get (lc_code, data, 'yes' == args.link);
if name then
return table.concat ({name, ' ', ietf_err});
end
else
for _, source in ipairs ({
'Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-2',
'Module:Language/data/ISO 639-3',
'Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-5'
}) do
data = mw.loadData (source);
name = lang_name_get (lc_code, data, 'yes' == args.link);
if name then
return table.concat ({name, ' ', ietf_err});
end
end
end
return error_msg ('not_found', {code, '1, -2, -3, -5'}); -- here when code is not found in the data tables
end
--[[--------------------------< I S O _ 6 3 9 _ C O D E _ C O M M O N >----------------------------------------
]]
local function iso_639_code_common (code, source, link, part)
local lc_code = code:lower(); -- holds lowercase version of code for indexing into the data tables
if (1 == part and 2 ~= #lc_code) or (1 < part and 3 ~= #lc_code) then -- 639-1 codes are 2 characters only; all others 3 characters
return error_msg ('not_code' .. part, code);
end
local data = mw.loadData (source); -- get the data
return lang_name_get (code, data, link); -- returns name or nil
-- return data[lc_code] and data[lc_code][1] or error_msg ('not_found', {code, part});
end
--[[--------------------------< I S O _ 6 3 9 _ 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
]]
local function iso_639_code_1 (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('required');
end
return iso_639_code_common (args[1], 'Module:Language/data/iana languages', 'yes' == args.link, 1) or error_msg ('not_found', {code, 1});
end
--[[--------------------------< I S O _ 6 3 9 _ 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
]]
local function iso_639_code_2 (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('required');
end
-- ISO 639-2 language codes / names; to be moved to Module:Language/data/ISO 639-2
return iso_639_code_common (args[1], 'Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-2', 'yes' == args.link, 2) or error_msg ('not_found', {args[1], 2});
end
--[[--------------------------< I S O _ 6 3 9 _ 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
]]
local function iso_639_code_3 (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('required');
end
return iso_639_code_common (args[1], 'Module:Language/data/ISO 639-3', 'yes' == args.link, 3) or error_msg ('not_found', {args[1], 3});
end
--[[--------------------------< I S O _ 6 3 9 _ 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
]]
local function iso_639_code_5 (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('required');
end
-- ISO 639-5 language codes / names; to be moved to Module:Language/data/ISO 639-5
return iso_639_code_common (args[1], 'Module:Sandbox/trappist the monk/ISO 639 name/ISO 639-5', 'yes' == args.link, 5) or error_msg ('not_found', {args[1], 5});
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, -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
]]
local function iso_639_name_to_code (frame)
local args = getArgs(frame);
if not args[1] then
return error_msg ('name');
end
local name = args[1]; -- used in error messaging
local lc_name = name:lower(); -- holds lowercase version of code for indexing into the data table
local part = tonumber(args[2]);
local name_data = mw.loadData ('Module:Sandbox/trappist the monk/ISO 639 name to code'); -- ISO 639 language names to code table; to be moved to Module:Language/data/ISO 639 name to code
if name_data[lc_name] then
if part then
if '' ~= name_data[lc_name][part] then
return name_data[lc_name][part];
else
return error_msg ('nocode', {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 list of get the first available code
if '' ~= name_data[lc_name][v] then
return name_data[lc_name][v];
end
end
end
else
return error_msg ('not_found', {name, '1, -2, -3, -5'});
end
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
iso_639_name = iso_639_name,
iso_639_code_1 = iso_639_code_1,
iso_639_code_2 = iso_639_code_2,
iso_639_code_3 = iso_639_code_3,
iso_639_code_5 = iso_639_code_5,
iso_639_name_to_code = iso_639_name_to_code,
};