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

63 lines (62 loc) 2.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); class Translator { constructor(authUrl, authClientId, authClientSecret, apiUrl) { this.authUrl = authUrl; this.authClientId = authClientId; this.authClientSecret = authClientSecret; this.apiUrl = apiUrl; } async requestToken() { if (this.token) { return; } const encodedParams = new URLSearchParams(); encodedParams.set("client_id", this.authClientId); encodedParams.set("client_secret", this.authClientSecret); encodedParams.set("grant_type", "client_credentials"); encodedParams.set("response_type", "token"); const options = { method: 'POST', url: `${this.authUrl}/oauth/token`, headers: { 'content-type': 'application/x-www-form-urlencoded' }, data: encodedParams, }; try { const response = await axios_1.default.request(options); this.token = response.data.access_token; } catch (error) { throw new Error(error.message); } } async translate(units, targetLanguages, sourceLanguage) { var _a; await this.requestToken(); const options = { method: "POST", url: `${this.apiUrl}/translationhub/api/v2/translate`, headers: { "content-type": "application/json; charset=utf-8", authorization: `Bearer ${this.token}`, }, data: { targetLanguages: targetLanguages, sourceLanguage: sourceLanguage, units: units } }; try { const response = await axios_1.default.request(options); return (_a = response.data) === null || _a === void 0 ? void 0 : _a.units; } catch (error) { throw new Error(error.message); } } } exports.default = Translator;