Module:Flags: Difference between revisions

From TwogPedia
No edit summary
mNo edit summary
 
(11 intermediate revisions by the same user not shown)
Line 4: Line 4:


function Flags.icon(flagString, category)
function Flags.icon(flagString, category)
 
flagString = mw.text.split(flagString, ',')[1]
local flagKey = Flags._convertToKey(flagString)
local flagKey = Flags._convertToKey(flagString)
if flagKey then
if flagKey then
local flagData = MasterData.data[flagKey]
local flagData = MasterData.data[flagKey]
local link = 'Category:'.. category .. '/' .. flagData.name
-- if category then
return '<span class="flag">[[' .. flagData.img ..
-- local link = 'Category:'.. category .. '/' .. flagData.name
'|' .. flagData.name .. '|link=' .. link .. '|]]</span>'
-- return '[[' .. flagData.img .. '|' .. flagData.name .. '|link=' .. link .. '|20px]]'
-- else
return '[[' .. flagData.img .. '|' .. flagData.name .. '|20px|link=]]'
-- end
else  
else  
mw.log('Unknown flag: ', flagName)
mw.log('Unknown flag: ', flagString)
return false
return false
end
end
Line 36: Line 39:
function Flags._convertToKey(flagName)
function Flags._convertToKey(flagName)
flagName = flagName:gsub(' ', ''):lower()
flagName = flagName:gsub(' ', ''):lower()
return MasterData.twoLetter[flagName]
return MasterData.twoLetter[flagName]
or MasterData.threeLetter[flagName]
or MasterData.threeLetter[flagName]

Latest revision as of 14:03, 11 March 2024

Documentation for this module may be created at Module:Flags/doc

local MasterData = mw.loadData('Module:Flags/MasterData')

local Flags = {}

function Flags.icon(flagString, category)
	flagString = mw.text.split(flagString, ',')[1]
	local flagKey = Flags._convertToKey(flagString)
	
	if flagKey then
		local flagData = MasterData.data[flagKey]
		-- if category then
		-- 	local link = 'Category:'.. category .. '/' .. flagData.name
		-- 	return '[[' .. flagData.img .. '|' .. flagData.name .. '|link=' .. link .. '|20px]]'
		-- else 
			return '[[' .. flagData.img .. '|' .. flagData.name .. '|20px|link=]]'
		-- end
	else 
		mw.log('Unknown flag: ', flagString)
		return false
	end
end

-- Converts a country name, flag code, or alias to a standardized country name
function Flags.CountryName(flagName)
	if (flagName == nil or flagName == '') then
		return ''
	end

	local flagKey = Flags._convertToKey(flagName)

	if flagKey then
		return MasterData.data[flagKey].name
	else
		mw.log('Unknown flag: ', flagName)
		return mw.text.trim(mw.text.split(Template.safeExpand(mw.getCurrentFrame(), 'Flag/' .. flagName), '|', true)[2] or '')
	end
end

function Flags._convertToKey(flagName)
	flagName = flagName:gsub(' ', ''):lower()
	return MasterData.twoLetter[flagName]
		or MasterData.threeLetter[flagName]
		or MasterData.aliases[flagName]
		or (MasterData.data[flagName] and flagName)
end

return Flags