2015-10-15 23:01:58 -04:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Converts the existing .po translation files in the module to JSON files.
|
|
|
|
|
Requires po2json in order to work. Takes as input a directory
|
|
|
|
|
in which to store the resulting json translation files.
|
|
|
|
|
*/
|
|
|
|
|
var fs = require('fs');
|
2015-10-16 15:09:20 -04:00
|
|
|
|
var glob = require('glob');
|
|
|
|
|
var path = require('path');
|
2015-10-15 23:01:58 -04:00
|
|
|
|
var po2icu = require('po2icu');
|
|
|
|
|
|
2015-11-09 11:40:36 -05:00
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Main script
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2015-10-15 23:01:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var args = process.argv.slice(2);
|
|
|
|
|
|
|
|
|
|
if (!args.length) {
|
|
|
|
|
process.stdout.write('A destination directory must be specified.');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var poUiDir = path.resolve(__dirname, '../../node_modules/scratchr2_translations/ui');
|
2015-10-19 18:13:28 -04:00
|
|
|
|
var outputFile = path.resolve(__dirname, '../../', args[0]);
|
|
|
|
|
// Create the directory if it doesn't exist.
|
|
|
|
|
var fileInfo = path.parse(outputFile);
|
|
|
|
|
try {
|
|
|
|
|
fs.accessSync(fileInfo.dir, fs.F_OK);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Doesn't exist – create it.
|
|
|
|
|
fs.mkdirSync(fileInfo.dir);
|
|
|
|
|
}
|
2015-10-15 23:01:58 -04:00
|
|
|
|
|
|
|
|
|
var icuTemplateFile = path.resolve(__dirname, '../../en.json');
|
|
|
|
|
var idsWithICU = JSON.parse(fs.readFileSync(icuTemplateFile, 'utf8'));
|
2015-11-09 11:40:36 -05:00
|
|
|
|
var locales = {
|
|
|
|
|
en: idsWithICU
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-15 23:01:58 -04:00
|
|
|
|
var icuWithIds = {};
|
|
|
|
|
for (var id in idsWithICU) {
|
|
|
|
|
icuWithIds[idsWithICU[id]] = id;
|
|
|
|
|
}
|
2015-11-09 11:40:36 -05:00
|
|
|
|
var md5WithIds = helpers.getMD5Map(icuWithIds);
|
2015-10-15 23:01:58 -04:00
|
|
|
|
|
|
|
|
|
// Get ui localization strings first
|
|
|
|
|
glob(poUiDir + '/*', function (err, files) {
|
2015-10-16 15:10:27 -04:00
|
|
|
|
if (err) throw new Error(err);
|
|
|
|
|
|
2015-10-15 23:01:58 -04:00
|
|
|
|
files.forEach(function (file) {
|
|
|
|
|
var lang = file.split('/').pop();
|
|
|
|
|
var jsFile = path.resolve(file, 'LC_MESSAGES/djangojs.po');
|
|
|
|
|
var pyFile = path.resolve(file, 'LC_MESSAGES/django.po');
|
|
|
|
|
|
|
|
|
|
var translations = {};
|
|
|
|
|
try {
|
|
|
|
|
var jsTranslations = po2icu.poFileToICUSync(lang, jsFile);
|
2015-11-09 11:40:36 -05:00
|
|
|
|
translations = helpers.mergeNewTranslations(translations, jsTranslations, md5WithIds);
|
2015-10-15 23:01:58 -04:00
|
|
|
|
} catch (err) {
|
2015-10-19 18:13:28 -04:00
|
|
|
|
process.stdout.write(lang + ': ' + err + '\n');
|
2015-10-15 23:01:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
var pyTranslations = po2icu.poFileToICUSync(lang, pyFile);
|
2015-11-09 11:40:36 -05:00
|
|
|
|
translations = helpers.mergeNewTranslations(translations, pyTranslations, md5WithIds);
|
2015-10-15 23:01:58 -04:00
|
|
|
|
} catch (err) {
|
2015-10-19 18:13:28 -04:00
|
|
|
|
process.stdout.write(lang + ': ' + err + '\n');
|
2015-10-15 23:01:58 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locales[lang] = translations;
|
|
|
|
|
});
|
|
|
|
|
fs.writeFileSync(outputFile, JSON.stringify(locales, null, 4));
|
|
|
|
|
});
|