generaltranslation 2.0.7 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/codes/codes.d.ts +4 -4
- package/dist/index.d.ts +30 -21
- package/dist/index.js +35 -32
- package/dist/translation/_bundleRequests.d.ts +15 -0
- package/dist/translation/_bundleRequests.js +102 -0
- package/dist/translation/_intl.d.ts +19 -0
- package/dist/translation/_intl.js +47 -0
- package/dist/translation/_translate.d.ts +20 -12
- package/dist/translation/_translate.js +65 -65
- package/dist/translation/_translateReactChildren.d.ts +1 -1
- package/dist/translation/_translateReactChildren.js +2 -2
- package/package.json +1 -1
package/dist/codes/codes.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
type LanguageObject = {
|
|
2
2
|
language: string;
|
|
3
|
-
script
|
|
4
|
-
region
|
|
5
|
-
}
|
|
3
|
+
script?: string;
|
|
4
|
+
region?: string;
|
|
5
|
+
};
|
|
6
6
|
/**
|
|
7
7
|
* Returns a language object from an array of codes or a single code.
|
|
8
8
|
* Returns null if there's no matching language.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _getLanguageObject, _isSameLanguage } from './codes/codes';
|
|
2
2
|
import { Content } from './translation/_translate';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Type representing the constructor parameters for the GT class.
|
|
5
5
|
*/
|
|
6
6
|
type GTConstructorParams = {
|
|
7
7
|
apiKey?: string;
|
|
@@ -27,26 +27,33 @@ declare class GT {
|
|
|
27
27
|
* @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
|
|
28
28
|
*/
|
|
29
29
|
constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Translates a string, caching it for re-use.
|
|
32
|
+
* @param {Content} content - The content to translate.
|
|
33
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
34
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
35
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
36
|
+
*/
|
|
37
|
+
translate(content: Content, targetLanguage: string, metadata?: {
|
|
38
|
+
[key: string]: any;
|
|
36
39
|
}): Promise<{
|
|
37
40
|
translation: string;
|
|
38
41
|
error?: Error | unknown;
|
|
39
42
|
}>;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Translates a single piece of content and caches for use in a public project.
|
|
45
|
+
* @param {Content} content - The content to translate.
|
|
46
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
47
|
+
* @param {string} projectID - The ID of the project
|
|
48
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
49
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
|
50
|
+
*/
|
|
51
|
+
intl(content: Content, targetLanguage: string, projectID?: string, metadata?: {
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
}): Promise<{
|
|
47
54
|
translation: string;
|
|
48
55
|
error?: Error | unknown;
|
|
49
|
-
}
|
|
56
|
+
}>;
|
|
50
57
|
/**
|
|
51
58
|
* Translates the content of React children elements.
|
|
52
59
|
*
|
|
@@ -57,16 +64,18 @@ declare class GT {
|
|
|
57
64
|
*
|
|
58
65
|
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
|
59
66
|
*/
|
|
60
|
-
translateReactChildren(
|
|
61
|
-
|
|
62
|
-
targetLanguage: string;
|
|
63
|
-
metadata: {
|
|
64
|
-
[key: string]: any;
|
|
65
|
-
};
|
|
67
|
+
translateReactChildren(content: any, targetLanguage: string, metadata?: {
|
|
68
|
+
[key: string]: any;
|
|
66
69
|
}): Promise<{
|
|
67
70
|
translation: any | null;
|
|
68
71
|
error?: Error | unknown;
|
|
69
72
|
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Bundles multiple requests and sends them to the server.
|
|
75
|
+
* @param requests - Array of requests to be processed and sent.
|
|
76
|
+
* @returns A promise that resolves to an array of processed results.
|
|
77
|
+
*/
|
|
78
|
+
bundleRequests(requests: any[]): Promise<Array<any | null>>;
|
|
70
79
|
}
|
|
71
80
|
export default GT;
|
|
72
81
|
export declare const getLanguageObject: typeof _getLanguageObject;
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
// `generaltranslation` language toolkit
|
|
3
3
|
// © 2024, General Translation, Inc.
|
|
4
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
-
if (k2 === undefined) k2 = k;
|
|
6
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(o, k2, desc);
|
|
11
|
-
}) : (function(o, m, k, k2) {
|
|
12
|
-
if (k2 === undefined) k2 = k;
|
|
13
|
-
o[k2] = m[k];
|
|
14
|
-
}));
|
|
15
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
-
}) : function(o, v) {
|
|
18
|
-
o["default"] = v;
|
|
19
|
-
});
|
|
20
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
-
if (mod && mod.__esModule) return mod;
|
|
22
|
-
var result = {};
|
|
23
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
-
__setModuleDefault(result, mod);
|
|
25
|
-
return result;
|
|
26
|
-
};
|
|
27
4
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
5
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
6
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -40,10 +17,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
40
17
|
exports.isSameLanguage = exports.getLanguageName = exports.getLanguageCode = exports.getLanguageObject = void 0;
|
|
41
18
|
// ----- IMPORTS ----- //
|
|
42
19
|
const codes_1 = require("./codes/codes");
|
|
43
|
-
const
|
|
20
|
+
const _bundleRequests_1 = __importDefault(require("./translation/_bundleRequests"));
|
|
21
|
+
const _intl_1 = __importDefault(require("./translation/_intl"));
|
|
22
|
+
const _translate_1 = __importDefault(require("./translation/_translate"));
|
|
44
23
|
const _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
|
|
45
24
|
// TO DO
|
|
46
|
-
// - Translation API
|
|
47
25
|
// - Times/dates?
|
|
48
26
|
// - Currency conversion?
|
|
49
27
|
// ----- CORE CLASS ----- //
|
|
@@ -72,14 +50,29 @@ class GT {
|
|
|
72
50
|
this.defaultLanguage = defaultLanguage.toLowerCase();
|
|
73
51
|
this.baseURL = baseURL;
|
|
74
52
|
}
|
|
75
|
-
|
|
76
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Translates a string, caching it for re-use.
|
|
55
|
+
* @param {Content} content - The content to translate.
|
|
56
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
57
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
58
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
59
|
+
*/
|
|
60
|
+
translate(content, targetLanguage, metadata) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
62
|
return yield (0, _translate_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
|
78
63
|
});
|
|
79
64
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Translates a single piece of content and caches for use in a public project.
|
|
67
|
+
* @param {Content} content - The content to translate.
|
|
68
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
69
|
+
* @param {string} projectID - The ID of the project
|
|
70
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
71
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
|
72
|
+
*/
|
|
73
|
+
intl(content, targetLanguage, projectID, metadata) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
return yield (0, _intl_1.default)(this, content, targetLanguage, projectID || this.projectID, Object.assign({ projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
|
83
76
|
});
|
|
84
77
|
}
|
|
85
78
|
/**
|
|
@@ -92,11 +85,21 @@ class GT {
|
|
|
92
85
|
*
|
|
93
86
|
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
|
94
87
|
*/
|
|
95
|
-
translateReactChildren(
|
|
96
|
-
return __awaiter(this,
|
|
88
|
+
translateReactChildren(content, targetLanguage, metadata) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
90
|
return yield (0, _translateReactChildren_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
|
98
91
|
});
|
|
99
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Bundles multiple requests and sends them to the server.
|
|
95
|
+
* @param requests - Array of requests to be processed and sent.
|
|
96
|
+
* @returns A promise that resolves to an array of processed results.
|
|
97
|
+
*/
|
|
98
|
+
bundleRequests(requests) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
return (0, _bundleRequests_1.default)(this, requests);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
100
103
|
}
|
|
101
104
|
// ----- EXPORTS ----- //
|
|
102
105
|
// Export the class
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
type Request = {
|
|
2
|
+
type: 'translate' | 'intl' | 'react';
|
|
3
|
+
data: any;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Bundles multiple requests and sends them to the server.
|
|
7
|
+
* @param gt - Contains the baseURL and apiKey for the server request.
|
|
8
|
+
* @param requests - Array of requests to be processed and sent.
|
|
9
|
+
* @returns A promise that resolves to an array of processed results.
|
|
10
|
+
*/
|
|
11
|
+
export default function _bundleRequests(gt: {
|
|
12
|
+
baseURL: string;
|
|
13
|
+
apiKey: string;
|
|
14
|
+
}, requests: Request[]): Promise<Array<any | null>>;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.default = _bundleRequests;
|
|
13
|
+
const _translate_1 = require("./_translate");
|
|
14
|
+
/**
|
|
15
|
+
* Preprocesses the request based on its type.
|
|
16
|
+
* For 'intl' and 'translate' types, it processes the content and extracts arrays for further use.
|
|
17
|
+
* @param request - The request to be processed.
|
|
18
|
+
* @returns An object containing the processed request and any additional data.
|
|
19
|
+
*/
|
|
20
|
+
const _preprocess = (request) => {
|
|
21
|
+
const { type, data } = request;
|
|
22
|
+
let processedData = data;
|
|
23
|
+
let additional = {};
|
|
24
|
+
if (type === 'intl' || type === 'translate') {
|
|
25
|
+
const { array, excludedArray } = (0, _translate_1._process)(data.content);
|
|
26
|
+
processedData.content = array;
|
|
27
|
+
additional.excludedArray = excludedArray;
|
|
28
|
+
additional.type = type;
|
|
29
|
+
}
|
|
30
|
+
if (Object.keys(additional).length > 0) {
|
|
31
|
+
return {
|
|
32
|
+
processed: {
|
|
33
|
+
type: type,
|
|
34
|
+
data: processedData
|
|
35
|
+
},
|
|
36
|
+
additional: additional
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return { processed: { type: type, data: processedData } };
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Postprocesses the result based on additional data.
|
|
43
|
+
* If the type is 'translate' or 'intl', it recombines the excluded array with the translation.
|
|
44
|
+
* If there is an error, it attaches the error to the result.
|
|
45
|
+
* @param result - The result to be postprocessed.
|
|
46
|
+
* @param additional - Additional data from preprocessing.
|
|
47
|
+
* @returns The postprocessed result.
|
|
48
|
+
*/
|
|
49
|
+
const _postprocess = (result, additional) => {
|
|
50
|
+
if ((additional === null || additional === void 0 ? void 0 : additional.type) === "translate" || (additional === null || additional === void 0 ? void 0 : additional.type) === "intl") {
|
|
51
|
+
if (additional === null || additional === void 0 ? void 0 : additional.excludedArray) {
|
|
52
|
+
result.translation = (0, _translate_1._recombine)(result.translation, additional.excludedArray);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (additional === null || additional === void 0 ? void 0 : additional.error) {
|
|
56
|
+
if (!additional)
|
|
57
|
+
return null;
|
|
58
|
+
result.error = additional.error;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Bundles multiple requests and sends them to the server.
|
|
64
|
+
* @param gt - Contains the baseURL and apiKey for the server request.
|
|
65
|
+
* @param requests - Array of requests to be processed and sent.
|
|
66
|
+
* @returns A promise that resolves to an array of processed results.
|
|
67
|
+
*/
|
|
68
|
+
function _bundleRequests(gt, requests) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
let processedArray = [];
|
|
71
|
+
let additionalArray = [];
|
|
72
|
+
// Preprocess each request and store the processed request and additional data.
|
|
73
|
+
requests.forEach(request => {
|
|
74
|
+
const { processed, additional } = _preprocess(request);
|
|
75
|
+
processedArray.push(processed);
|
|
76
|
+
additionalArray.push(additional || {});
|
|
77
|
+
});
|
|
78
|
+
try {
|
|
79
|
+
// Send the processed requests to the server as a bundled request.
|
|
80
|
+
const response = yield fetch(`${gt.baseURL}/bundle`, {
|
|
81
|
+
method: 'POST',
|
|
82
|
+
headers: {
|
|
83
|
+
'Content-Type': 'application/json',
|
|
84
|
+
'gtx-api-key': gt.apiKey,
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify(processedArray)
|
|
87
|
+
});
|
|
88
|
+
// Check for response errors.
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
91
|
+
}
|
|
92
|
+
// Parse the response and postprocess each result.
|
|
93
|
+
const resultArray = yield response.json();
|
|
94
|
+
return resultArray.map((item, index) => _postprocess(item, additionalArray[index]));
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error(error);
|
|
98
|
+
// If there is an error, postprocess each request with the error information.
|
|
99
|
+
return processedArray.map((item, index) => _postprocess(item, Object.assign({ error: error }, additionalArray[index])));
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Content } from "./_translate";
|
|
2
|
+
/**
|
|
3
|
+
* Translates a single piece of content and caches for use in a public project.
|
|
4
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
5
|
+
* @param {Content} content - The content to translate.
|
|
6
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
7
|
+
* @param {string} projectID - The ID of the project
|
|
8
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
9
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
10
|
+
*/
|
|
11
|
+
export default function _intl(gt: {
|
|
12
|
+
baseURL: string;
|
|
13
|
+
apiKey: string;
|
|
14
|
+
}, content: Content, targetLanguage: string, projectID: string, metadata: {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
translation: string;
|
|
18
|
+
error?: Error | unknown;
|
|
19
|
+
}>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.default = _intl;
|
|
13
|
+
const _translate_1 = require("./_translate");
|
|
14
|
+
/**
|
|
15
|
+
* Translates a single piece of content and caches for use in a public project.
|
|
16
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
17
|
+
* @param {Content} content - The content to translate.
|
|
18
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
19
|
+
* @param {string} projectID - The ID of the project
|
|
20
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
21
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
22
|
+
*/
|
|
23
|
+
function _intl(gt, content, targetLanguage, projectID, metadata) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
const f = (array) => __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
const response = yield fetch(`${gt.baseURL}/intl`, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
headers: {
|
|
29
|
+
'Content-Type': 'application/json',
|
|
30
|
+
'gtx-api-key': gt.apiKey,
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify({
|
|
33
|
+
content: array,
|
|
34
|
+
targetLanguage: targetLanguage,
|
|
35
|
+
projectID: projectID,
|
|
36
|
+
metadata: metadata
|
|
37
|
+
})
|
|
38
|
+
});
|
|
39
|
+
if (!response.ok) {
|
|
40
|
+
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
41
|
+
}
|
|
42
|
+
const result = yield response.json();
|
|
43
|
+
return result.translation;
|
|
44
|
+
});
|
|
45
|
+
return (0, _translate_1._createTranslation)(content, f);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -1,22 +1,30 @@
|
|
|
1
|
-
type ContentObject = {
|
|
1
|
+
export type ContentObject = {
|
|
2
2
|
text: string;
|
|
3
3
|
exclude?: boolean;
|
|
4
4
|
cache?: boolean;
|
|
5
|
-
label?: string;
|
|
6
5
|
};
|
|
7
|
-
type ContentItem =
|
|
6
|
+
type ContentItem = ContentObject | string;
|
|
8
7
|
export type Content = ContentItem | ContentItem[];
|
|
9
|
-
export declare
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
export declare const _process: (content: Content) => {
|
|
9
|
+
array: ContentObject[];
|
|
10
|
+
excludedArray: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare const _recombine: (translatedContent: ContentObject[], excludedArray: string[]) => string;
|
|
13
|
+
export declare const _createTranslation: (content: Content, f: Function) => Promise<{
|
|
15
14
|
translation: string;
|
|
16
|
-
error
|
|
17
|
-
}
|
|
15
|
+
error?: undefined;
|
|
16
|
+
} | {
|
|
18
17
|
translation: string;
|
|
19
|
-
|
|
18
|
+
error: unknown;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* Translates a single piece of content.
|
|
22
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
23
|
+
* @param {Content} content - The content to translate.
|
|
24
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
25
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
26
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
27
|
+
*/
|
|
20
28
|
export default function _translate(gt: {
|
|
21
29
|
baseURL: string;
|
|
22
30
|
apiKey: string;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
// TYPE DEFINITIONS
|
|
2
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
5
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -9,56 +10,73 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
10
|
});
|
|
10
11
|
};
|
|
11
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
+
exports._createTranslation = exports._recombine = exports._process = void 0;
|
|
13
14
|
exports.default = _translate;
|
|
14
|
-
//
|
|
15
|
-
const
|
|
16
|
-
let
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
result += contentItem.text;
|
|
22
|
-
else if ((contentItem === null || contentItem === void 0 ? void 0 : contentItem.exclude) && redactedArray && redactedArray.length > 0) {
|
|
23
|
-
const redactedItem = redactedArray.shift();
|
|
24
|
-
if (redactedItem && (redactedItem === null || redactedItem === void 0 ? void 0 : redactedItem.text)) {
|
|
25
|
-
result += redactedItem.text;
|
|
26
|
-
}
|
|
15
|
+
// PROCESSING
|
|
16
|
+
const _process = (content) => {
|
|
17
|
+
let array = [];
|
|
18
|
+
let excludedArray = [];
|
|
19
|
+
const handleSingleItem = (contentItem) => {
|
|
20
|
+
if (typeof contentItem === 'string') {
|
|
21
|
+
array.push({ text: contentItem });
|
|
27
22
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let redactedArray = [];
|
|
35
|
-
const _handleItem = (contentItem) => {
|
|
36
|
-
if (typeof contentItem === 'string')
|
|
37
|
-
contentArray.push({
|
|
38
|
-
text: contentItem
|
|
39
|
-
});
|
|
40
|
-
else {
|
|
41
|
-
if (contentItem.exclude) {
|
|
42
|
-
contentArray.push(Object.assign(Object.assign({}, contentItem), { text: '', exclude: true }));
|
|
43
|
-
redactedArray.push(contentItem);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
contentArray.push(contentItem);
|
|
47
|
-
}
|
|
23
|
+
else if (typeof contentItem === 'object' && contentItem.exclude) {
|
|
24
|
+
array.push(Object.assign(Object.assign({}, contentItem), { text: '' }));
|
|
25
|
+
excludedArray.push(contentItem.text);
|
|
26
|
+
}
|
|
27
|
+
else if (typeof contentItem === 'object' && contentItem.text) {
|
|
28
|
+
array.push(contentItem);
|
|
48
29
|
}
|
|
49
30
|
};
|
|
50
31
|
if (Array.isArray(content))
|
|
51
|
-
content.
|
|
32
|
+
content.map(handleSingleItem);
|
|
52
33
|
else
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
contentArray, redactedArray
|
|
56
|
-
};
|
|
34
|
+
handleSingleItem(content);
|
|
35
|
+
return { array, excludedArray };
|
|
57
36
|
};
|
|
58
|
-
|
|
37
|
+
exports._process = _process;
|
|
38
|
+
const _recombine = (translatedContent, excludedArray) => {
|
|
39
|
+
let result = '';
|
|
40
|
+
for (const object of translatedContent) {
|
|
41
|
+
if (object.exclude && excludedArray.length < 0) {
|
|
42
|
+
result += excludedArray.shift();
|
|
43
|
+
}
|
|
44
|
+
else if (object.text) {
|
|
45
|
+
result += object.text;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
exports._recombine = _recombine;
|
|
51
|
+
// REQUEST
|
|
52
|
+
const _createTranslation = (content, f) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const { array, excludedArray } = (0, exports._process)(content);
|
|
54
|
+
try {
|
|
55
|
+
const result = yield f(content);
|
|
56
|
+
return {
|
|
57
|
+
translation: (0, exports._recombine)(result, excludedArray)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error(error);
|
|
62
|
+
return {
|
|
63
|
+
translation: (0, exports._recombine)(array, excludedArray),
|
|
64
|
+
error: error
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
exports._createTranslation = _createTranslation;
|
|
69
|
+
/**
|
|
70
|
+
* Translates a single piece of content.
|
|
71
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
72
|
+
* @param {Content} content - The content to translate.
|
|
73
|
+
* @param {string} targetLanguage - The target language for the translation.
|
|
74
|
+
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
75
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
76
|
+
*/
|
|
77
|
+
function _translate(gt, content, targetLanguage, metadata) {
|
|
59
78
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const
|
|
61
|
-
try {
|
|
79
|
+
const f = (array) => __awaiter(this, void 0, void 0, function* () {
|
|
62
80
|
const response = yield fetch(`${gt.baseURL}/text`, {
|
|
63
81
|
method: 'POST',
|
|
64
82
|
headers: {
|
|
@@ -66,7 +84,7 @@ function _translateMany(gt, array, targetLanguage, metadata) {
|
|
|
66
84
|
'gtx-api-key': gt.apiKey,
|
|
67
85
|
},
|
|
68
86
|
body: JSON.stringify({
|
|
69
|
-
|
|
87
|
+
content: array,
|
|
70
88
|
targetLanguage: targetLanguage,
|
|
71
89
|
metadata: metadata
|
|
72
90
|
})
|
|
@@ -74,27 +92,9 @@ function _translateMany(gt, array, targetLanguage, metadata) {
|
|
|
74
92
|
if (!response.ok) {
|
|
75
93
|
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
76
94
|
}
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
translation: _combine(item.translation, processed[index].redactedArray)
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
return finalArray;
|
|
85
|
-
}
|
|
86
|
-
catch (error) {
|
|
87
|
-
console.error(error);
|
|
88
|
-
return processed.map(item => ({
|
|
89
|
-
translation: _combine(item.contentArray, item.redactedArray),
|
|
90
|
-
error: error
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
function _translate(gt, content, targetLanguage, metadata) {
|
|
96
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
-
const finalArray = yield _translateMany(gt, [content], targetLanguage, metadata);
|
|
98
|
-
return finalArray[0];
|
|
95
|
+
const result = yield response.json();
|
|
96
|
+
return result.translation;
|
|
97
|
+
});
|
|
98
|
+
return (0, exports._createTranslation)(content, f);
|
|
99
99
|
});
|
|
100
100
|
}
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
|
12
12
|
*
|
|
13
13
|
**/
|
|
14
|
-
export default function
|
|
14
|
+
export default function _translateReactChildren(gt: {
|
|
15
15
|
baseURL: string;
|
|
16
16
|
apiKey: string;
|
|
17
17
|
}, content: any, targetLanguage: string, metadata: {
|
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.default =
|
|
12
|
+
exports.default = _translateReactChildren;
|
|
13
13
|
/**
|
|
14
14
|
* Translates the given content into the target language using a specified API.
|
|
15
15
|
*
|
|
@@ -23,7 +23,7 @@ exports.default = translateReactChildren;
|
|
|
23
23
|
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
|
24
24
|
*
|
|
25
25
|
**/
|
|
26
|
-
function
|
|
26
|
+
function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
|
27
27
|
return __awaiter(this, void 0, void 0, function* () {
|
|
28
28
|
try {
|
|
29
29
|
const response = yield fetch(`${gt.baseURL}/react`, {
|