Turn a country code into an emoji flag (US ➑️ πŸ‡ΊπŸ‡Έ)

ionbazan

Ion Bazan

Posted on May 21, 2024

Turn a country code into an emoji flag (US ➑️ πŸ‡ΊπŸ‡Έ)

Flag emojis are a fun and visual way to represent countries and regions. These emojis are part of the Unicode standard and are created using a pair of regional indicator symbols.

In this article, we will explore how to convert 2-letter ISO 3166-1 country codes to flag emojis in Go, PHP and TypeScript.

Understanding Flag Emojis

Flag emojis are composed of two Unicode characters, each representing a regional indicator symbol. For example, the flag of the United States (πŸ‡ΊπŸ‡Έ) is created by placing the Unicode points for the regional indicators "πŸ‡Ί" and "πŸ‡Έ" next to each other.

These Unicode points start at U+1F1E6 (regional indicator symbol letter "A") and continue to U+1F1FF (regional indicator symbol letter "Z").

The formula πŸ€“

The formula to convert a letter to its corresponding regional indicator symbol is:

Unicode point = <ASCII code of letter> βˆ’ 65 + 127462
Enter fullscreen mode Exit fullscreen mode
  • 65 is a decimal value of letter "A"
  • 127462 is a decimal value of U+1F1E6

Since 127462 - 65 = 127397 = 0x1F1A5, we will use 0x1F1A5 in further calculations.

Conversion in Go πŸ‘¨πŸ»β€πŸ’»

Here is an example in Go (Golang) to convert an ISO 3166-1 country code to a flag emoji. This code snippet normalizes the input to uppercase and then converts each character to its corresponding regional indicator symbol. For simplicity, it doesn't perform any validation so you should make sure it is a valid 2-letter code first.

package main

import (
    "fmt"
    "strings"
)

func country2flag(countryCode string) string {
    var flagEmoji strings.Builder
    countryCode = strings.ToUpper(countryCode)
    for _, char := range countryCode {
        flagEmoji.WriteRune(rune(char) + 0x1F1A5)
    }
    return flagEmoji.String()
}

func main() {
    fmt.Println(country2flag("pl"))  // πŸ‡΅πŸ‡±
    fmt.Println(country2flag("JP"))  // πŸ‡―πŸ‡΅
    fmt.Println(country2flag("us"))  // πŸ‡ΊπŸ‡Έ
    fmt.Println(country2flag("EU"))  // πŸ‡ͺπŸ‡Ί
}
Enter fullscreen mode Exit fullscreen mode

Explanation πŸ‘¨πŸ»β€πŸ«

  1. Normalization: The input country code is converted to uppercase using strings.ToUpper().
  2. String Builder: A strings.Builder is used to efficiently build the resulting flag emoji string.
  3. Character Processing: Each character of the uppercase country code is processed: rune(char) + 0x1F1A5: Converts the character to its corresponding regional indicator symbol by adding 0x1F1A5 (127397).
  4. Appending Characters: The resulting regional indicator symbols are appended to the strings.Builder.
  5. Returning the Result: The String() method of strings.Builder is called to get the final flag emoji string.

Conversion in PHP 🐘

Using preg_replace_callback allows to replace each character in a string using a custom callback:

function country2flag(string $countryCode): string
{
    return preg_replace_callback(
        '/./',
        static fn (array $m) => chr(ord($m[0]) + 0x1F1A5),
        strtoupper($countryCode)
    );
}

echo country2flag('pl'); // πŸ‡΅πŸ‡±
echo country2flag('JP'); // πŸ‡―πŸ‡΅
echo country2flag('us'); // πŸ‡ΊπŸ‡Έ
echo country2flag('EU'); // πŸ‡ͺπŸ‡Ί
Enter fullscreen mode Exit fullscreen mode

Conversion in TypeScript 😎

This example uses a combination of split and join to process each character individually.

function country2flag(countryCode: string): string {
    return countryCode
        .toUpperCase()
        .split('')
        .map(char => String.fromCodePoint(char.charCodeAt(0) + 0x1F1A5))
        .join('');
}

console.log(country2flag('pl')); // πŸ‡΅πŸ‡±
console.log(country2flag('JP')); // πŸ‡―πŸ‡΅
console.log(country2flag('us')); // πŸ‡ΊπŸ‡Έ
console.log(country2flag('EU')); // πŸ‡ͺπŸ‡Ί
Enter fullscreen mode Exit fullscreen mode

Conclusion

πŸ€— Converting ISO 3166-1 country codes to flag emojis can be fun and straightforward in Go, PHP and TypeScript! By understanding how regional indicator symbols work and using simple character manipulation, you can easily generate these emojis programmatically in any other language ✨.

πŸ› οΈ Whether you're building a web application or a server-side script, this technique can add a fun and informative visual element to your project.

☝🏻 Please note that the appearance and availability of the flag emojis might vary between systems and locale.

Feel free to post snippets in your favourite programming language in the comments!

This article is based on the snippet I posted on GitHub Gist 3 years ago:

πŸ’– πŸ’ͺ πŸ™… 🚩
ionbazan
Ion Bazan

Posted on May 21, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related