Commits

Bill Branan authored 95a2da64107
Adds script to merge in i18n translation files from themes

See: https://github.com/DSpace/dspace-angular/issues/1299
No tags

scripts/merge-i18n-files.ts

Added
1 +import { projectRoot} from '../webpack/helpers';
2 +const commander = require('commander');
3 +const fs = require('fs');
4 +const JSON5 = require('json5');
5 +const _cliProgress = require('cli-progress');
6 +const _ = require('lodash');
7 +
8 +const program = new commander.Command();
9 +program.version('1.0.0', '-v, --version');
10 +
11 +const LANGUAGE_FILES_LOCATION = 'src/assets/i18n';
12 +
13 +parseCliInput();
14 +
15 +/**
16 + * Purpose: Allows customization of i18n labels from within themes
17 + * e.g. Customize the label "menu.section.browse_global" to display "Browse DSpace" rather than "All of DSpace"
18 + *
19 + * This script uses the i18n files found in a source directory to override settings in files with the same
20 + * name in a destination directory. Only the i18n labels to be overridden need be in the source files.
21 + *
22 + * Execution (using custom theme):
23 + * ```
24 + * yarn merge-i18n -s src/themes/custom/assets/i18n
25 + * ```
26 + *
27 + * Input parameters:
28 + * * Output directory: The directory in which the original i18n files are stored
29 + * - Defaults to src/assets/i18n (the default i18n file location)
30 + * - This is where the final output files will be written
31 + * * Source directory: The directory with override files
32 + * - Required
33 + * - Recommended to place override files in the theme directory under assets/i18n (but this is not required)
34 + * - Files must have matching names in both source and destination directories, for example:
35 + * en.json5 in the source directory will be merged with en.json5 in the destination directory
36 + * fr.json5 in the source directory will be merged with fr.json5 in the destination directory
37 + */
38 +function parseCliInput() {
39 + program
40 + .option('-d, --output-dir <output-dir>', 'output dir when running script on all language files', projectRoot(LANGUAGE_FILES_LOCATION))
41 + .option('-s, --source-dir <source-dir>', 'source dir of transalations to be merged')
42 + .usage('(-s <source-dir> [-d <output-dir>])')
43 + .parse(process.argv);
44 +
45 + if (program.outputDir && program.sourceDir) {
46 + if (!fs.existsSync(program.outputDir) && !fs.lstatSync(program.outputDir).isDirectory() ) {
47 + console.error('Output does not exist or is not a directory.');
48 + console.log(program.outputHelp());
49 + process.exit(1);
50 + }
51 + if (!fs.existsSync(program.sourceDir) && !fs.lstatSync(program.sourceDir).isDirectory() ) {
52 + console.error('Source does not exist or is not a directory.');
53 + console.log(program.outputHelp());
54 + process.exit(1);
55 + }
56 + fs.readdirSync(projectRoot(program.sourceDir)).forEach(file => {
57 + if (fs.existsSync(program.outputDir + '/' + file) )
58 + {
59 + console.log("Merging: "+ program.outputDir + '/' + file + ' with '+ program.sourceDir + '/' + file)
60 + mergeFileWithSource(program.sourceDir + '/' + file, program.outputDir + '/' + file)
61 + }
62 + });
63 + } else {
64 + console.error('Source or Output parameter is missing.');
65 + console.log(program.outputHelp());
66 + process.exit(1);
67 + }
68 +}
69 +
70 +/**
71 + * Reads source file and output file to merge the contents
72 + * > Iterates over the source file keys
73 + * > Updates values for each key and adds new keys as needed
74 + * > Updates the output file with the new merged json
75 + * @param pathToSourceFile Valid path to source file to merge from
76 + * @param pathToOutputFile Valid path to merge and write output
77 + */
78 +function mergeFileWithSource(pathToSourceFile, pathToOutputFile) {
79 + const progressBar = new _cliProgress.SingleBar({}, _cliProgress.Presets.shades_classic);
80 + progressBar.start(100, 0);
81 +
82 + const source_file = fs.readFileSync(pathToSourceFile, 'utf8')
83 + progressBar.update(10);
84 + const output_file = fs.readFileSync(pathToOutputFile, 'utf8')
85 + progressBar.update(20);
86 +
87 + let parsed_source = JSON5.parse(source_file)
88 + progressBar.update(30);
89 + let parsed_output = JSON5.parse(output_file)
90 + progressBar.update(40);
91 +
92 + for (let key of Object.keys(parsed_source)) {
93 + parsed_output[key] = parsed_source[key];
94 + }
95 + progressBar.update(80);
96 + fs.writeFileSync(pathToOutputFile,JSON5.stringify(parsed_output,{space:'\n '}), {encoding:'utf8'})
97 +
98 + progressBar.update(100);
99 + progressBar.stop();
100 +}

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut