#!/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'); var glob = require('glob'); var path = require('path'); var po2icu = require('po2icu'); var localeCompare = require('../locale-compare'); // ----------------------------------------------------------------------------- // Main script // ----------------------------------------------------------------------------- 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'); 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); } var icuTemplateFile = path.resolve(__dirname, '../../en.json'); var idsWithICU = JSON.parse(fs.readFileSync(icuTemplateFile, 'utf8')); var locales = { en: idsWithICU }; var icuWithIds = {}; for (var id in idsWithICU) { icuWithIds[idsWithICU[id]] = id; } var md5WithIds = localeCompare.getMD5Map(icuWithIds); // Get ui localization strings first glob(poUiDir + '/*', function (err, files) { if (err) throw new Error(err); 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); translations = localeCompare.mergeNewTranslations(translations, jsTranslations, md5WithIds); } catch (err) { process.stdout.write(lang + ': ' + err + '\n'); } try { var pyTranslations = po2icu.poFileToICUSync(lang, pyFile); translations = localeCompare.mergeNewTranslations(translations, pyTranslations, md5WithIds); } catch (err) { process.stdout.write(lang + ': ' + err + '\n'); } locales[lang] = translations; }); fs.writeFileSync(outputFile, JSON.stringify(locales, null, 4)); });