iris-messenger/scripts/CsvToTranslations.mjs

36 lines
1.1 KiB
JavaScript
Raw Normal View History

import fs from 'fs';
// Read the csv file with translations
let csv = fs.readFileSync('translations.csv', 'utf8');
let lines = csv.split('\n');
// Get the list of available languages
2023-02-04 07:30:33 +00:00
let languages = lines[0].split(',').map((l) => l.replace(/"/g, '').trim());
languages.shift();
// Create an object to store the translations
let translations = {};
// Iterate through the csv lines and add the translations to the `translations` object
for (let i = 1; i < lines.length; i++) {
let line = lines[i].split(',');
let key = line[0].replace(/"/g, '');
line.shift();
for (let j = 0; j < languages.length; j++) {
if (!translations[languages[j]]) {
translations[languages[j]] = {};
}
2023-02-03 21:36:25 +00:00
if (line[j]) {
2023-02-04 07:30:33 +00:00
translations[languages[j]][key] = line[j].replace(/"/g, '').trim() || null;
}
}
}
// Write the translations back to the language files
for (let lang in translations) {
let fileContent = `export default ${JSON.stringify(translations[lang], null, 2)};`;
fs.writeFileSync(`../src/js/translations/${lang}.mjs`, fileContent);
}
console.log('Translations added successfully.');