require('strict');local getArgs = require('Module:Arguments').getArgs
----------------------------< E R R _ M S G _ M A K E >------------------------------------------------------
local function err_msg_make (msg) return '
error: ' .. msg .. ' not recognized'end----------------------------< _ I S _ C O D E >--------------------------------------------------------------
local or required entry point
return true if is a mediawiki recognized code; false else
<code> - language code to validate; expected to be lowercase without leading/trailing whitespace
<target_lang_code> - language code for target language; expected to be valid; expected to be lowercase without leading/trailing whitespace
specifying <target_lang_code> may be a pointless exercise because of cldr fallback. For example,
mw.language.fetchLanguageName (crh, sq) -> Crimean Turkish
because the Albanian language definitions do not have an Albanian-language version of the language name
local function _is_code (code, target_lang_code) code = mw.language.fetchLanguageName (code, target_lang_code); return ~= code;end
----------------------------< _ _ V A L I D A T E _ T A R G E T _ L A N G _ C O D E >------------------------
validates target_lang_code as a know language code; returns two values when target_lang_code is valid, first return value holds target_lang_code; second return value is nil when target_lang_code is invalid, first retrun value is nil; second return value has error message
if target_lang_code argument is nil, (missing or empty in the invoke) use local wiki's language code
local function __validate_target_lang_code (target_lang_code) local msg; if target_lang_code then -- not missing or empty if not _is_code (target_lang_code) then -- validate target_lang_code msg = err_msg_make ('target language code: ' .. target_lang_code); target_lang_code = nil; -- unset as invalid end end if not target_lang_code then -- if nil because missing or empty or because invlaid and we set it nil target_lang_code = mw.getContentLanguage:getCode; -- use local wiki's language code end
return target_lang_code, msg; -- target_lang_code is valid or nil; msg is nil or has an error messageend
-- return true if is a mediawiki recognized code; nil else
local function is_code (frame) local args = getArgs (frame,); local code = args[1]; local target_lang_code = __validate_target_lang_code (args[2]);
return code and _is_code (code, target_lang_code) and true or nil;end
-- return language-name if language-code is a mediawiki recognized code; error message string else returned language name not guarenteed to be in target_lang_code (if specified), because mw language lists are incomplete
local function name_from_code (frame) local args = getArgs (frame,); local code = args[1]; if not code then return err_msg_make ('code: (empty)'); end local target_lang_code, msg = __validate_target_lang_code (args[2]); if msg then return msg; end
local name = mw.language.fetchLanguageName (code, target_lang_code); -- returns empty string if code not found return ~= name and name or err_msg_make ('language code: ' .. code); -- return language name or error messageend
--
local function _code_from_name (args) local name = args[1]; if not name then return err_msg_make ('name: (empty)'); end
local target_lang_code, msg = __validate_target_lang_code (args[2]);
if msg then return msg; end
local code_name_list = mw.language.fetchLanguageNames (target_lang_code, 'all'); -- get language code / name list in target_lang_code language indexed by language code local name_code_list = ; -- to hold language name / code list indexed by name
for k, v in pairs (code_name_list) do -- spin through the code / name list and name_code_list[v:lower] = k; -- make a name / code list end
if name_code_list[name] then return name_code_list[name], true; -- returns code when name is found and true for is_name else return err_msg_make ('language name: ' .. name), false; -- return error message when name not found and false for is_name endend
--
local function code_from_name (frame) local args = getArgs (frame,); local result, _ = _code_from_name (args); -- suppress true/false return used by is_name return result;end
--
local function is_name (frame) local args = getArgs (frame,); local _, result = _code_from_name (args); -- suppress code return used by code_from_name return result and true or nil;end
----------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
return