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
161 lines (160 loc) • 6.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const command_delta_1 = __importDefault(require("./command-delta"));
const command_full_1 = __importDefault(require("./command-full"));
class Helper {
static getAllI18nFolders(dirPath, arrayOfI18nFolders) {
const files = fs_1.default.readdirSync(dirPath);
arrayOfI18nFolders = arrayOfI18nFolders || [];
files.forEach(function (file) {
if (fs_1.default.statSync(dirPath + path_1.default.sep + file).isDirectory()) {
if (!["node_modules", "dist"].includes(file)) {
arrayOfI18nFolders = Helper.getAllI18nFolders(dirPath + path_1.default.sep + file, arrayOfI18nFolders);
}
if (file === "i18n") {
arrayOfI18nFolders.push(path_1.default.join(dirPath, path_1.default.sep, file));
}
}
});
return arrayOfI18nFolders;
}
static getTextType(line) {
// Samples of classifications:
// #<Type>
// #<Type>:<Description>
// #<Type>,<maxLength>
// #<Type>,<maxLength>:<Description>
const textType = line.split(",", 1)[0].split(":", 1)[0].split("#", 2)[1];
const textTypes = [
"XACT",
"XBUT",
"XCKL",
"XCOL",
"XFLD",
"XGRP",
"XLNK",
"XLOG",
"XLST",
"XMIT",
"XMSG",
"XRBL",
"XSEL",
"XTIT",
"XTOL",
"YINS",
];
return textTypes.includes(textType) ? textType : "";
}
static getMaxLength(line) {
if (line.includes(",")) {
// Handle manual defined maxLength (#TYPE,maxLength:Description)
return Number(line.split(",", 2)[1].split(":", 1)[0]);
}
else {
// Handle maxLength defined by the textType (everyting besides YINS must be <= 120 as defined here https://github.com/SAP-docs/sapui5/blob/main/docs/05_Developing_Apps/text-classification-582ce93.md)
const textType = Helper.getTextType(line);
const textTypesBelow120Characters = [
"XACT",
"XBUT",
"XCKL",
"XCOL",
"XFLD",
"XGRP",
"XLNK",
"XLOG",
"XLST",
"XMIT",
"XMSG",
"XRBL",
"XSEL",
"XTIT",
"XTOL",
];
return textTypesBelow120Characters.includes(textType) ? 120 : 1000;
}
}
static getAllEntriesOfI18nFile(filePath) {
let lines = fs_1.default.readFileSync(filePath).toString().split("\n");
lines = lines.filter((line) => line !== "");
let entryArray = [];
for (const index in lines) {
const line = lines[index].trim();
if (line.startsWith("#")) {
continue;
}
let classification = "";
let textType = "";
let maxLength = 1000;
if (Number(index) > 0) {
const previousLine = lines[Number(index) - 1].trim();
if (previousLine.startsWith("#")) {
textType = Helper.getTextType(previousLine);
if (textType !== "") {
classification = previousLine;
maxLength = Helper.getMaxLength(previousLine);
}
}
}
if (!line) {
continue;
}
entryArray.push({
classification: classification,
textType: textType,
key: line.split("=")[0],
value: line.split("=")[1],
maxLength: maxLength,
});
}
return entryArray;
}
static getAllI18nFilesOfFolder(dirPath) {
return fs_1.default.readdirSync(dirPath).map((file) => {
var _a;
return ({
name: file,
language: ((_a = file.split("_", 2)[1]) === null || _a === void 0 ? void 0 : _a.split(".", 1)[0]) || "fallback",
entries: Helper.getAllEntriesOfI18nFile(path_1.default.join(dirPath, path_1.default.sep, file)),
});
});
}
static getMissingTranslations(files, translate_to) {
const fallback_file = files.find((file) => file.language === "fallback");
let missing_translations = [];
for (const language of translate_to) {
const file = files.find((file) => file.language === language);
let missing_entries = [];
if (file) {
missing_entries = fallback_file.entries.filter((fallback_entry) => !file.entries.some((entry) => entry.key === fallback_entry.key));
}
else {
missing_entries = fallback_file.entries;
}
missing_translations.push({
language: language,
missing_entries: missing_entries
});
}
return missing_translations;
}
static hasMissingEntries(folder) {
for (const missing_translation of folder.missing_translations) {
if (missing_translation.missing_entries.length > 0) {
return true;
}
}
return false;
}
static getDefaultHelpPrefix() {
return "UI5 i18n Translate via SAP Translation Hub CLI by joshuaheislerde.\nThis tool will translate your fallback i18n into other languages.\n\n";
}
static showGeneralHelp() {
console.log(Helper.getDefaultHelpPrefix(), "USAGE:\n", "\n", "\x1b[33mui5-i18n-translate [COMMAND] [COMMAND OPTIONS]\x1b[0m\n", "\n", "COMMAND:\n", "\n", "delta " + command_delta_1.default.description + "\n", "full " + command_full_1.default.description + "\n", "\n", "GLOBAL COMMAND OPTIONS:\n", "\n", "--help Show help for the given command\n");
}
}
exports.default = Helper;