From 66e55d3526c39f58aae2d4faacacf91397d68304 Mon Sep 17 00:00:00 2001 From: Hung Viet Nguyen Date: Fri, 11 Mar 2022 01:16:53 +0700 Subject: [PATCH] =?UTF-8?q?[i18n]=20=F0=9F=8C=8E=20sync=20missing=20keys?= =?UTF-8?q?=20to=20other=20language=20This=20will=20ease=20the=20process?= =?UTF-8?q?=20of=20contributing=20to=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../add-missing-keys-to-other-languages.js | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 scripts/front/add-missing-keys-to-other-languages.js diff --git a/scripts/front/add-missing-keys-to-other-languages.js b/scripts/front/add-missing-keys-to-other-languages.js new file mode 100644 index 0000000000..7e2fea96d9 --- /dev/null +++ b/scripts/front/add-missing-keys-to-other-languages.js @@ -0,0 +1,60 @@ +'use strict'; + +const { join, dirname } = require('path'); +const { promisify } = require('util'); +const fs = require('fs-extra'); +const glob = promisify(require('glob').glob); + +const addMissingKeyForSingleFile = async filePath => { + console.log('Start adding missing keys to', filePath); + try { + // 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 updatedFile = Object.keys(mainTranslationFileJSON).reduce((acc, current) => { + if (currentTranslationFileJSON[current]) { + acc[current] = currentTranslationFileJSON[current]; + } else { + acc[current] = mainTranslationFileJSON[current]; + } + + return acc; + }, {}); + await fs.writeJson(filePath, updatedFile, { spaces: 2 }); + console.log('Added missing keys to ', filePath); + return Promise.resolve(); + } catch (err) { + return Promise.reject(err); + } +}; + +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 + .filter(dir => { + return fs.existsSync(join(dir, ...pathToTranslationsFolder, `${lang}.json`)); + }) + .reduce((acc, dir) => { + return [...acc, 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 (process.argv.length < 3) { + console.log( + 'Please provide a language.For examples:\nnode scripts/front/add-missing-keys-to-other-languages.js vi' + ); + process.exit(1); +} +addMissingKeys(process.argv[2]).catch(err => console.error(err));