Module:TemplateHelpers: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
Tag: Manual revert
Line 297: Line 297:
     -- Use the caching wrapper
     -- Use the caching wrapper
     return p.withCache(cacheKey, function()
     return p.withCache(cacheKey, function()
         -- First try to process as ISO-2 or country name
         return CountryData.formatCountries(value)
        local result = CountryData.formatCountries(value)
       
        -- If no output was generated, check if input might contain ISO-3 codes
        if result == "" then
            -- Split multi-value input
            local iso2Values = {}
            for country in string.gmatch(value, "[^;]+") do
                local trimmed = NormalizationText.trim(country)
                if trimmed and trimmed ~= "" then
                    -- Try to get ISO-2 from ISO-3
                    local iso2 = CountryData.getCountryCodeByIso3(trimmed)
                    if iso2 then
                        table.insert(iso2Values, iso2)
                    end
                end
            end
           
            -- If we found any ISO-2 codes, join them and try again
            if #iso2Values > 0 then
                result = CountryData.formatCountries(table.concat(iso2Values, "; "))
            end
        end
       
        return result
     end)
     end)
end
end