Skip to content

Instantly share code, notes, and snippets.

@jgoodall
Last active July 27, 2023 02:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jgoodall/15e3a65d7f46c644106c to your computer and use it in GitHub Desktop.
Save jgoodall/15e3a65d7f46c644106c to your computer and use it in GitHub Desktop.
Download flag css and convert to 3 char country codes

Download SVG country flags and css, then convert the codes from the iso 3166 two character codes to three character codes.

Usage

./get_flags.sh

Then copy the flag-icon.css file into public/css and the flags directory to public/.

/* globals require, console */
'use strict';
var fs = require('fs');
var path = require('path');
var process = require('process');
// destination output
var destDir = 'flags';
try {
fs.mkdirSync(destDir);
} catch (err) {
if (err.code !== 'EEXIST') {
console.error(err);
process.exit(1);
}
}
// read mappings
var contents = fs.readFileSync('iso-3166.json', 'utf8');
var iso3166 = JSON.parse(contents);
var mappings = {};
for (var i = 0; i < iso3166.length; i++) {
var key = iso3166[i]['alpha-2'].toLowerCase();
var val = iso3166[i]['alpha-3'].toLowerCase();
mappings[key] = val;
}
// rename svg files
var flagDir = 'flag-icon-css-0.8.4/flags/4x3';
var files = fs.readdirSync(flagDir);
for (var i = 0; i < files.length; i++) {
var f = files[i];
var src = path.join(flagDir, f);
var file = path.parse(f);
var newName = mappings[file.name] + file.ext;
var dst = path.join(destDir, newName);
fs.createReadStream(src).pipe(fs.createWriteStream(dst));
}
// create the css
var cssFile = 'flag-icon-css-0.8.4/css/flag-icon.css';
var content = fs.readFileSync(cssFile, 'utf-8');
for (var twoChar in mappings) {
if (mappings.hasOwnProperty(twoChar)) {
var threeChar = mappings[twoChar];
var re = new RegExp('.*\.flag-icon-squared {\n.*\n}\n');
content = content.replace(re, '');
re = new RegExp('4x3/' + twoChar + '\.svg');
content = content.replace(re, threeChar + '.svg');
re = new RegExp('flag-icon-' + twoChar + ' ');
content = content.replace(re, 'flag-icon-' + threeChar + ' ');
}
}
fs.writeFileSync('flag-icon.css', content, 'utf8')
#!/bin/bash
echo 'Downloading ISO 3166 mappings..'
curl -sL -o iso-3166.json https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.json
echo 'Downloading SVG icon flags...'
curl -sL https://github.com/lipis/flag-icon-css/archive/0.8.4.tar.gz | tar xzf -
echo 'Converting ISO 3166 codes from 2 character codes to 3 character codes..'
node ./convert_codes.js
echo 'Cleaning up...'
rm iso-3166.json
rm -rf flag-icon-css-0.8.4
echo 'SVG flags are in the "flags" directory and style sheet is "flag-icon.css"'
@domaa11
Copy link

domaa11 commented Jul 27, 2023

Thank you @jgoodall, Good script. I had to modify it to change lines 30 and 42 from this directory:
flag-icon-css-0.8.4/
To this:
flag-icons-0.8.4/

But worked well after that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment