Language Code for Flag Emoji
unicodejavascripti18n
Is it possible to convert language codes such as "de" into an emoji flag, i.e. in Javascript?
Yes, you can convert language codes such as "de" into an emoji flag by using the Unicode regional indicators. In JavaScript you can do this like this:
Solution:
function languageCodeToFlagEmoji(langCode) { // Shorten language to two letters and convert to uppercase const code = langCode.slice(0, 2).toUpperCase(); // Convert each character to a regional indicator emoji return [...code].map(char => String.fromCodePoint(0x1F1E6 + char.charCodeAt(0) - 65)).join(''); } // Example: console.log(languageCodeToFlagEmoji("de")); // 🇩🇪 console.log(languageCodeToFlagEmoji("us")); // 🇺🇸 console.log(languageCodeToFlagEmoji("fr")); // 🇫🇷 console.log(languageCodeToFlagEmoji("gb")); // 🇬🇧
Explanation:
- The language code is shortened to two letters and converted to uppercase.
- The Unicode regional indicators start at 0x1F1E6 and are arranged alphabetically.
- Each letter is converted to its ASCII value (charCodeAt(0)).
- The position of the letter is determined by subtracting 65 ('A'.charCodeAt(0)).
- This is added to 0x1F1E6 to get the corresponding emoji.
- The two emoji characters are combined and displayed as a flag.
This method works for all countries with a standardized ISO 3166-1 Alpha-2 code (two letters). 🚀
Meta data

★★★★★
- Source
- ChatGPT
- Updated on
- February 6, 202518 days ago