| 
									
										
										
										
											2022-03-11 01:23:51 +07:00
										 |  |  | // 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]
 | 
					
						
							| 
									
										
										
										
											2022-03-11 01:23:51 +07:00
										 |  |  | // 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { join, dirname } = require('path'); | 
					
						
							|  |  |  | const { promisify } = require('util'); | 
					
						
							|  |  |  | const fs = require('fs-extra'); | 
					
						
							|  |  |  | const glob = promisify(require('glob').glob); | 
					
						
							| 
									
										
										
										
											2022-03-14 22:47:31 +07:00
										 |  |  | const chalk = require('chalk'); | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const updateMissingKeysToJSON = async (filePath) => { | 
					
						
							| 
									
										
										
										
											2022-03-15 15:52:57 +07:00
										 |  |  |   // 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) => { | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  |   console.log('Start adding missing keys to', filePath); | 
					
						
							|  |  |  |   try { | 
					
						
							| 
									
										
										
										
											2022-03-15 15:52:57 +07:00
										 |  |  |     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); | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  |     return Promise.resolve(); | 
					
						
							|  |  |  |   } catch (err) { | 
					
						
							|  |  |  |     return Promise.reject(err); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const addMissingKeys = async (lang) => { | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  |   // 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) => { | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  |       return fs.existsSync(join(dir, ...pathToTranslationsFolder, `${lang}.json`)); | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     .map((dir) => { | 
					
						
							| 
									
										
										
										
											2022-03-14 22:49:27 +07:00
										 |  |  |       return join(dir, ...pathToTranslationsFolder, `${lang}.json`); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-03-11 01:16:53 +07:00
										 |  |  |   console.log('List of files to add missing keys', translationFiles, '\n'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // For each file run addMissingKeyForSingleFile
 | 
					
						
							|  |  |  |   translationFiles.forEach(addMissingKeyForSingleFile); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 22:47:31 +07:00
										 |  |  | if (require.main === module) { | 
					
						
							| 
									
										
										
										
											2022-04-13 00:31:32 +04:30
										 |  |  |   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)); | 
					
						
							| 
									
										
										
										
											2022-03-14 22:47:31 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2022-03-15 15:52:57 +07:00
										 |  |  |   updateMissingKeysToJSON, | 
					
						
							| 
									
										
										
										
											2022-03-14 22:47:31 +07:00
										 |  |  | }; |