umami/scripts/merge-messages.js

41 lines
1.0 KiB
JavaScript
Raw Normal View History

2022-11-04 05:33:37 +01:00
/* eslint-disable no-console */
2020-09-12 13:19:33 +02:00
const fs = require('fs');
const path = require('path');
const prettier = require('prettier');
2020-09-14 04:01:02 +02:00
const messages = require('../build/messages.json');
2020-09-12 13:19:33 +02:00
2023-08-31 18:32:17 +02:00
const dest = path.resolve(__dirname, '../src/lang');
2020-09-14 04:01:02 +02:00
const files = fs.readdirSync(dest);
const keys = Object.keys(messages).sort();
2020-09-12 13:19:33 +02:00
2023-04-22 09:55:55 +02:00
/*
This script takes extracted messages and merges them
with the existing files under `lang`. Any newly added
keys will be printed to the console.
*/
2020-09-12 13:19:33 +02:00
files.forEach(file => {
2023-08-31 18:32:17 +02:00
const lang = require(`../src/lang/${file}`);
2020-09-12 13:19:33 +02:00
console.log(`Merging ${file}`);
const merged = keys.reduce((obj, key) => {
const message = lang[key];
2023-04-17 01:34:49 +02:00
if (file === 'en-US.json') {
obj[key] = messages[key].defaultMessage;
} else {
obj[key] = message || messages[key].defaultMessage;
}
2020-09-12 13:19:33 +02:00
if (!message) {
2020-09-12 13:26:32 +02:00
console.log(`* Added key ${key}`);
2020-09-12 13:19:33 +02:00
}
return obj;
}, {});
const json = prettier.format(JSON.stringify(merged), { parser: 'json' });
2020-09-14 04:01:02 +02:00
fs.writeFileSync(path.resolve(dest, file), json);
2020-09-12 13:19:33 +02:00
});