Last active
August 4, 2016 14:06
-
-
Save firstred/0b47dac668a520378c788859767a672d to your computer and use it in GitHub Desktop.
PrestaShop - Generate country names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = require('xmlbuilder'); | |
var filewalker = require('filewalker'); | |
var prestashop_langs_dir = './langs'; // Relative or absolute path to language folder in install dir | |
var cldr_data = './node_modules/cldr-data'; // Cldr data location | |
var _ = require('lodash'); | |
var fs = require('fs'); | |
var missing_langs = []; | |
var found_langs = []; | |
var filewalking_done = false; | |
var analyzing_dirs = 0; | |
var ps_lang_map = { | |
'br': 'pt', // Brazilian Portuguese | |
'no': 'nb', // Norwegian Bokmål | |
'qc': 'fr-CA', // General Canadian French | |
'si': 'sl', // Fix wrong ISO code in PrestaShop | |
'zh': 'zh-Hans', // Chinese Simplified | |
'tw': 'zh-Hant', // Chinese Traditional | |
'vn': 'vi' // Vietnamese | |
}; | |
// Check availability | |
filewalker(prestashop_langs_dir, {recursive: false}) | |
.on('dir', function (lang) { | |
analyzing_dirs++; | |
fs.stat(cldr_data + '/main/' + mapLang(lang), function (err, stats) { | |
if (err) { | |
missing_langs.push(lang); | |
} else { | |
if (!stats.isDirectory()) { | |
missing_langs.push(lang); | |
} else { | |
found_langs.push(lang); | |
} | |
if (filewalking_done) { | |
processLangs(); | |
} | |
} | |
analyzing_dirs--; | |
}); | |
}) | |
.on('done', function () { | |
if (analyzing_dirs == 0) { | |
processLangs(); | |
} | |
filewalking_done = true; | |
}) | |
.walk(); | |
// Process languages | |
function processLangs() { | |
_.forEach(found_langs, function (lang) { | |
var xml = builder.create('entity_country', {version: '1.0', encoding: 'UTF-8'}); | |
var countries = require('cldr-data/main/' + mapLang(lang) + '/territories.json'); | |
_.forEach(countries['main'][mapLang(lang)]['localeDisplayNames']['territories'], function (val, key) { | |
if (key.match(/^([a-zA-Z]{2})$/) && !key.match('ZZ')) { | |
xml.ele('country', {'id': key}) | |
.ele('name', {}) | |
.raw(val) | |
} | |
}); | |
fs.stat(prestashop_langs_dir + '/' + lang + '/data', function (err, stats) { | |
if (err || !stats.isDirectory()) { | |
fs.mkdir(prestashop_langs_dir + '/' + lang + '/data', function (err) { | |
if (err) throw err; | |
fs.readFile(prestashop_langs_dir + '/en/data/index.php', function (err, data) { | |
if (err) throw err; | |
fs.writeFile(prestashop_langs_dir + '/' + lang + '/data/index.php', data, function (err) { | |
if (err) throw err; | |
}); | |
}); | |
fs.writeFile(prestashop_langs_dir + '/' + lang + '/data/country.xml', xml.end({pretty: true}) + '\n', function (err) { | |
if (err) throw err; | |
}); | |
}); | |
} else { | |
fs.writeFile(prestashop_langs_dir + '/' + lang + '/data/country.xml', xml.end({pretty: true}) + '\n', function (err) { | |
if (err) throw err; | |
}); | |
} | |
}); | |
}); | |
} | |
// Map language codes to PrestaShop codes | |
function mapLang(lang) { | |
if (ps_lang_map[lang]) { | |
return ps_lang_map[lang]; | |
} | |
return lang; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "prestashopcountrynames", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"cldr-data": "^29.0.1", | |
"filewalker": "^0.1.2", | |
"html-entities": "^1.2.0", | |
"lodash": "^4.10.0", | |
"node-html-encoder": "0.0.2", | |
"xmlbuilder": "^8.2.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should be run in
/install-dev/
. Sloppy, I know 😉