Module:CountryData: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 799: Line 799:
function CountryData.getFlagFileName(countryNameOrCode)
function CountryData.getFlagFileName(countryNameOrCode)
     if not countryNameOrCode or countryNameOrCode == '' then return nil end
     if not countryNameOrCode or countryNameOrCode == '' then return nil end
     -- Normalize input: replace underscores with spaces
      
     local input = countryNameOrCode:gsub('_', ' ')
     local inputName = countryNameOrCode:gsub('_', ' ') -- Clean the input
     local code
     local isoCode
     if #input == 2 then
   
         code = string.upper(input)
    -- First, try to get the ISO code by treating inputName as a country name.
    else
    -- CountryData.getCountryCodeByName handles internal normalization.
         -- Normalize country name to canonical form
    isoCode = CountryData.getCountryCodeByName(inputName)
         local norm = CountryData.normalizeCountryName(input)
   
         code = CountryData.getCountryCodeByName(norm)
    -- If no code was found by name, and the inputName itself is 2 characters long,
    -- it might be an ISO code already. Let's validate it.
     if not isoCode and #inputName == 2 then
         -- Check if this 2-char string is a valid country code by attempting to fetch country data.
         -- We use getCountryByCode because it directly uses the code.
         if CountryData.getCountryByCode(inputName) then
            isoCode = inputName -- It's a valid code
         end
     end
     end
     if not code or code == '' then return nil end
   
     return 'Flag_of_' .. code .. '.svg'
    -- If we still don't have a valid ISO code, we can't proceed.
     if not isoCode or isoCode == '' then return nil end
   
    -- Ensure the code is indeed 2 letters long (as a final sanity check).
    if #isoCode ~= 2 then return nil end
 
    -- Construct the filename in the format "Flag-xx.svg" (e.g., "Flag-ad.svg")
     return 'Flag-' .. string.lower(isoCode) .. '.svg'  
end
end


return CountryData
return CountryData