Module:ISO 639/sandbox

local getArgs = require ('Module:Arguments').getArgs; local p = {}

--[[--< 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'] = 'error: $1 ', ['err_msg'] = ' Error: $1 ', ['err_text'] = { ['required'] = ' ISO 639 code or name is required', ['no_code'] = ' no code in ISO 639-$1 for $2', ['not_found'] = ' $1 not found', ['ietf'] = ' $1 is an IETF tag', }	}

--[[--< 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

--[[--< N A M E >---

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

]]

function p.name (frame) local args = getArgs(frame); if not args[1] or 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; code, ietf = code:gsub('(.-)%-.*', '%1');									-- strip ietf subtags; count is non-zero when subtags are stripped lc_code = code:lower; if 0 ~= ietf then ietf = error_msg ('ietf', args[1]); else ietf = ''; end local name_data = mw.loadData ('Module:Language/data/ISO 639 name to code'); local part1_data = mw.loadData ('Module:Language/data/ISO 639-1'); local part2_data = mw.loadData ('Module:Language/data/ISO 639-2'); local part3_data = mw.loadData ('Module:Language/data/ISO 639-3'); local part5_data = mw.loadData ('Module:Language/data/ISO 639-5');

if part1_data[lc_code] then                                            	-- If input is part1 return table.concat ({part1_data[lc_code][1],ietf}); elseif part2_data[lc_code] then                                            -- If input is part2 return table.concat ({part2_data[lc_code][1],ietf}); elseif part3_data[lc_code] then                                            -- If input is part3 return table.concat ({part3_data[lc_code][1],ietf}); elseif part5_data[lc_code] then                                            -- If input is part5 return table.concat ({part5_data[lc_code][1],ietf}); elseif name_data[lc_code] then                                             -- If input is name lc_code = name_data[lc_code] if part3_data[lc_code[3]][1] then return table.concat ({part3_data[lc_code[3]][1],ietf}); else return table.concat ({part5_data[lc_code[4]][1],ietf}); end end return error_msg ('not_found', code); end

----< R E T U R N C O D E >-----

local function return_code (frame,code_type) local data = mw.loadData ('Module:Language/data/ISO 639 name to code'); local args = frame["args"] if not args[1] then return error_msg ('required');                                         -- if no input return error end args[1] = args[1]:lower if not data[args[1]] then                                                  -- if input is not in data table args[1] = p.name(frame)                                                -- set to name using p.name args[1] = args[1]:lower end if data[args[1]][code_type] ~= "" then if code_type == 5 then code_type = 4 end return data[args[1]][code_type];                                       -- return code else return error_msg ('no_code', {code_type,args[1]});                     -- or return error end end

----< C O D E 1 >---

function p.part1 (frame) return return_code(frame,1) end

----< C O D E 2 >---

function p.part2 (frame) return return_code(frame,2) end

----< C O D E 3 >---

function p.part3 (frame) return return_code(frame,3) end

----< C O D E 5 >---

function p.part5 (frame) return return_code(frame,5) end

----< E X P O R T E D >-----

return p;