Module:Lang/name to tag

From blackwiki
Jump to navigation Jump to search

This module creates language name (key) to language tag (value) support tables used by Module:Lang:

These reversed data tables are loaded and used by _tag_from_name().


--[[--------------------------< A D D _ T O _ L I S T >--------------------------------------------------------

local function to add <name> (key) and <tag> (value) pair to <list>

<had_dab> is boolean indicating that <name> did have dab that was stripped

<name> without dab must always win; this rule arises because of the three Marwari language code/name pairs:
	rwr: Marwari (India)
	mve: Marwari (Pakistan)
	mwr: Marwari
when
	<name> is in <list> and <had_dab> is true do not overwrite; name may have been added from code/name pair without dab (mwr:Marwari)
	<name> is in <list> and <had_dab> is false or nil, ok to overwrite because code/name pairs without dab must win 

]]

local function add_to_list (list, name, tag, had_dab)
	if list[name] then															-- if already in <list>
		if 2 == tag:len() then													-- is this is an ISO 639-1 code?
			list[name] = tag;													-- yes, overwrite ISO 639-3 language <name> and <tag> pair in <list>
		end
		if not had_dab then
			list[name] = tag;
		end
	else																		-- here when not yet in <list>
		list[name] = tag;														-- add language <name> and <tag> (value) pair to <list>
	end
end


--[[--------------------------< N A M E - T O - T A G   D A T A >----------------------------------------------

Creates name-to-tag tables from the data in Module:Language/name/data (from Module:Language/data/iana languages,
Module:Language/data/ISO 639-3, Module:Language/data/wp languages) and Module:Lang/data so that templates can
get language tags from the same names as the {{lang}} templates get from those tags.  The conversion prefers
ISO 639-1 codes.

]]

local rev_lang_name_table = {};													-- same as Module:Language/name/data except reversed so language name is key and language tag is value
local rev_lang_data = {};														-- same as Module:Lang/data except reversed

local raw_data = mw.loadData ('Module:Language/name/data')						-- load the data from the standard source module

for tag, name_table in pairs (raw_data.lang) do
	if 3 == tag:len() then														-- do iso 639-2, -3 codes first
		local name_raw = name_table[1]:lower();									-- there can be multiple names, always take the first name; may have disambiguation
		local name, dab = name_raw:gsub ('%s+%b()', '');						-- remove parenthetical disambiguators or qualifiers from names that have them; <dab> non-zero when disambiguation removed
	
		add_to_list (rev_lang_name_table, name_raw, tag);						-- add no-dab-names and names-with-dab here
	
		if 0 ~= dab then														-- if dab was removed
			add_to_list (rev_lang_name_table, name, tag, true);					-- add names-stripped-of-dab; true flag to prevent overwrite of no-dab-names
		end
	end
end

for tag, name_table in pairs (raw_data.lang) do
	if 2 == tag:len() then														-- now do iso 639-1 codes
		local name_raw = name_table[1]:lower();									-- there can be multiple names, always take the first name; may have disambiguation
		local name, dab = name_raw:gsub ('%s+%b()', '');						-- remove parenthetical disambiguators or qualifiers from names that have them; <dab> non-zero when disambiguation removed

		add_to_list (rev_lang_name_table, name_raw, tag);						-- add no-dab-names and names-with-dab here
	
		if 0 ~= dab then														-- if dab was removed
			add_to_list (rev_lang_name_table, name, tag, true);					-- add names-stripped-of-dab; true flag to prevent overwrite of no-dab-names
		end
	end
end

raw_data = mw.loadData ('Module:Lang/data')										-- load the data from the override source module
for tag, name_table in pairs (raw_data.override) do
	if 2 < tag:len() then														-- do iso 639-2, -3 codes and ietf tags first
		local name_raw = name_table[1]:lower();									-- there can be multiple names, always take the first name
		local name, dab = name_raw:gsub ('%s+%b()', '');						-- remove parenthetical disambiguators or qualifiers from names that have them
		add_to_list (rev_lang_data, name_raw, tag);
	
		if 0 ~= dab then														-- if dab was removed
			add_to_list (rev_lang_data, name, tag, true);						-- flag to say there was a dab
		end
	end
end

for tag, name_table in pairs (raw_data.override) do
	if 2 == tag:len() then														-- now do iso 639-1 codes
		local name_raw = name_table[1]:lower();									-- there can be multiple names, always take the first name
		local name, dab = name_raw:gsub ('%s+%b()', '');						-- remove parenthetical disambiguators or qualifiers from names that have them
		add_to_list (rev_lang_data, name_raw, tag);
	
		if 0 ~= dab then														-- if dab was removed
			add_to_list (rev_lang_data, name, tag, true);						-- flag to say there was a dab
		end
	end
end

raw_data = {};																	-- init back to an empty table?


--[[--------------------------< E X P O R T E D   T A B L E S >------------------------------------------------
]]

return {
	rev_lang_name_table = rev_lang_name_table,
	rev_lang_data = rev_lang_data,
	}