2016-05-24 18:34:06 +03:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/* eslint-disable no-var, no-console */
|
|
|
|
// We can't use strict mode because po2json causes
|
|
|
|
// referenceErrors.
|
|
|
|
|
2018-11-19 19:14:38 +03:00
|
|
|
require('@babel/register');
|
2016-05-24 18:34:06 +03:00
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2017-03-22 14:00:16 +03:00
|
|
|
|
2016-05-24 18:34:06 +03:00
|
|
|
const config = require('config');
|
|
|
|
const po2json = require('po2json');
|
|
|
|
const glob = require('glob');
|
|
|
|
const shelljs = require('shelljs');
|
|
|
|
const chalk = require('chalk');
|
2016-12-20 18:34:07 +03:00
|
|
|
const toSource = require('tosource');
|
2018-10-18 19:43:48 +03:00
|
|
|
const { oneLine } = require('common-tags');
|
2016-05-24 18:34:06 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
const appName = config.get('appName');
|
2016-05-24 18:34:06 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
if (!appName) {
|
|
|
|
console.log(chalk.red('Please specify the appName with NODE_APP_INSTANCE'));
|
2016-05-24 18:34:06 +03:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
const localeDir = path.join(__dirname, '../locale');
|
|
|
|
const poFiles = glob.sync(`${localeDir}/**/${appName}.po`);
|
|
|
|
const dest = path.join(__dirname, '../src/locale/');
|
2016-05-24 18:34:06 +03:00
|
|
|
|
2018-10-18 19:43:48 +03:00
|
|
|
const getLocaleModulePath = (locale, { _config = config } = {}) => {
|
|
|
|
const momentLangMap = _config.get('momentLangMap');
|
|
|
|
|
|
|
|
const momentLocale = locale.replace('_', '-').toLowerCase();
|
|
|
|
const mappedLocale = momentLangMap[momentLocale];
|
|
|
|
|
|
|
|
let localeModulePath = `moment/locale/${momentLocale}`;
|
|
|
|
|
|
|
|
if (mappedLocale) {
|
|
|
|
localeModulePath = `moment/locale/${mappedLocale}`;
|
|
|
|
|
|
|
|
console.info(oneLine`${locale} is mapped to ${mappedLocale} in
|
|
|
|
configuration, therefore ${mappedLocale} will be used.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return localeModulePath;
|
|
|
|
};
|
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
poFiles.forEach((pofile) => {
|
|
|
|
const dir = path.dirname(pofile);
|
|
|
|
const subdir = path.dirname(dir);
|
|
|
|
const locale = path.basename(subdir);
|
|
|
|
const stem = path.basename(pofile, '.po');
|
|
|
|
const localeOutputFilePath = path.join(dest, locale, `${stem}.js`);
|
|
|
|
shelljs.mkdir('-p', path.join(dest, locale));
|
2016-08-02 13:32:53 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
const json = po2json.parseFileSync(pofile, {
|
|
|
|
stringify: true,
|
|
|
|
pretty: true,
|
|
|
|
format: 'jed1.x',
|
|
|
|
fuzzy: config.get('po2jsonFuzzyOutput'),
|
|
|
|
});
|
|
|
|
const localeObject = JSON.parse(json);
|
2016-12-20 18:34:07 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
// Add the moment locale JS into our locale file, if one is available and
|
|
|
|
// we're building for AMO (which is the only app that uses moment right
|
|
|
|
// now).
|
|
|
|
if (appName === 'amo') {
|
|
|
|
var defineLocale = null;
|
|
|
|
try {
|
2018-10-18 19:43:48 +03:00
|
|
|
const localeModulePath = getLocaleModulePath(locale);
|
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
// Check for the locale first; if it doesn't exist we don't have
|
|
|
|
// a moment locale that matches.
|
|
|
|
fs.accessSync(`./node_modules/${localeModulePath}.js`);
|
2017-01-24 01:01:44 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
// We're using `new Function()` here to create a function out of the
|
|
|
|
// raw code; this function won't be executed but will be written out by
|
|
|
|
// `toSource()` so that it can be used later (at runtime, by moment).
|
|
|
|
defineLocale = new Function(`
|
|
|
|
// By requiring this module, the new locale is defined and
|
|
|
|
// registered internally for moment.js
|
|
|
|
require('${localeModulePath}');`);
|
|
|
|
} catch (e) {
|
|
|
|
// We ignore missing locale errors for en_US as its moment's default
|
|
|
|
// locale so we don't need to provide a translation.
|
|
|
|
if (locale !== 'en_US') {
|
|
|
|
console.info(oneLine`No moment i18n available for ${locale};
|
|
|
|
consider adding one or creating a mapping.`);
|
2016-12-20 18:34:07 +03:00
|
|
|
}
|
2017-03-22 14:00:16 +03:00
|
|
|
}
|
2016-12-20 18:34:07 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
localeObject._momentDefineLocale = defineLocale;
|
|
|
|
}
|
2018-05-04 14:24:58 +03:00
|
|
|
|
2018-08-06 22:26:17 +03:00
|
|
|
// This is used to force cache-busting of the JS via changing all locale JS.
|
|
|
|
// DO NOT REMOVE! See https://github.com/mozilla/addons-frontend/issues/4927
|
|
|
|
localeObject._cacheBust = '2018.08.02';
|
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
localeOutputFilePath, `module.exports = ${toSource(localeObject)}`);
|
|
|
|
});
|
2018-10-18 19:43:48 +03:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getLocaleModulePath,
|
|
|
|
};
|