addons-frontend/bin/build-locales

106 строки
3.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* eslint-disable no-var, no-console */
// We can't use strict mode because po2json causes
// referenceErrors.
require('@babel/register');
const fs = require('fs');
const path = require('path');
const config = require('config');
const po2json = require('po2json');
const glob = require('glob');
const shelljs = require('shelljs');
const chalk = require('chalk');
const toSource = require('tosource');
const { oneLine } = require('common-tags');
const appName = config.get('appName');
if (!appName) {
console.log(chalk.red('Please specify the appName with NODE_APP_INSTANCE'));
process.exit(1);
}
const localeDir = path.join(__dirname, '../locale');
const poFiles = glob.sync(`${localeDir}/**/${appName}.po`);
const dest = path.join(__dirname, '../src/locale/');
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;
};
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));
const json = po2json.parseFileSync(pofile, {
stringify: true,
pretty: true,
format: 'jed1.x',
fuzzy: config.get('po2jsonFuzzyOutput'),
});
const localeObject = JSON.parse(json);
// 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 {
const localeModulePath = getLocaleModulePath(locale);
// 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`);
// 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.`);
}
}
localeObject._momentDefineLocale = defineLocale;
}
// 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)}`);
});
module.exports = {
getLocaleModulePath,
};