/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const languages = ['en', 'de', 'fr', 'nl', 'ko', 'it'];
const translationsDir = 'src/core/locales';
const outputCsvFilePath = 'translations.csv';
const encoding = 'utf16le'; // Change this to the appropriate encoding
// Initialize CSV data with headers
const csvData = [['Key', ...languages]];
// Read translations for each language
for (const language of languages) {
const translationFilePath = path.join(translationsDir, language, 'translation.json');
const jsonData = fs.readFileSync(translationFilePath, 'utf8'); // Specify the encoding here
const translations = JSON.parse(jsonData);
// Add language's translations to the CSV data
for (const key of Object.keys(translations)) {
// Find or create a row for the key
let row = csvData.find((row) => row[0] === key);
if (!row) {
row = [key];
csvData.push(row);
}
// Add translation value to the corresponding column
row.push(translations[key]);
}
}
// Convert CSV data to string
const csvString = csvData.map((row) => row.map((cell) => `"${cell}"`).join(',')).join('\n');
// Write CSV file with appropriate encoding
fs.writeFileSync(outputCsvFilePath, csvString, encoding);
console.log('CSV file successfully written');