Skip to content

Commit

Permalink
refactor: Update language files with missing translations
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocjohn committed Sep 5, 2024
1 parent 38bbf8e commit 3c4832a
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions scripts/add-missing-translations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const fs = require('fs');
const path = require('path');

// Paths
const translationsDir = path.join(__dirname, '../src/languages');
const missingTranslationsPath = path.join(__dirname, 'missing_translations.json');

// Read missing translations JSON
const missingTranslations = JSON.parse(fs.readFileSync(missingTranslationsPath, 'utf8'));

// Function to update language files
const updateLanguageFiles = () => {
Object.entries(missingTranslations).forEach(([languageFile, translations]) => {
const languageFilePath = path.join(translationsDir, languageFile);

// Check if the language file exists
if (fs.existsSync(languageFilePath)) {
// Read the language file content
const languageData = JSON.parse(fs.readFileSync(languageFilePath, 'utf8'));

// Add missing translations
translations.forEach(({ key, value }) => {
const keyParts = key.split('.');

// Add the nested keys and values to the language JSON file
let current = languageData;
keyParts.forEach((part, index) => {
if (index === keyParts.length - 1) {
current[part] = value; // Add the value at the final key
} else {
current = current[part] = current[part] || {}; // Continue nesting
}
});
});

// Write the updated data back to the language file
fs.writeFileSync(languageFilePath, JSON.stringify(languageData, null, 2), 'utf8');
console.log(`Updated ${languageFile}`);
} else {
console.log(`Language file ${languageFile} not found.`);
}
});
};

// Run the script
updateLanguageFiles();

0 comments on commit 3c4832a

Please sign in to comment.