Jump to content

Module:Sandbox/Certes

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Certes (talk | contribs) at 17:40, 9 December 2018 (Allow link= syntax in language list). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local function name_from_code (frame)
	local subtags = {};															-- IETF subtags script, region, variant, and private
	local raw_code = frame.args[1];												-- save a copy of the input IETF subtag
	local link = 'yes' == frame.args['link'];									-- save a copy of the link-enable positional parameter (value can be anything)
	local code;																	-- the language code
	local msg;																	-- gets an error message if IETF language tag is malformed or invalid
	local language_list = {''};
	local language_name;
	
	code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
	if msg then
		local template = (frame.args['template'] and table.concat ({'{{', frame.args['template'], '}}: '})) or '';	-- make template name (if provided by the template)
		return table.concat ({'<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: ', template, msg, '</span>'});
	end

	if lang_data.override[raw_code:lower()] then								-- look for whole IETF tag in override table (force lower case)
		language_list = lang_data.override[raw_code:lower()];
	elseif lang_data.override[code] then										-- not there so try basic language code in override table
		language_list = lang_data.override[code];
	elseif not is_set (subtags.variant) then									
		if lang_name_table.lang[code] then
			language_list = lang_name_table.lang[code];
		end
	else																		-- TODO: is this the right thing to do: take language display name from variants table?
		if lang_name_table.variant[subtags.variant] then						-- TODO: there is some discussion at Template talk:Lang about having a label parameter for use when variant name is not desired among other things
			language_list = lang_name_table.variant[subtags.variant]['descriptions'];	-- table entries sometimes have multiple names, always take the first one
		end
	end

	language_name = language_list[1];											-- table entries sometimes have multiple names, always take the first one
	language_name = language_name:gsub ('%s+%b()', '');							-- remove IANA parenthetical disambiguators or qualifiers from names that have them

	if link then																-- when |link=yes, wikilink the language name
		if language_list["link"] then
			return make_wikilink (language_list["link"], language_name);		-- explicit wikilink given with link= syntax
		elseif language_name:find ('languages') then
			return make_wikilink (language_name);								-- collective language name uses simple wikilink
		else
			return make_wikilink (language_name .. ' language', language_name);	-- language name with wikilink
		end
	end

	return language_name;

end