2025-05-17 18:15:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
module CountryFlagHelper
|
|
|
|
|
def country_flag(country_name)
|
|
|
|
|
country_code = country_to_code(country_name)
|
2025-11-04 15:21:20 -05:00
|
|
|
return '' unless country_code
|
|
|
|
|
|
|
|
|
|
country_code = 'TW' if country_code == 'CN-TW'
|
2025-05-17 18:15:25 -04:00
|
|
|
|
|
|
|
|
# Convert country code to regional indicator symbols (flag emoji)
|
2025-11-04 15:21:20 -05:00
|
|
|
country_code.upcase.each_char.map { |c| (c.ord + 127_397).chr(Encoding::UTF_8) }.join
|
2025-05-17 18:15:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def country_to_code(country_name)
|
2025-05-18 05:17:25 -04:00
|
|
|
mapping = Country.names_to_iso_a2
|
2025-05-17 18:15:25 -04:00
|
|
|
|
|
|
|
|
return mapping[country_name] if mapping[country_name]
|
|
|
|
|
|
|
|
|
|
mapping.each do |name, code|
|
|
|
|
|
return code if country_name.downcase == name.downcase
|
|
|
|
|
return code if country_name.downcase.include?(name.downcase) || name.downcase.include?(country_name.downcase)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|