Skip to content

Commit

Permalink
Adds script to merge in i18n translation files from themes
Browse files Browse the repository at this point in the history
  • Loading branch information
bbranan committed Aug 26, 2021
1 parent adb40d8 commit 95a2da6
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"clean": "yarn run clean:prod && yarn run clean:env && yarn run clean:node",
"clean:env": "rimraf src/environments/environment.ts",
"sync-i18n": "yarn run config:dev && ts-node --project ./tsconfig.ts-node.json scripts/sync-i18n-files.ts",
"merge-i18n": "yarn run config:dev && ts-node --project ./tsconfig.ts-node.json scripts/merge-i18n-files.ts",
"postinstall": "ngcc"
},
"browser": {
Expand Down
100 changes: 100 additions & 0 deletions scripts/merge-i18n-files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { projectRoot} from '../webpack/helpers';
const commander = require('commander');
const fs = require('fs');
const JSON5 = require('json5');
const _cliProgress = require('cli-progress');
const _ = require('lodash');

const program = new commander.Command();
program.version('1.0.0', '-v, --version');

const LANGUAGE_FILES_LOCATION = 'src/assets/i18n';

parseCliInput();

/**
* Purpose: Allows customization of i18n labels from within themes
* e.g. Customize the label "menu.section.browse_global" to display "Browse DSpace" rather than "All of DSpace"
*
* This script uses the i18n files found in a source directory to override settings in files with the same
* name in a destination directory. Only the i18n labels to be overridden need be in the source files.
*
* Execution (using custom theme):
* ```
* yarn merge-i18n -s src/themes/custom/assets/i18n
* ```
*
* Input parameters:
* * Output directory: The directory in which the original i18n files are stored
* - Defaults to src/assets/i18n (the default i18n file location)
* - This is where the final output files will be written
* * Source directory: The directory with override files
* - Required
* - Recommended to place override files in the theme directory under assets/i18n (but this is not required)
* - Files must have matching names in both source and destination directories, for example:
* en.json5 in the source directory will be merged with en.json5 in the destination directory
* fr.json5 in the source directory will be merged with fr.json5 in the destination directory
*/
function parseCliInput() {
program
.option('-d, --output-dir <output-dir>', 'output dir when running script on all language files', projectRoot(LANGUAGE_FILES_LOCATION))
.option('-s, --source-dir <source-dir>', 'source dir of transalations to be merged')
.usage('(-s <source-dir> [-d <output-dir>])')
.parse(process.argv);

if (program.outputDir && program.sourceDir) {
if (!fs.existsSync(program.outputDir) && !fs.lstatSync(program.outputDir).isDirectory() ) {
console.error('Output does not exist or is not a directory.');
console.log(program.outputHelp());
process.exit(1);
}
if (!fs.existsSync(program.sourceDir) && !fs.lstatSync(program.sourceDir).isDirectory() ) {
console.error('Source does not exist or is not a directory.');
console.log(program.outputHelp());
process.exit(1);
}
fs.readdirSync(projectRoot(program.sourceDir)).forEach(file => {
if (fs.existsSync(program.outputDir + '/' + file) )
{
console.log("Merging: "+ program.outputDir + '/' + file + ' with '+ program.sourceDir + '/' + file)
mergeFileWithSource(program.sourceDir + '/' + file, program.outputDir + '/' + file)
}
});
} else {
console.error('Source or Output parameter is missing.');
console.log(program.outputHelp());
process.exit(1);
}
}

/**
* Reads source file and output file to merge the contents
* > Iterates over the source file keys
* > Updates values for each key and adds new keys as needed
* > Updates the output file with the new merged json
* @param pathToSourceFile Valid path to source file to merge from
* @param pathToOutputFile Valid path to merge and write output
*/
function mergeFileWithSource(pathToSourceFile, pathToOutputFile) {
const progressBar = new _cliProgress.SingleBar({}, _cliProgress.Presets.shades_classic);
progressBar.start(100, 0);

const source_file = fs.readFileSync(pathToSourceFile, 'utf8')
progressBar.update(10);
const output_file = fs.readFileSync(pathToOutputFile, 'utf8')
progressBar.update(20);

let parsed_source = JSON5.parse(source_file)
progressBar.update(30);
let parsed_output = JSON5.parse(output_file)
progressBar.update(40);

for (let key of Object.keys(parsed_source)) {
parsed_output[key] = parsed_source[key];
}
progressBar.update(80);
fs.writeFileSync(pathToOutputFile,JSON5.stringify(parsed_output,{space:'\n '}), {encoding:'utf8'})

progressBar.update(100);
progressBar.stop();
}
7 changes: 7 additions & 0 deletions src/themes/custom/assets/i18n/en.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// DSpace default: "All of DSpace"
"menu.section.browse_global": "Browse DSpace",

// DSpace default: "DSpace Angular :: "
"repository.title.prefix": "DSpace :: ",
}

0 comments on commit 95a2da6

Please sign in to comment.