generaltranslation 2.0.64 → 2.0.65
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/formatting/_string_content.d.ts +1 -0
- package/dist/formatting/_string_content.js +104 -0
- package/dist/index.d.ts +34 -18
- package/dist/index.js +32 -7
- package/dist/translation/dictionaries/_translateBundle.js +6 -21
- package/dist/translation/dictionaries/_updateProjectDictionary.js +14 -23
- package/dist/translation/react/_translateReact.js +6 -26
- package/dist/translation/strings/_translate.d.ts +1 -7
- package/dist/translation/strings/_translate.js +6 -26
- package/dist/types/types.d.ts +33 -2
- package/package.json +1 -1
- /package/dist/{format → formatting}/_format.d.ts +0 -0
- /package/dist/{format → formatting}/_format.js +0 -0
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,104 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
3
|
+
__assign = Object.assign || function(t) {
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
+
s = arguments[i];
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
+
t[p] = s[p];
|
8
|
+
}
|
9
|
+
return t;
|
10
|
+
};
|
11
|
+
return __assign.apply(this, arguments);
|
12
|
+
};
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
14
|
+
exports._splitStringToContent = _splitStringToContent;
|
15
|
+
exports._renderContentToString = _renderContentToString;
|
16
|
+
var _format_1 = require("./_format");
|
17
|
+
// Variable types mapping
|
18
|
+
var variableTypeMap = {
|
19
|
+
var: "variable",
|
20
|
+
num: "number",
|
21
|
+
datetime: "datetime",
|
22
|
+
currency: "currency"
|
23
|
+
};
|
24
|
+
/**
|
25
|
+
* @internal
|
26
|
+
*/
|
27
|
+
/**
|
28
|
+
* @internal
|
29
|
+
*/
|
30
|
+
function _splitStringToContent(string) {
|
31
|
+
var result = [];
|
32
|
+
var regex = /{([^}]+)}/g;
|
33
|
+
var lastIndex = 0;
|
34
|
+
var match;
|
35
|
+
while ((match = regex.exec(string)) !== null) {
|
36
|
+
var fullMatch = match[0], content = match[1];
|
37
|
+
var startIndex = match.index;
|
38
|
+
// Check for escaped braces with '^' right before the opening brace
|
39
|
+
if (string[startIndex - 1] === "^") {
|
40
|
+
// Add text before the escape sequence
|
41
|
+
if (startIndex - 1 > lastIndex) {
|
42
|
+
result.push(string.slice(lastIndex, startIndex - 1));
|
43
|
+
}
|
44
|
+
// Add the escaped content as literal text
|
45
|
+
result.push(fullMatch);
|
46
|
+
lastIndex = startIndex + fullMatch.length;
|
47
|
+
continue;
|
48
|
+
}
|
49
|
+
// Add text before the match
|
50
|
+
if (startIndex > lastIndex) {
|
51
|
+
result.push(string.slice(lastIndex, startIndex));
|
52
|
+
}
|
53
|
+
// Handle the variable substitution inside the braces
|
54
|
+
var parts = content.split(",").map(function (part) { return part.trim(); });
|
55
|
+
var key = parts[0];
|
56
|
+
var variableType = parts[1] ? variableTypeMap[parts[1]] : undefined;
|
57
|
+
var variableObject = __assign({ key: key }, (variableType && { variable: variableType }));
|
58
|
+
result.push(variableObject);
|
59
|
+
lastIndex = startIndex + fullMatch.length;
|
60
|
+
}
|
61
|
+
// Add the remaining part of the string after the last match
|
62
|
+
if (lastIndex < string.length) {
|
63
|
+
result.push(string.slice(lastIndex));
|
64
|
+
}
|
65
|
+
return result;
|
66
|
+
}
|
67
|
+
/**
|
68
|
+
* @internal
|
69
|
+
*/
|
70
|
+
function _renderContentToString(content, languages, variables, variableOptions) {
|
71
|
+
if (languages === void 0) { languages = 'en'; }
|
72
|
+
if (variables === void 0) { variables = {}; }
|
73
|
+
if (variableOptions === void 0) { variableOptions = {}; }
|
74
|
+
if (typeof content === 'string') {
|
75
|
+
content = _splitStringToContent(content);
|
76
|
+
}
|
77
|
+
if (typeof content === 'string') {
|
78
|
+
return content;
|
79
|
+
}
|
80
|
+
return content.map(function (item) {
|
81
|
+
var _a;
|
82
|
+
if (typeof item === 'string')
|
83
|
+
return item;
|
84
|
+
if (typeof item === 'object') {
|
85
|
+
var value = variables[item.key];
|
86
|
+
if (!item.variable)
|
87
|
+
return value;
|
88
|
+
if (item.variable === "number") {
|
89
|
+
return (0, _format_1._formatNum)({
|
90
|
+
value: value,
|
91
|
+
languages: languages,
|
92
|
+
options: variableOptions[item.key]
|
93
|
+
});
|
94
|
+
}
|
95
|
+
if (item.variable === "currency") {
|
96
|
+
return (0, _format_1._formatCurrency)(__assign(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })), (((_a = variableOptions[item.key]) === null || _a === void 0 ? void 0 : _a.currency) && { currency: variableOptions[item.key].currency })));
|
97
|
+
}
|
98
|
+
if (item.variable === "datetime") {
|
99
|
+
return (0, _format_1._formatDateTime)(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })));
|
100
|
+
}
|
101
|
+
return value;
|
102
|
+
}
|
103
|
+
}).join('');
|
104
|
+
}
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { LanguageObject } from './codes/codes';
|
2
|
-
import {
|
3
|
-
import { Content, Update, Request } from './types/types';
|
2
|
+
import { Content, Update, Request, ReactChildrenAsObject, ReactTranslationResult, ContentTranslationResult } from './types/types';
|
4
3
|
/**
|
5
4
|
* Type representing the constructor parameters for the GT class.
|
6
5
|
*/
|
@@ -30,48 +29,45 @@ declare class GT {
|
|
30
29
|
constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
|
31
30
|
/**
|
32
31
|
* Translates a string or an array of strings/variables into a target language.
|
33
|
-
* If `metadata.
|
32
|
+
* If `metadata.save` is provided, the translation is cached for use in a public project.
|
34
33
|
*
|
35
34
|
* @param {Content} content - The string or array of strings/variables to be translated.
|
36
35
|
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
37
|
-
* @param {{ context?: string,
|
36
|
+
* @param {{ context?: string, save?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
|
38
37
|
* @param {string} [metadata.context] - Contextual information to assist with the translation.
|
39
|
-
* @param {boolean} [metadata.
|
38
|
+
* @param {boolean} [metadata.save] - Whether to cache the translation for use in a public project.
|
40
39
|
*
|
41
|
-
* @returns {Promise<
|
40
|
+
* @returns {Promise<ContentTranslationResult>} A promise that resolves to the translated content, or an error if the translation fails.
|
42
41
|
*/
|
43
42
|
translate(content: Content, targetLanguage: string, metadata?: {
|
44
43
|
context?: string;
|
45
|
-
|
44
|
+
save?: boolean;
|
46
45
|
[key: string]: any;
|
47
46
|
}): Promise<{
|
48
|
-
translation:
|
49
|
-
|
47
|
+
translation: Content;
|
48
|
+
language: string;
|
50
49
|
}>;
|
51
50
|
/**
|
52
51
|
* Translates the content of React children elements.
|
53
52
|
*
|
54
53
|
* @param {Object} params - The parameters for the translation.
|
55
|
-
* @param {
|
54
|
+
* @param {ReactChildrenAsObject} params.children - The React children content to be translated.
|
56
55
|
* @param {string} params.targetLanguage - The target language for the translation.
|
57
56
|
* @param {Object} params.metadata - Additional metadata for the translation process.
|
58
57
|
*
|
59
|
-
* @returns {Promise<
|
58
|
+
* @returns {Promise<ReactTranslationResult>} - A promise that resolves to the translated content.
|
60
59
|
*/
|
61
|
-
translateReact(children:
|
60
|
+
translateReact(children: ReactChildrenAsObject, targetLanguage: string, metadata?: {
|
62
61
|
context?: string;
|
63
|
-
|
62
|
+
save?: boolean;
|
64
63
|
[key: string]: any;
|
65
|
-
}): Promise<
|
66
|
-
translation: any | null;
|
67
|
-
error?: Error | unknown;
|
68
|
-
}>;
|
64
|
+
}): Promise<ReactTranslationResult>;
|
69
65
|
/**
|
70
66
|
* Bundles multiple translation requests and sends them to the server.
|
71
67
|
* @param requests - Array of requests to be processed and sent.
|
72
68
|
* @returns A promise that resolves to an array of processed results.
|
73
69
|
*/
|
74
|
-
translateBundle(requests: Request[]): Promise<Array<
|
70
|
+
translateBundle(requests: Request[]): Promise<Array<ReactTranslationResult | ContentTranslationResult>>;
|
75
71
|
/**
|
76
72
|
* Pushes updates to a remotely cached translation dictionary.
|
77
73
|
* @param {Update[]} updates - Array of updates with optional targetLanguage.
|
@@ -184,3 +180,23 @@ export declare function formatCurrency({ value, languages, currency, options }:
|
|
184
180
|
currency?: string;
|
185
181
|
options?: Intl.NumberFormatOptions;
|
186
182
|
}): string;
|
183
|
+
/**
|
184
|
+
* Splits a string into an array of text and variable objects.
|
185
|
+
*
|
186
|
+
* @param {string} string - The input string to split.
|
187
|
+
* @returns {Content} - An array containing strings and VariableObjects.
|
188
|
+
*/
|
189
|
+
export declare function splitStringToContent(string: string): Content;
|
190
|
+
/**
|
191
|
+
* Renders content to a string by replacing variables with their formatted values.
|
192
|
+
*
|
193
|
+
* @param {Content} content - The content to render, which can be a string or an array of strings and VariableObjects.
|
194
|
+
* @param {string | string[]} [languages='en'] - The language(s) to use for formatting.
|
195
|
+
* @param {Record<string, any>} [variables={}] - An object containing variable values keyed by variable names.
|
196
|
+
* @param {Record<string, any>} [variableOptions={}] - An object containing options for formatting variables, keyed by variable names.
|
197
|
+
* @returns {string} - The rendered string with variables replaced by their formatted values.
|
198
|
+
*
|
199
|
+
*/
|
200
|
+
export declare function renderContentToString<V extends Record<string, any>>(content: Content, languages?: string | string[], variables?: V, variableOptions?: {
|
201
|
+
[key in keyof V]?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions;
|
202
|
+
}): string;
|
package/dist/index.js
CHANGED
@@ -60,6 +60,8 @@ exports.isSameLanguage = isSameLanguage;
|
|
60
60
|
exports.formatNum = formatNum;
|
61
61
|
exports.formatDateTime = formatDateTime;
|
62
62
|
exports.formatCurrency = formatCurrency;
|
63
|
+
exports.splitStringToContent = splitStringToContent;
|
64
|
+
exports.renderContentToString = renderContentToString;
|
63
65
|
// ----- IMPORTS ----- //
|
64
66
|
var codes_1 = require("./codes/codes");
|
65
67
|
var getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
|
@@ -67,7 +69,8 @@ var _translateBundle_1 = __importDefault(require("./translation/dictionaries/_tr
|
|
67
69
|
var _translate_1 = __importDefault(require("./translation/strings/_translate"));
|
68
70
|
var _translateReact_1 = __importDefault(require("./translation/react/_translateReact"));
|
69
71
|
var _updateProjectDictionary_1 = __importDefault(require("./translation/dictionaries/_updateProjectDictionary"));
|
70
|
-
var _format_1 = require("./
|
72
|
+
var _format_1 = require("./formatting/_format");
|
73
|
+
var _string_content_1 = require("./formatting/_string_content");
|
71
74
|
// ----- CORE CLASS ----- //
|
72
75
|
var getDefaultFromEnv = function (VARIABLE) {
|
73
76
|
if (typeof process !== 'undefined' && process.env) {
|
@@ -97,15 +100,15 @@ var GT = /** @class */ (function () {
|
|
97
100
|
}
|
98
101
|
/**
|
99
102
|
* Translates a string or an array of strings/variables into a target language.
|
100
|
-
* If `metadata.
|
103
|
+
* If `metadata.save` is provided, the translation is cached for use in a public project.
|
101
104
|
*
|
102
105
|
* @param {Content} content - The string or array of strings/variables to be translated.
|
103
106
|
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
104
|
-
* @param {{ context?: string,
|
107
|
+
* @param {{ context?: string, save?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
|
105
108
|
* @param {string} [metadata.context] - Contextual information to assist with the translation.
|
106
|
-
* @param {boolean} [metadata.
|
109
|
+
* @param {boolean} [metadata.save] - Whether to cache the translation for use in a public project.
|
107
110
|
*
|
108
|
-
* @returns {Promise<
|
111
|
+
* @returns {Promise<ContentTranslationResult>} A promise that resolves to the translated content, or an error if the translation fails.
|
109
112
|
*/
|
110
113
|
GT.prototype.translate = function (content, targetLanguage, metadata) {
|
111
114
|
return __awaiter(this, void 0, void 0, function () {
|
@@ -121,11 +124,11 @@ var GT = /** @class */ (function () {
|
|
121
124
|
* Translates the content of React children elements.
|
122
125
|
*
|
123
126
|
* @param {Object} params - The parameters for the translation.
|
124
|
-
* @param {
|
127
|
+
* @param {ReactChildrenAsObject} params.children - The React children content to be translated.
|
125
128
|
* @param {string} params.targetLanguage - The target language for the translation.
|
126
129
|
* @param {Object} params.metadata - Additional metadata for the translation process.
|
127
130
|
*
|
128
|
-
* @returns {Promise<
|
131
|
+
* @returns {Promise<ReactTranslationResult>} - A promise that resolves to the translated content.
|
129
132
|
*/
|
130
133
|
GT.prototype.translateReact = function (children, targetLanguage, metadata) {
|
131
134
|
return __awaiter(this, void 0, void 0, function () {
|
@@ -253,3 +256,25 @@ function formatCurrency(_a) {
|
|
253
256
|
var value = _a.value, languages = _a.languages, currency = _a.currency, options = _a.options;
|
254
257
|
return (0, _format_1._formatCurrency)({ value: value, languages: languages, currency: currency, options: options });
|
255
258
|
}
|
259
|
+
/**
|
260
|
+
* Splits a string into an array of text and variable objects.
|
261
|
+
*
|
262
|
+
* @param {string} string - The input string to split.
|
263
|
+
* @returns {Content} - An array containing strings and VariableObjects.
|
264
|
+
*/
|
265
|
+
function splitStringToContent(string) {
|
266
|
+
return (0, _string_content_1._splitStringToContent)(string);
|
267
|
+
}
|
268
|
+
/**
|
269
|
+
* Renders content to a string by replacing variables with their formatted values.
|
270
|
+
*
|
271
|
+
* @param {Content} content - The content to render, which can be a string or an array of strings and VariableObjects.
|
272
|
+
* @param {string | string[]} [languages='en'] - The language(s) to use for formatting.
|
273
|
+
* @param {Record<string, any>} [variables={}] - An object containing variable values keyed by variable names.
|
274
|
+
* @param {Record<string, any>} [variableOptions={}] - An object containing options for formatting variables, keyed by variable names.
|
275
|
+
* @returns {string} - The rendered string with variables replaced by their formatted values.
|
276
|
+
*
|
277
|
+
*/
|
278
|
+
function renderContentToString(content, languages, variables, variableOptions) {
|
279
|
+
return (0, _string_content_1._renderContentToString)(content, languages, variables, variableOptions);
|
280
|
+
}
|
@@ -38,16 +38,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
39
39
|
exports.default = _translateBundle;
|
40
40
|
/**
|
41
|
-
* Bundles multiple requests and sends them to the server.
|
42
|
-
* @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
|
43
|
-
* @param {Request[]} requests - Array of requests to be processed and sent.
|
44
|
-
* @param {{ timeout?: number }} options - Additional options for the request, including timeout.
|
45
|
-
* @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
|
46
41
|
* @internal
|
47
42
|
*/
|
48
43
|
function _translateBundle(gt, requests) {
|
49
44
|
return __awaiter(this, void 0, void 0, function () {
|
50
|
-
var controller, signal, response, _a, _b, _c, resultArray
|
45
|
+
var controller, signal, response, _a, _b, _c, resultArray;
|
51
46
|
var _d, _e, _f;
|
52
47
|
return __generator(this, function (_g) {
|
53
48
|
switch (_g.label) {
|
@@ -57,9 +52,6 @@ function _translateBundle(gt, requests) {
|
|
57
52
|
if ((_f = (_e = (_d = requests[0]) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.metadata) === null || _f === void 0 ? void 0 : _f.timeout) {
|
58
53
|
setTimeout(function () { return controller.abort(); }, requests[0].data.metadata.timeout);
|
59
54
|
}
|
60
|
-
_g.label = 1;
|
61
|
-
case 1:
|
62
|
-
_g.trys.push([1, 6, , 7]);
|
63
55
|
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/bundle"), {
|
64
56
|
method: 'POST',
|
65
57
|
headers: {
|
@@ -69,24 +61,17 @@ function _translateBundle(gt, requests) {
|
|
69
61
|
body: JSON.stringify(requests),
|
70
62
|
signal: signal
|
71
63
|
})];
|
72
|
-
case
|
64
|
+
case 1:
|
73
65
|
response = _g.sent();
|
74
|
-
if (!!response.ok) return [3 /*break*/,
|
66
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
75
67
|
_a = Error.bind;
|
76
68
|
_c = (_b = "".concat(response.status, ": ")).concat;
|
77
69
|
return [4 /*yield*/, response.text()];
|
78
|
-
case
|
79
|
-
case
|
80
|
-
case
|
70
|
+
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_g.sent()])]))();
|
71
|
+
case 3: return [4 /*yield*/, response.json()];
|
72
|
+
case 4:
|
81
73
|
resultArray = _g.sent();
|
82
74
|
return [2 /*return*/, resultArray];
|
83
|
-
case 6:
|
84
|
-
error_1 = _g.sent();
|
85
|
-
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
86
|
-
throw new Error('Error: Request timed out.');
|
87
|
-
}
|
88
|
-
throw new Error("".concat(error_1));
|
89
|
-
case 7: return [2 /*return*/];
|
90
75
|
}
|
91
76
|
});
|
92
77
|
});
|
@@ -42,24 +42,22 @@ exports.default = _updateProjectDictionary;
|
|
42
42
|
*/
|
43
43
|
function _updateProjectDictionary(gt, updates, languages, projectID, replace) {
|
44
44
|
return __awaiter(this, void 0, void 0, function () {
|
45
|
-
var response, _a, _b, _c, result
|
45
|
+
var response, _a, _b, _c, result;
|
46
46
|
return __generator(this, function (_d) {
|
47
47
|
switch (_d.label) {
|
48
|
-
case 0:
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
})
|
62
|
-
})];
|
48
|
+
case 0: return [4 /*yield*/, fetch("".concat(gt.baseURL, "/update"), {
|
49
|
+
method: 'POST',
|
50
|
+
headers: {
|
51
|
+
'Content-Type': 'application/json',
|
52
|
+
'gtx-api-key': gt.apiKey,
|
53
|
+
},
|
54
|
+
body: JSON.stringify({
|
55
|
+
updates: updates,
|
56
|
+
languages: languages,
|
57
|
+
projectID: projectID,
|
58
|
+
replace: replace
|
59
|
+
})
|
60
|
+
})];
|
63
61
|
case 1:
|
64
62
|
response = _d.sent();
|
65
63
|
if (!!response.ok) return [3 /*break*/, 3];
|
@@ -71,13 +69,6 @@ function _updateProjectDictionary(gt, updates, languages, projectID, replace) {
|
|
71
69
|
case 4:
|
72
70
|
result = _d.sent();
|
73
71
|
return [2 /*return*/, result.languages];
|
74
|
-
case 5:
|
75
|
-
error_1 = _d.sent();
|
76
|
-
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
77
|
-
throw new Error('Error: Request timed out.');
|
78
|
-
}
|
79
|
-
throw new Error("".concat(error_1));
|
80
|
-
case 6: return [2 /*return*/];
|
81
72
|
}
|
82
73
|
});
|
83
74
|
});
|
@@ -38,21 +38,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
39
39
|
exports.default = _translateReact;
|
40
40
|
/**
|
41
|
-
* Translates the given React children into the target language using a specified API.
|
42
|
-
*
|
43
|
-
* @param {{ baseURL: string, apiKey: string }} gt - An object containing baseURL and apiKey for the API.
|
44
|
-
* @param {any} children - The React children to be translated.
|
45
|
-
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
46
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata to be sent with the translation request.
|
47
|
-
*
|
48
|
-
* @returns {Promise<{ translation: any | null, error?: Error | unknown }>} - A promise that resolves to the translated content as an object, or null if an error occurs.
|
49
|
-
*
|
50
|
-
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
51
41
|
* @internal
|
52
42
|
**/
|
53
43
|
function _translateReact(gt, content, targetLanguage, metadata) {
|
54
44
|
return __awaiter(this, void 0, void 0, function () {
|
55
|
-
var controller, signal, response, _a, _b, _c
|
45
|
+
var controller, signal, response, _a, _b, _c;
|
56
46
|
return __generator(this, function (_d) {
|
57
47
|
switch (_d.label) {
|
58
48
|
case 0:
|
@@ -61,9 +51,6 @@ function _translateReact(gt, content, targetLanguage, metadata) {
|
|
61
51
|
if (metadata.timeout) {
|
62
52
|
setTimeout(function () { return controller.abort(); }, metadata.timeout);
|
63
53
|
}
|
64
|
-
_d.label = 1;
|
65
|
-
case 1:
|
66
|
-
_d.trys.push([1, 6, , 7]);
|
67
54
|
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/react"), {
|
68
55
|
method: 'POST',
|
69
56
|
headers: {
|
@@ -77,22 +64,15 @@ function _translateReact(gt, content, targetLanguage, metadata) {
|
|
77
64
|
}),
|
78
65
|
signal: signal
|
79
66
|
})];
|
80
|
-
case
|
67
|
+
case 1:
|
81
68
|
response = _d.sent();
|
82
|
-
if (!!response.ok) return [3 /*break*/,
|
69
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
83
70
|
_a = Error.bind;
|
84
71
|
_c = (_b = "".concat(response.status, ": ")).concat;
|
85
72
|
return [4 /*yield*/, response.text()];
|
86
|
-
case
|
87
|
-
case
|
88
|
-
case
|
89
|
-
case 6:
|
90
|
-
error_1 = _d.sent();
|
91
|
-
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
92
|
-
throw new Error('Error: Request timed out.');
|
93
|
-
}
|
94
|
-
throw new Error("".concat(error_1));
|
95
|
-
case 7: return [2 /*return*/];
|
73
|
+
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
74
|
+
case 3: return [4 /*yield*/, response.json()];
|
75
|
+
case 4: return [2 /*return*/, _d.sent()];
|
96
76
|
}
|
97
77
|
});
|
98
78
|
});
|
@@ -38,21 +38,11 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
39
39
|
exports.default = _translate;
|
40
40
|
/**
|
41
|
-
* Translates the given string into the target language using a specified API.
|
42
|
-
*
|
43
|
-
* @param {{ baseURL: string, apiKey: string }} gt - An object containing baseURL and apiKey for the API.
|
44
|
-
* @param {any} content - The string to be translated.
|
45
|
-
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
46
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata to be sent with the translation request.
|
47
|
-
*
|
48
|
-
* @returns {Promise<{ translation: any | null, error?: Error | unknown }>} - A promise that resolves to the translated content as an object, or null if an error occurs.
|
49
|
-
*
|
50
|
-
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
51
41
|
* @internal
|
52
42
|
**/
|
53
43
|
function _translate(gt, content, targetLanguage, metadata) {
|
54
44
|
return __awaiter(this, void 0, void 0, function () {
|
55
|
-
var controller, signal, response, _a, _b, _c, result
|
45
|
+
var controller, signal, response, _a, _b, _c, result;
|
56
46
|
return __generator(this, function (_d) {
|
57
47
|
switch (_d.label) {
|
58
48
|
case 0:
|
@@ -61,9 +51,6 @@ function _translate(gt, content, targetLanguage, metadata) {
|
|
61
51
|
if (metadata.timeout) {
|
62
52
|
setTimeout(function () { return controller.abort(); }, metadata.timeout);
|
63
53
|
}
|
64
|
-
_d.label = 1;
|
65
|
-
case 1:
|
66
|
-
_d.trys.push([1, 6, , 7]);
|
67
54
|
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/translate"), {
|
68
55
|
method: 'POST',
|
69
56
|
headers: {
|
@@ -77,24 +64,17 @@ function _translate(gt, content, targetLanguage, metadata) {
|
|
77
64
|
}),
|
78
65
|
signal: signal
|
79
66
|
})];
|
80
|
-
case
|
67
|
+
case 1:
|
81
68
|
response = _d.sent();
|
82
|
-
if (!!response.ok) return [3 /*break*/,
|
69
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
83
70
|
_a = Error.bind;
|
84
71
|
_c = (_b = "".concat(response.status, ": ")).concat;
|
85
72
|
return [4 /*yield*/, response.text()];
|
86
|
-
case
|
87
|
-
case
|
88
|
-
case
|
73
|
+
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
74
|
+
case 3: return [4 /*yield*/, response.json()];
|
75
|
+
case 4:
|
89
76
|
result = _d.sent();
|
90
77
|
return [2 /*return*/, result];
|
91
|
-
case 6:
|
92
|
-
error_1 = _d.sent();
|
93
|
-
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
94
|
-
throw new Error('Error: Request timed out.');
|
95
|
-
}
|
96
|
-
throw new Error("".concat(error_1));
|
97
|
-
case 7: return [2 /*return*/];
|
98
78
|
}
|
99
79
|
});
|
100
80
|
});
|
package/dist/types/types.d.ts
CHANGED
@@ -1,8 +1,21 @@
|
|
1
|
-
export type
|
1
|
+
export type VariableObject = {
|
2
2
|
variable?: string;
|
3
3
|
key: string;
|
4
4
|
};
|
5
|
-
export type Content = string | Array<string |
|
5
|
+
export type Content = string | Array<string | VariableObject>;
|
6
|
+
export type ElementAsObject = {
|
7
|
+
type: string;
|
8
|
+
props: {
|
9
|
+
'data-generaltranslation'?: {
|
10
|
+
id: number;
|
11
|
+
[key: string]: any;
|
12
|
+
};
|
13
|
+
children?: ReactChildrenAsObject;
|
14
|
+
[key: string]: any;
|
15
|
+
};
|
16
|
+
};
|
17
|
+
export type ReactChildAsObject = string | ElementAsObject | VariableObject;
|
18
|
+
export type ReactChildrenAsObject = ReactChildAsObject | ReactChildAsObject[];
|
6
19
|
export type Update = {
|
7
20
|
type: 'react';
|
8
21
|
data: {
|
@@ -31,3 +44,21 @@ export type Request = {
|
|
31
44
|
metadata: Record<string, any>;
|
32
45
|
};
|
33
46
|
};
|
47
|
+
export type ContentTranslationResult = {
|
48
|
+
translation: Content;
|
49
|
+
language: string;
|
50
|
+
reference?: {
|
51
|
+
id: string;
|
52
|
+
key: string;
|
53
|
+
dictionaryName: string;
|
54
|
+
};
|
55
|
+
};
|
56
|
+
export type ReactTranslationResult = {
|
57
|
+
translation: Content;
|
58
|
+
language: string;
|
59
|
+
reference?: {
|
60
|
+
id: string;
|
61
|
+
key: string;
|
62
|
+
dictionaryName: string;
|
63
|
+
};
|
64
|
+
};
|
package/package.json
CHANGED
File without changes
|
File without changes
|