Skip to content

Instantly share code, notes, and snippets.

@shahata
Last active December 21, 2015 15:59
Show Gist options
  • Save shahata/6330387 to your computer and use it in GitHub Desktop.
Save shahata/6330387 to your computer and use it in GitHub Desktop.
//requirements:
//npm install cldr
//npm install csv
//npm install path
'use strict';
var cldr = require('cldr');
var csv = require('csv');
var path = require('path');
var output = [['Key']];
var index = {};
//we want use alpha-3 codes, so let's extract this mapping
function extractTerritoryCodes () {
var territoryCodeMapping = {};
var document = cldr.getDocument(
path.resolve(cldr.cldrPath, 'common', 'supplemental', 'supplementalData.xml'));
document.find('/supplementalData/codeMappings/territoryCodes')
.forEach(function (territoryCodesNode) {
var territoryCode = territoryCodesNode.attr('type').value();
var alpha3 = territoryCodesNode.attr('alpha3');
if (!alpha3) { return; }
territoryCodeMapping[territoryCode] = alpha3.value();
});
return territoryCodeMapping;
}
function doit(locale) {
var reqlen = output[0].length;
output[0].push(locale === 'en' ? 'English' : locale);
var countries = cldr.extractTerritoryDisplayNames(locale);
var alpha3Mapping = extractTerritoryCodes();
for (var i in countries) {
if (i.match(/^\d*$/) || alpha3Mapping[i] === undefined) {
delete countries[i]; //ignore continents
} else {
if (index[i] === undefined) {
index[i] = output.length;
output.push(['countries.'+alpha3Mapping[i]]);
}
if (output[index[i]].length !== reqlen) {
delete output[index[i]];
delete index[i];
console.log('Ignoring: '+locale+' '+i+'\n');
} else {
output[index[i]].push(countries[i]);
}
}
}
}
doit('en');
doit('de');
doit('es');
doit('fr');
doit('it');
doit('ja');
doit('ko');
doit('pl');
doit('pt');
doit('ru');
doit('tr');
csv().from(output).to('countries.csv');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment