UNPKG

ui5-i18n-translate

Version:

A command-line interface (CLI) tool to effortlessly translate your UI5 i18n fallback files into multiple languages using the SAP Translation Hub. This package streamlines the localization process for your UI5 applications by automating the translation of

96 lines (83 loc) 3.42 kB
#!/usr/bin/env node import minimist from "minimist"; import Settings from "./settings"; import Helper from "./helper"; import CommandDelta from "./command-delta"; import CommandFull from "./command-full"; import Translator from "./translator"; (async () => { const currentDirectory = process.cwd() const parameter = minimist(process.argv.slice(2)) // exit if no command has been entered if (parameter._.length !== 1) { Helper.showGeneralHelp() process.exit() } const settings = Settings.Instance // exit if a non-exiting command has been entered const command = parameter._[0]; if (!["delta", "full"].includes(command)) { Helper.showGeneralHelp() process.exit() } // exit if help parameter has been requested if (parameter.help) { switch (command) { case CommandDelta.command: CommandDelta.showHelp() break case CommandFull.command: CommandFull.showHelp() break } process.exit() } // read information let arrayOfI18nFolders = Helper.getAllI18nFolders(currentDirectory, []) arrayOfI18nFolders = arrayOfI18nFolders.map((folder) => ({ path: folder })) for (const folder of arrayOfI18nFolders) { folder.files = Helper.getAllI18nFilesOfFolder(folder.path) folder.missing_translations = Helper.getMissingTranslations( folder.files, settings.targetLanguages ) } // exit if any folder does not contain a fallback i18n for (const folder of arrayOfI18nFolders) { const fallbackFile = folder?.files?.find((file) => file.language === "fallback") if (!fallbackFile) { console.log(`[${folder.path}] Folder does not contain a fallback file`) process.exit() } } // print information console.log(`${arrayOfI18nFolders.length} location${arrayOfI18nFolders.length !== 1 ? "s" : ""} of i18n folders found:`) for (const folder of arrayOfI18nFolders) { console.log(` - ${folder.path}`) for (const file of folder.files) { console.log(` - ${file.name} (${file.language}): ${file.entries.length} ${file.entries.length !== 1 ? "entries" : "entry"}`) } if (Helper.hasMissingEntries(folder)) { console.log(` Missing translations:`); for (const missing_translation of folder.missing_translations) { console.log(` - ${missing_translation.language}: ${missing_translation.missing_entries.length} ${missing_translation.missing_entries.length !== 1 ? "entries" : "entry"}`) } } else { console.log(` No missing translations found!\n`) } } // exit if test mode is enabled if (settings.isTest) { process.exit() } // handle translation const translator = new Translator(settings.authUrl, settings.authClientId, settings.authClientSecret, settings.apiUrl) switch (command) { case CommandDelta.command: CommandDelta.translate(settings, translator, arrayOfI18nFolders) break case CommandFull.command: CommandFull.translate(settings, translator, arrayOfI18nFolders) break } })();