This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
people/src/frontend/packages/i18n/format-deploy.mjs
Anthony LC f21966cca9 🌐(app-desk) order translations asc
When we pull the translations from crowdin we
get lot of git diff noise with the json file.
We order the keys in the json file to make the
diffs more readable.
2024-03-20 14:23:42 +01:00

57 lines
1.4 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs/yargs';
// Get our args
const argv = yargs(hideBin(process.argv)).argv;
const { app, output } = argv;
const folderPath = './locales/' + app;
const namefile = 'translations.json';
const jsonI18n = {};
// Fetch the files in the locales folder
fs.readdirSync(folderPath).map((language) => {
const languagePath = path.join(folderPath, path.sep, language);
// Crowdin output file in folder, we want to treat only these ones
if (!fs.lstatSync(languagePath).isDirectory()) {
return;
}
jsonI18n[language] = {
translation: {},
};
// Get the json file generated by crowdin
const pathTranslateFile = path.join(languagePath, path.sep, namefile);
if (!fs.existsSync(pathTranslateFile)) {
throw new Error(`File ${pathTranslateFile} not found!`);
}
const json = JSON.parse(fs.readFileSync(pathTranslateFile, 'utf8'));
// Transform the json file to the format expected by i18next
const jsonKeyMessage = {};
Object.keys(json)
.sort()
.forEach((key) => {
jsonKeyMessage[key] = json[key].message;
});
jsonI18n[language] = {
translation: jsonKeyMessage,
};
});
if (!Object.keys(jsonI18n).length) {
throw new Error(`No translation to deploy`);
}
// Write the file to the output
fs.writeFileSync(output, JSON.stringify(jsonI18n), 'utf8');
console.log(`${app} translations deployed!`);