mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2026-11-03 18:11:07 +01:00
56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
/**
|
|
* Generates src/js/intl/generated/<locale>.js + loaders.js.
|
|
*
|
|
* src/js/intl/strings.js holds every locale in one object (~270 KB). The app
|
|
* only needs the active locale, plus en_US as a synchronous fallback, so we
|
|
* split it per-locale here and let intl/ dynamically import the rest.
|
|
*
|
|
* Source of truth remains src/js/intl/strings.js (also used by checkStrings
|
|
* and the translation tooling).
|
|
*/
|
|
const { writeFileSync, mkdirSync } = require('fs');
|
|
const { join } = require('path');
|
|
|
|
const { strings } = require('../src/js/intl/strings');
|
|
|
|
const OUT_DIR = join(__dirname, '..', 'src', 'js', 'intl', 'generated');
|
|
|
|
// key -> { locale: value } ==> locale -> { key: value }
|
|
const byLocale = {};
|
|
Object.keys(strings).forEach((key) => {
|
|
const translations = strings[key];
|
|
Object.keys(translations).forEach((locale) => {
|
|
if (locale === '__desc__') { return; }
|
|
if (!byLocale[locale]) { byLocale[locale] = {}; }
|
|
byLocale[locale][key] = translations[locale];
|
|
});
|
|
});
|
|
|
|
mkdirSync(OUT_DIR, { recursive: true });
|
|
|
|
const locales = Object.keys(byLocale).sort();
|
|
let totalBytes = 0;
|
|
|
|
locales.forEach((locale) => {
|
|
const out = "'use strict';\n\n" +
|
|
'// AUTO-GENERATED by scripts/generateIntlStrings.js -- do not edit.\n' +
|
|
'exports.strings = ' + JSON.stringify(byLocale[locale]) + ';\n';
|
|
writeFileSync(join(OUT_DIR, locale + '.js'), out);
|
|
totalBytes += out.length;
|
|
});
|
|
|
|
// en_US is loaded synchronously by intl/ as the fallback; the rest stream in
|
|
// on demand. Loader keys must be static string literals for the bundler.
|
|
let loaders = "'use strict';\n\n" +
|
|
'// AUTO-GENERATED by scripts/generateIntlStrings.js -- do not edit.\n' +
|
|
'exports.loaders = {\n';
|
|
locales.forEach((locale) => {
|
|
loaders += ' ' + JSON.stringify(locale) +
|
|
': function () { return import(' + JSON.stringify('./' + locale + '.js') + '); },\n';
|
|
});
|
|
loaders += '};\n';
|
|
writeFileSync(join(OUT_DIR, 'loaders.js'), loaders);
|
|
|
|
console.log('Generated ' + locales.length + ' locale string modules (' +
|
|
Math.round(totalBytes / 1024) + ' KB total) in ' + OUT_DIR);
|