mue/scripts/updatetranslations.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
2024-03-16 21:48:42 +00:00
const path = require('path');
const merge = require('@eartharoid/deep-merge');
2023-03-15 20:55:54 +00:00
const compareAndRemoveKeys = (json1, json2) => {
for (let key in json1) {
if (json2.hasOwnProperty(key)) {
if (typeof json1[key] === 'object' && typeof json2[key] === 'object') {
compareAndRemoveKeys(json1[key], json2[key]);
}
} else {
delete json1[key];
}
}
2023-03-16 11:11:18 +00:00
};
2023-03-15 20:55:54 +00:00
2024-03-17 11:34:35 +00:00
const localesDir = path.join(__dirname, '../src/i18n/locales');
const achievementsDir = path.join(localesDir, 'achievements');
2024-03-16 21:48:42 +00:00
2024-03-17 11:34:35 +00:00
// Check if the locales directory exists, if not, create it
if (!fs.existsSync(localesDir)) {
fs.mkdirSync(localesDir, { recursive: true });
}
// Check if the achievements directory exists, if not, create it
if (!fs.existsSync(achievementsDir)) {
fs.mkdirSync(achievementsDir, { recursive: true });
}
fs.readdirSync(localesDir).forEach((file) => {
if (file === 'en_GB.json') {
return;
2024-03-16 21:48:42 +00:00
}
2024-03-17 11:34:35 +00:00
if (fs.lstatSync(path.join(localesDir, file)).isDirectory()) {
return;
}
2024-03-17 11:34:35 +00:00
const en = require(path.join(localesDir, 'en_GB.json'));
const newdata = merge(en, require(path.join(localesDir, file)));
2023-03-15 20:55:54 +00:00
2024-03-17 11:34:35 +00:00
compareAndRemoveKeys(newdata, en);
2023-03-15 20:55:54 +00:00
2024-03-17 11:34:35 +00:00
fs.writeFileSync(path.join(localesDir, file), JSON.stringify(newdata, null, 2));
2024-03-17 11:34:35 +00:00
fs.appendFileSync(path.join(localesDir, file), '\n');
});
2024-03-17 11:34:35 +00:00
fs.readdirSync(achievementsDir).forEach((file) => {
if (file === 'en_GB.json') {
return;
}
2024-03-17 11:34:35 +00:00
if (fs.lstatSync(path.join(achievementsDir, file)).isDirectory()) {
return;
}
2024-03-17 11:34:35 +00:00
const enGBFilePath = path.join(achievementsDir, 'en_GB.json');
if (!fs.existsSync(enGBFilePath)) {
console.error(`File 'en_GB.json' does not exist in the directory '${achievementsDir}'`);
return;
}
2024-03-17 11:34:35 +00:00
const en = require(enGBFilePath);
const newdata = merge(en, require(path.join(achievementsDir, file)));
2024-03-17 11:34:35 +00:00
compareAndRemoveKeys(newdata, en);
2024-03-17 11:34:35 +00:00
fs.writeFileSync(path.join(achievementsDir, file), JSON.stringify(newdata, null, 2));
2024-03-16 21:48:42 +00:00
2024-03-17 11:34:35 +00:00
fs.appendFileSync(path.join(achievementsDir, file), '\n');
2024-03-16 21:48:42 +00:00
2024-03-17 11:34:35 +00:00
const locales = fs.readdirSync(localesDir);
locales.forEach((locale) => {
if (!fs.existsSync(path.join(achievementsDir, locale))) {
// ignore directories
if (fs.lstatSync(path.join(localesDir, locale)).isDirectory()) {
return;
2024-03-16 21:48:42 +00:00
}
2024-03-17 11:34:35 +00:00
fs.writeFileSync(path.join(achievementsDir, locale), JSON.stringify(en, null, 2));
fs.appendFileSync(path.join(achievementsDir, locale), '\n');
}
});
2024-03-17 11:34:35 +00:00
});