|
| 1 | +/** |
| 2 | + * For some unexpected reasons it can happen that the translations in Crowdin are lost. |
| 3 | + * If that happens, we can rebuild the Crowdin translations file from our translated json file. |
| 4 | + * |
| 5 | + * This script is used to rebuild the translations file from the empty skeleton file generated by Crowdin. |
| 6 | + */ |
| 7 | + |
| 8 | +import fs from 'fs'; |
| 9 | +import path from 'path'; |
| 10 | + |
| 11 | +import { hideBin } from 'yargs/helpers'; |
| 12 | +import yargs from 'yargs/yargs'; |
| 13 | + |
| 14 | +// Get our args |
| 15 | +const argv = yargs(hideBin(process.argv)).argv; |
| 16 | +const { app, output, language } = argv; |
| 17 | + |
| 18 | +const folderPath = './locales/' + app; |
| 19 | +const namefile = 'translations-skeleton.json'; |
| 20 | +const nameRebuildfile = 'translations-rebuild.json'; |
| 21 | + |
| 22 | +const pathRebuildFile = path.join(folderPath, path.sep, nameRebuildfile); |
| 23 | + |
| 24 | +// Get the skeleton generated from crowdin |
| 25 | +const pathSkeletonFile = path.join(folderPath, path.sep, namefile); |
| 26 | + |
| 27 | +if (!fs.existsSync(pathSkeletonFile)) { |
| 28 | + throw new Error(`File ${pathSkeletonFile} not found!`); |
| 29 | +} |
| 30 | + |
| 31 | +// Get the translated file |
| 32 | +if (!fs.existsSync(output)) { |
| 33 | + throw new Error(`File ${output} not found!`); |
| 34 | +} |
| 35 | + |
| 36 | +const jsonSkel = JSON.parse(fs.readFileSync(pathSkeletonFile, 'utf8')); |
| 37 | +const jsonTrans = JSON.parse(fs.readFileSync(output, 'utf8')); |
| 38 | + |
| 39 | +// Transform the json file to the format expected by i18next |
| 40 | +const jsonRebuild = jsonSkel; |
| 41 | +Object.keys(jsonSkel) |
| 42 | + .sort() |
| 43 | + .forEach((key) => { |
| 44 | + jsonRebuild[key]['message'] = jsonTrans[language]['translation'][key] || ''; |
| 45 | + }); |
| 46 | + |
| 47 | +// Write the file to the output |
| 48 | +fs.writeFileSync(pathRebuildFile, JSON.stringify(jsonRebuild), 'utf8'); |
| 49 | + |
| 50 | +console.log(`${app} translations rebuild!`); |
0 commit comments