Jump to content

Module:CountryFlag

From Wikimania
Module documentation
-- Module:CountryFlag
local p = {}
local wd = require('Module:Wd')
local data = mw.loadJsonData('Module:CountryFlag/data.json')

-- Build lookup table for alpha2, alpha3, and itemLabel → JSON entry
local lookup = {}
for _, entry in ipairs(data) do
    if entry.alpha2 then lookup[entry.alpha2] = entry end
    if entry.alpha3 then lookup[entry.alpha3] = entry end
    if entry.itemLabel then lookup[entry.itemLabel] = entry end
end

function p.flagAndName(frame)
    local input = mw.text.trim(frame.args[1] or '')
    if input == '' then
        return 'No input provided'
    end

    local entry = lookup[input] or lookup[mw.ustring.upper(input)] or lookup[mw.ustring.lower(input)]
    if not entry then
        return 'Unknown country input: ' .. input
    end

    local qid = entry.item
	local label = mw.wikibase.getLabelByLang(qid, mw.language.getContentLanguage().code)

    -- FIX: wrap arguments in a table
    local flagFile = wd._property{ 'raw', eid = qid, 'P41' }

    if flagFile and flagFile ~= '' then
        return string.format('[[File:%s|20px|class=noviewer]] %s', flagFile, label)
    else
        return label or ('[QID: ' .. qid .. ']')
    end
    
end

return p