UNPKG

translation-io-sync

Version:

This Typescript implementation acts as a `translation.io` client

90 lines (89 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const synchronization_response_1 = require("./synchronization-response"); const url_builder_1 = require("./url-builder"); const node_fetch_1 = require("node-fetch"); class Client { constructor(configuration) { this.configuration = configuration; } async update(translationKeys, purge) { if (translationKeys.length === 0) { return []; } else { const url = new url_builder_1.UrlBuilder(this.configuration.service).synchronize(); const payload = this.buildUpdateParameters(translationKeys, purge); console.log(`Sending translations to translation.io`); const response = await (0, node_fetch_1.default)(url, { method: "POST", headers: { "Content-Type": "application/json", "Accept": "application/json" }, body: JSON.stringify(payload) }); if (response.status === 200) { const parsedResponse = synchronization_response_1.SynchronizationResponse.parse(await response.json()); this.logUpdateResponse(parsedResponse); return parsedResponse.getTextTranslations; } else { const body = await response.json(); throw new Error(`An error occurred (status code: ${response.status}) when calling translation.io. Body: ${JSON.stringify(body)}`); } } } async init(translations) { const url = new url_builder_1.UrlBuilder(this.configuration.service).init(); const payload = this.buildInitParameters(translations); console.log(`Initializing project @ translation.io`); const response = await (0, node_fetch_1.default)(url, { method: "POST", headers: { "Content-Type": "application/json", "Accept": "application/json" }, body: JSON.stringify(payload) }); if (response.status === 200) { return await response.json(); } else { const body = await response.json(); throw new Error(`An error occurred (status code: ${response.status}) when calling translation.io. Body: ${JSON.stringify(body)}`); } } buildInitParameters(translations) { const content = {}; content.target_languages = this.configuration.targetLocales; content.version = this.configuration.service.version; content.source_language = this.configuration.sourceLocale; translations.forEach(translation => { content[`po_data_${translation.locale}`] = translation.content; content[`yaml_po_data_${translation.locale}`] = ""; }); return content; } buildUpdateParameters(translationKeys, purge) { return { target_languages: this.configuration.targetLocales, pot_data: translationKeys[0].content, version: this.configuration.service.version, source_language: this.configuration.sourceLocale, purge: purge.toString() }; } logUpdateResponse(response) { console.log(`The project '${response.projectName}' has been updated at '${response.projectUrl}'`); if (response.numberOfUnusedSegments > 0) { const unusedKeys = response.unusedSegments.map(segment => segment.messageId).join("\n>>"); console.log(`> ${response.numberOfUnusedSegments} translations are unused:\n ${unusedKeys}`); } else { console.log("> There is no key unused!"); } } } exports.Client = Client;