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
44 lines (43 loc) • 3.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const helper_1 = __importDefault(require("./helper"));
const ora_1 = __importDefault(require("ora"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const os_1 = __importDefault(require("os"));
class CommandFull {
static showHelp() {
console.log(helper_1.default.getDefaultHelpPrefix(), "COMMAND NAME:\n", "\n", `\x1b[33m${CommandFull.command} - ${CommandFull.description}\x1b[0m\n`, "\n", "USAGE:\n", "\n", `\x1b[33mui5-i18n-translate ${CommandFull.command} [...OPTIONS]\x1b[0m\n`, "\n", " --auth_url uaa URL of the key for the SAP Translation Hub instance\n", " --auth_client_id uaa client id of the key for the SAP Translation Hub instance\n", " --auth_client_secret uaa client secret of the key for the SAP Translation Hub instance\n", " --api_url The api URL of the key for the SAP Translation Hub instance\n", " --fallback_language The BCP 47 language used in the fallback i18n file (default: en)\n", " --translate_to The BCP 47 language(s) for which i18n files should be maintained (e.g. de,fr,es)\n", " --test Don't run translate or change data, only show what would happen");
}
static async translate(settings, translator, arrayOfI18nFolders) {
var _a;
for (const folder of arrayOfI18nFolders) {
const spinner = (0, ora_1.default)(`[${folder.path}] Translating...`).start();
const fallbackFile = (_a = folder === null || folder === void 0 ? void 0 : folder.files) === null || _a === void 0 ? void 0 : _a.find((file) => file.language === "fallback");
let translations = await translator.translate(fallbackFile.entries, settings.targetLanguages, settings.sourceLanguage);
for (const language of settings.targetLanguages) {
var writeStream = fs_1.default.createWriteStream(path_1.default.join(folder.path, path_1.default.sep, `i18n_${language}.properties`));
for (const entry of fallbackFile.entries) {
let translation = translations
.find(translation => translation.key === entry.key)
.translations.find(translation => translation.language === language)
.value;
if (translation) {
if (entry.classification) {
writeStream.write(entry.classification + os_1.default.EOL);
}
writeStream.write(`${entry.key}=${translation}` + os_1.default.EOL);
}
}
writeStream.end();
}
spinner.succeed(`[${folder.path}] Translation complete`);
}
}
}
CommandFull.command = "full";
CommandFull.description = "Add translations for all keys in i18n-files (this will overwrite existing translations!)";
exports.default = CommandFull;