mono/packages/kbot/gui/tauri-app/scripts/translations.js
2025-09-27 07:55:50 +02:00

51 lines
1.7 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
// Replicate __dirname behavior for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const i18nDir = path.join(__dirname, '../i18n');
const outputDir = path.join(__dirname, '../src');
const outputFile = path.join(outputDir, 'translations.ts');
const allTranslations = {};
try {
// Ensure output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const files = fs.readdirSync(i18nDir);
files.forEach(file => {
if (path.extname(file) === '.json') {
const lang = path.basename(file, '.json');
const filePath = path.join(i18nDir, file);
const content = fs.readFileSync(filePath, 'utf8');
try {
allTranslations[lang] = JSON.parse(content);
console.log(`Successfully processed ${file}`);
} catch (parseError) {
console.error(`Error parsing JSON from ${file}:`, parseError);
}
}
});
const tsContent = `// Auto-generated by scripts/translations.js
// Do not edit this file manually.
// All changes will be overwritten.
export const translations: Record<string, { [key: string]: string }> = ${JSON.stringify(allTranslations, null, 2)};
`;
fs.writeFileSync(outputFile, tsContent, 'utf8');
console.log(`\nSuccessfully generated TypeScript translations file at ${outputFile}`);
console.log('Included languages:', Object.keys(allTranslations).join(', '));
} catch (error) {
console.error('Error generating translations file:', error);
process.exit(1);
}