モジュール:Mw lang

require('strict');
local getArgs = require('Module:Arguments').getArgs
----------------------------------------------------------------------------------
local function err_msg_make (msg)
	return 'error: ' .. msg .. ' not recognized'
end
------------------------------------------------------------------------------------------
local or require()d entry point
return true if  is a mediawiki recognized code; false else
- language code to validate; expected to be lowercase without leading/trailing whitespace
- language code for target language; expected to be valid; expected to be lowercase without leading/trailing whitespace
specifying  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
----------------------------------------------------
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 message
end
--------------------------------------------------------------------------------------------
module entry point
args1: language code -> 
args2: optional target language code; same as  in ; defaults to the local wiki language
return true if  is a mediawiki recognized code; nil else
local function is_code (frame)
	local args = getArgs (frame, {
		valueFunc = function (key, value)
			return (value and  ~= value) and value:lower():gsub ('^%s*(.-)%s*$', '%1') or nil;
		end
		});
	local code = args1;
	local target_lang_code = __validate_target_lang_code (args2);
	return code and _is_code (code, target_lang_code) and true or nil;
end
------------------------------------------------------------------------------
module entry point
args1: language code
args2: optional target language code; same as  in ; defaults to the local wiki language
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, {
		valueFunc = function (key, value)
			return (value and  ~= value) and value:lower():gsub ('%s*(.-)%s*', '%1') or nil;
		end
		});
	local code = args1;
	if not code then
		return err_msg_make ('code: (empty)');
	end
	
	local target_lang_code, msg = __validate_target_lang_code (args2);
	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 message
end
------------------------------------------------------------------------------
local entry point
args1: language name
args2: optional target language code; instruct this function to fetch language name list in 'this' language
return language-code if language-name is a mediawiki recognized name and target language code is valid; error message string else
second return value is a boolean used by is_name(); true when name is found; false else
local function _code_from_name (args)
	local name = args1;
	if not name then
		return err_msg_make ('name: (empty)');
	end
	local target_lang_code, msg =  __validate_target_lang_code (args2);
	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_listv:lower() = k;											-- make a name / code list
	end
	if name_code_listname then
		return name_code_listname, 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()
	end
end
------------------------------------------------------------------------------
module entry point
args1: language name
args2: optional target language code; instruct this function to fetch language name list in 'this' language
return language-code if language-name is a mediawiki recognized name and target language code is valid; error message string else
local function code_from_name (frame)
	local args = getArgs (frame, {
		valueFunc = function (key, value)
			return (value and  ~= value) and value:lower():gsub ('^%s*(.-)%s*$', '%1') or nil;
		end
		});
	
	local result, _ = _code_from_name (args);									-- suppress true/false return used by is_name()
	return result;
end
--------------------------------------------------------------------------------------------
return true if  is a mediawiki recognized language name; false else
args1: language name
args2: optional target language code; instructs _code_from_name to fetch language name list in 'this' language
			defaults to local wiki's language;  when this parameter not valid, language name is assumed to be not valid
local function is_name (frame)
	local args = getArgs (frame, {
		valueFunc = function (key, value)
			return (value and  ~= value) and value:lower():gsub ('%s*(.-)%s*', '%1') or nil;
		end
		});
	
	local _, result = _code_from_name (args);									-- suppress code return used by code_from_name()
	return result and true or nil;
end
----------------------------------------------------------------------
return {
	code_from_name = code_from_name,
	is_code = is_code,
	is_name = is_name,
	name_from_code = name_from_code,
	_is_code = _is_code,														-- entry point from another module
	}