strapi/scripts/front/add-missing-keys-to-other-language.js

83 lines
2.7 KiB
JavaScript
Raw Normal View History

// Add missing keys to non-english languages from `en.json`.
// This script eases the process of translating strapi to other languages.
// Usage:
2022-03-14 22:51:49 +07:00
// node scripts/front/add-missing-keys-to-other-language.js [language]
// Example:
2022-03-14 22:51:49 +07:00
// node scripts/front/add-missing-keys-to-other-language.js vi
2022-08-08 15:50:34 +02:00
'use strict';
const { join, dirname } = require('path');
const { promisify } = require('util');
const fs = require('fs-extra');
const glob = promisify(require('glob').glob);
const chalk = require('chalk');
2022-08-08 23:33:39 +02:00
const updateMissingKeysToJSON = async (filePath) => {
// Read translation file
const currentTranslationFileJSON = await fs.readJSON(filePath);
// Read en.json
const mainTranslationFile = join(dirname(filePath), 'en.json');
const mainTranslationFileJSON = await fs.readJSON(mainTranslationFile);
// Add missing keys from en.json to translation file
const updatedFileJSON = Object.keys(mainTranslationFileJSON).reduce((acc, current) => {
if (currentTranslationFileJSON[current]) {
acc[current] = currentTranslationFileJSON[current];
} else {
acc[current] = mainTranslationFileJSON[current];
}
return acc;
}, {});
return updatedFileJSON;
};
2022-08-08 23:33:39 +02:00
const addMissingKeyForSingleFile = async (filePath) => {
console.log('Start adding missing keys to', filePath);
try {
const updatedFileJSON = await updateMissingKeysToJSON(filePath);
await fs.writeJson(filePath, updatedFileJSON, { spaces: 2 });
2022-03-11 17:40:33 +07:00
console.log('Added missing keys to', filePath);
return Promise.resolve();
} catch (err) {
return Promise.reject(err);
}
};
2022-08-08 23:33:39 +02:00
const addMissingKeys = async (lang) => {
// Get translation files
const corePackageDirs = await glob('packages/core/*');
const pluginsPackageDirs = await glob('packages/plugins/*');
const packageDirs = [...corePackageDirs, ...pluginsPackageDirs];
const pathToTranslationsFolder = ['admin', 'src', 'translations'];
const translationFiles = packageDirs
2022-08-08 23:33:39 +02:00
.filter((dir) => {
return fs.existsSync(join(dir, ...pathToTranslationsFolder, `${lang}.json`));
})
2022-08-08 23:33:39 +02:00
.map((dir) => {
return join(dir, ...pathToTranslationsFolder, `${lang}.json`);
});
console.log('List of files to add missing keys', translationFiles, '\n');
// For each file run addMissingKeyForSingleFile
translationFiles.forEach(addMissingKeyForSingleFile);
};
if (require.main === module) {
if (process.argv.length < 3) {
console.warn(
chalk.yellow(
'Please provide a language. For example:\nnode scripts/front/add-missing-keys-to-other-language.js vi'
)
);
process.exit(1);
}
2022-08-08 23:33:39 +02:00
addMissingKeys(process.argv[2]).catch((err) => console.error(err));
}
module.exports = {
updateMissingKeysToJSON,
};