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 | ||
local | local inputName = countryNameOrCode:gsub('_', ' ') -- Clean the input | ||
local code | local isoCode | ||
if # | |||
code | -- First, try to get the ISO code by treating inputName as a country name. | ||
-- CountryData.getCountryCodeByName handles internal normalization. | |||
-- | isoCode = CountryData.getCountryCodeByName(inputName) | ||
-- 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 | |||
return ' | -- 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 | ||