The Country Flag Emoji Polyfill is a crucial tool for developers who need to ensure that country flag emojis display correctly across various platforms, particularly on Chromium-based browsers and Windows systems where native support is lacking. If you've noticed that emojis like "🇨🇭" appear as "ᴄʜ" instead of as flags, this polyfill is designed to address that exact issue.
The polyfill can be included in projects via NPM or directly through a script tag from the Skypack CDN, facilitating easy adoption.
It uses the "Twemoji Country Flags" webfont to ensure that emojis are displayed as intended, without affecting other text styles thanks to the unicode-range property.
The polyfill intelligently injects the webfont only when necessary, ensuring optimal performance without unnecessarily loading resources on browsers that already support country flag emojis.
The backbone of this solution is the selective use of a subset of "Twemoji Mozilla", optimized to be just 78kb, thus minimizing the impact on application performance. It's designed to work specifically with browsers that support color emojis but not country flags, providing a targeted fix.
Despite the broad adoption of emojis, support for country flags remains inconsistent in major browsers on Windows. This polyfill steps in to fill that gap, particularly in response to the lack of planned support from Chromium projects. It ensures that developers can provide a uniform experience across platforms without waiting for browser vendors to implement native support.
The repository includes instructions for building the polyfill locally, catering to updates in the "Twemoji Mozilla" repository, ensuring that the polyfill remains current with any new country flags or emoji updates.
This polyfill is an essential tool for any web developer looking to provide a consistent user experience across all browsers and platforms, particularly in internationalized applications where country flags play a crucial role in content presentation.
This module is sponsored by TalkJS, a Chat API with pre-built UI for web & mobile apps.
Developers can integrate the polyfill into their web projects by installing it via NPM or including it directly in their HTML with a script tag. Additionally, they must update their CSS to use the "Twemoji Country Flags" font in their font-family definitions to activate the emoji display where needed.
npm install country-flag-emoji-polyfill
import { polyfillCountryFlagEmojis } from "country-flag-emoji-polyfill";
// ...
polyfillCountryFlagEmojis();
Thanks to the excellent Skypack CDN, you can also use this polyfill without NPM:
<script type="module" defer>
import { polyfillCountryFlagEmojis } from "https://cdn.skypack.dev/country-flag-emoji-polyfill";
polyfillCountryFlagEmojis();
</script>
This code only works on browsers that support ES Modules, but Chromium has done so for quite a while so it should work appropriately.
See here for a full working example (source)
This will load a webfont called "Twemoji Country Flags"
on relevant browsers. Next, prefix your font-family
CSS with this font everywhere where you want country flags to work. Eg if your CSS currently has this:
body {
font-family: "Helvetica", "Comic Sans", serif;
}
then you want to change it to
body {
font-family: "Twemoji Country Flags", "Helvetica", "Comic Sans", serif;
}
This is safe because the font is loaded such that the browser will only use it for country flag emojis and not for any other characters (using unicode-range
). Therefore, the browser will simply use the next font in the list for every character except country flags.
Browsers that have native support for country flags will not load the font at all, and therefore will simply ignore it in the font-family
list.
function polyfillCountryFlags(fontName?: string, fontUrl?: string): boolean;
Injects a web font with country flags if deemed necessary.
Parameters:
fontName
- (optional) Override the default font name ("Twemoji Country Flags")fontUrl
- (optional) Override the font URL (defaults to a jsdeliver-hosted)
If the browser supports color emojis but not country flags, this function injects a style
element into the HEAD with a web font with country flags, and returns true
. Otherwise, it does nothing and returns false
.
Firefox on Windows adds country flag emoji support falling back on their Twemoji Mozilla font, which itself is simply all Twemoji emojis concatenated into a single huge color font.
Chromium, however, apparently does not plan to support country flags on Windows, except if Windows itself adds it. This means that Chromium-based browsers such as Chrome, Edge and Brave won't likely support it soon either. That's a huge chunk of browser users, who will complain that other people's nice flag emojis look to them like "ᴄʜ" and not like a picture.
Until either Microsoft or Google recognize how ridiculous this is, you're stuck with this polyfill.
This polyfill merely combines other people's hard work.
The key building block of this polyfill is a font, "Twemoji Country Flags", a subset of "Twemoji Mozilla" made using the excellent pyftsubset
tool from fonttools.
This is important, because Twemoji Mozilla is 1.5MB, which is a pretty huge hit on your app perfomance. The subset is only 78kb, which is much better.
It then injects some CSS to load this font as a webfont, but only if the browser supports regular emojis and not country flags. It uses the excellent is-emoji-supported for all the heavy lifting.
As far as I can tell, all browsers that have this problem support WOFF2 fonts, so I made no effort to do the usual multi-font-format @font-face
syntax (with eg ttf and woff fonts also).
This might need updates if Twemoji Mozilla gets a new version - especially if new country flags are added.
- clone the repo
- make sure you're on a system with bash, fonttools and curl (On my WSL/Ubuntu, a single
apt install fonttools
was enough) - run
npm run make-font
- find the new font in
dist/TwemojiCountryFlags.woff2