Modulo:CountryFlags
Questo modulo converte i codici paese di due lettere (come IT, US) in emoji di bandiere nazionali (come 🇮🇹, 🇺🇸) e viceversa. Si basa sui simboli indicatori regionali Unicode che, quando combinati in coppie, vengono visualizzati come bandiere nazionali nella maggior parte dei browser e sistemi operativi moderni.
Istruzioni
La funzione toFlag
accetta un codice paese di due lettere (non sensibile alle maiuscole) e restituisce l'emoji della bandiera corrispondente.
Codice | Risultato |
---|---|
{{#invoke: CountryFlags | toFlag | IT }} |
🇮🇹 |
{{#invoke: CountryFlags | toFlag | us }} |
🇺🇸 |
{{#invoke: CountryFlags | toFlag | JP }} |
🇯🇵 |
{{#invoke: CountryFlags | toFlag | de }} |
🇩🇪 |
{{#invoke: CountryFlags | toFlag | troppolungo }} |
Error: Input must be a 2-letter country code |
La funzione fromFlag
accetta un'emoji di bandiera e restituisce il codice paese di due lettere corrispondente.
Codice | Risultato |
---|---|
{{#invoke: CountryFlags | fromFlag | 🇮🇹 }} |
IT |
{{#invoke: CountryFlags | fromFlag | 🇺🇸 }} |
US |
{{#invoke: CountryFlags | fromFlag | 🇯🇵 }} |
JP |
{{#invoke: CountryFlags | fromFlag | 🇩🇪 }} |
DE |
{{#invoke: CountryFlags | fromFlag | 💩 }} |
Error: Input is not a valid flag emoji |
Uso programmatico
Per utilizzare il modulo all'interno di altri moduli Lua, è possibile importarlo e utilizzare le funzioni getFlagEmoji
e getCountryCode
:
local flags = require('Module:CountryFlags')
local bandiera = flags.getFlagEmoji("IT") -- restituisce 🇮🇹
local codice = flags.getCountryCode("🇮🇹") -- restituisce IT
Note tecniche
Gli emoji delle bandiere funzionano combinando due simboli indicatori regionali Unicode (intervallo da U+1F1E6
a U+1F1FF
). Questi simboli corrispondono alle 26 lettere dell'alfabeto latino, permettendo la rappresentazione di 676 possibili combinazioni di due lettere. Non tutte le combinazioni di due lettere rappresentano un paese esistente o riconosciuto, ma il modulo accetterà qualsiasi input di due lettere valido.
È importante notare che la visualizzazione delle emoji delle bandiere può variare a seconda del browser, del sistema operativo o del dispositivo utilizzato. Alcuni sistemi più vecchi potrebbero visualizzare i due simboli indicatori regionali separati invece dell'emoji della bandiera completa. Per ovviare parzialmente a questo problema, Pokémon Central Wiki implementa un polyfill che viene aggiunto all'HTML delle pagine unicamente su Windows e su browser basati su Chromium.
-- Provides functions to convert country codes to flag emojis and vice versa
local p = {}
-- Local function to convert a country code to flag emoji
local function countryCodeToFlag(countryCode)
-- Check if the input is valid
if not countryCode or type(countryCode) ~= "string" then
return nil, "Input must be a 2-letter country code"
end
-- Trim whitespace and convert to uppercase
countryCode = mw.text.trim(countryCode)
countryCode = string.upper(countryCode)
-- Check length after trimming
if #countryCode ~= 2 then
return nil, "Input must be a 2-letter country code"
end
-- Regional indicator symbols start at Unicode code point U+1F1E6 (for 'A')
-- We need to convert each letter to its corresponding regional indicator symbol
local flagEmoji = ""
for i = 1, 2 do
local char = string.sub(countryCode, i, i)
-- Check if character is a valid uppercase letter
if char < "A" or char > "Z" then
return nil, "Country code must contain only letters A-Z"
end
-- Convert ASCII letter to regional indicator symbol
-- 'A' is ASCII 65, and regional indicator A is Unicode 0x1F1E6 (127462 decimal)
local codePoint = string.byte(char) - 65 + 0x1F1E6
-- Convert code point to UTF-8 sequence
flagEmoji = flagEmoji .. mw.ustring.char(codePoint)
end
return flagEmoji
end
-- Local function to convert a flag emoji to country code
local function flagToCountryCode(flagEmoji)
-- Check if the input is valid
if not flagEmoji or type(flagEmoji) ~= "string" then
return nil, "Input must be a flag emoji"
end
-- Trim whitespace
flagEmoji = mw.text.trim(flagEmoji)
-- A flag emoji consists of two regional indicator symbols
-- Each regional indicator symbol is a surrogate pair in UTF-16
-- This means a flag emoji is typically represented as a 4-byte sequence per letter
-- Let's check if we have at least 2 characters (which could be the flag emoji)
local len = mw.ustring.len(flagEmoji)
if len < 2 then
return nil, "Input is not a valid flag emoji"
end
local countryCode = ""
-- Process the first two Unicode characters (should be regional indicators)
for i = 1, 2 do
local char = mw.ustring.sub(flagEmoji, i, i)
local codePoint = mw.ustring.codepoint(char)
-- Check if this is a regional indicator symbol (range U+1F1E6 to U+1F1FF)
if codePoint < 0x1F1E6 or codePoint > 0x1F1FF then
return nil, "Input is not a valid flag emoji"
end
-- Convert regional indicator to ASCII letter
-- Regional indicator A (0x1F1E6) corresponds to ASCII 'A' (65)
local letter = string.char(codePoint - 0x1F1E6 + 65)
countryCode = countryCode .. letter
end
return countryCode
end
-- Main function to be called from a template to convert country code to flag
function p.toFlag(frame)
local countryCode = frame.args[1] or frame:getParent().args[1]
if not countryCode then
return '<span class="error">Error: No country code provided</span>'
end
local flagEmoji, errorMsg = countryCodeToFlag(countryCode)
if not flagEmoji then
return '<span class="error">Error: ' .. (errorMsg or "Unknown error") .. '</span>'
end
return flagEmoji
end
-- Main function to be called from a template to convert flag to country code
function p.fromFlag(frame)
local flagEmoji = frame.args[1] or frame:getParent().args[1]
if not flagEmoji then
return '<span class="error">Error: No flag emoji provided</span>'
end
local countryCode, errorMsg = flagToCountryCode(flagEmoji)
if not countryCode then
return '<span class="error">Error: ' .. (errorMsg or "Unknown error") .. '</span>'
end
return countryCode
end
-- For programmatic use within other Lua modules
function p.getFlagEmoji(countryCode)
return countryCodeToFlag(countryCode)
end
-- For programmatic use within other Lua modules
function p.getCountryCode(flagEmoji)
return flagToCountryCode(flagEmoji)
end
return p