generaltranslation 2.0.4 → 2.0.5
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/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _getLanguageObject, _isSameLanguage } from './codes/codes';
|
|
2
|
+
import { Content } from './translation/_translate';
|
|
2
3
|
/**
|
|
3
4
|
* Interface representing the constructor parameters for the GT class.
|
|
4
5
|
*/
|
|
@@ -26,6 +27,16 @@ declare class GT {
|
|
|
26
27
|
* @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
|
|
27
28
|
*/
|
|
28
29
|
constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
|
|
30
|
+
translate({ content, targetLanguage, metadata }: {
|
|
31
|
+
content: Content;
|
|
32
|
+
targetLanguage: string;
|
|
33
|
+
metadata: {
|
|
34
|
+
[key: string]: any;
|
|
35
|
+
};
|
|
36
|
+
}): Promise<{
|
|
37
|
+
translation: string;
|
|
38
|
+
error?: Error | unknown;
|
|
39
|
+
}>;
|
|
29
40
|
/**
|
|
30
41
|
* Translates the content of React children elements.
|
|
31
42
|
*
|
|
@@ -42,7 +53,10 @@ declare class GT {
|
|
|
42
53
|
metadata: {
|
|
43
54
|
[key: string]: any;
|
|
44
55
|
};
|
|
45
|
-
}): Promise<
|
|
56
|
+
}): Promise<{
|
|
57
|
+
translation: any | null;
|
|
58
|
+
error?: Error | unknown;
|
|
59
|
+
}>;
|
|
46
60
|
}
|
|
47
61
|
export default GT;
|
|
48
62
|
export declare const getLanguageObject: typeof _getLanguageObject;
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.isSameLanguage = exports.getLanguageName = exports.getLanguageCode = exports.getLanguageObject = void 0;
|
|
18
18
|
// ----- IMPORTS ----- //
|
|
19
19
|
const codes_1 = require("./codes/codes");
|
|
20
|
+
const _translate_1 = __importDefault(require("./translation/_translate"));
|
|
20
21
|
const _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
|
|
21
22
|
// TO DO
|
|
22
23
|
// - Translation API
|
|
@@ -48,6 +49,11 @@ class GT {
|
|
|
48
49
|
this.defaultLanguage = defaultLanguage.toLowerCase();
|
|
49
50
|
this.baseURL = baseURL;
|
|
50
51
|
}
|
|
52
|
+
translate(_a) {
|
|
53
|
+
return __awaiter(this, arguments, void 0, function* ({ content, targetLanguage, metadata }) {
|
|
54
|
+
return yield (0, _translate_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
51
57
|
/**
|
|
52
58
|
* Translates the content of React children elements.
|
|
53
59
|
*
|
|
@@ -60,7 +66,7 @@ class GT {
|
|
|
60
66
|
*/
|
|
61
67
|
translateReactChildren(_a) {
|
|
62
68
|
return __awaiter(this, arguments, void 0, function* ({ content, targetLanguage, metadata }) {
|
|
63
|
-
return yield _translateReactChildren_1.default
|
|
69
|
+
return yield (0, _translateReactChildren_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
|
64
70
|
});
|
|
65
71
|
}
|
|
66
72
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type ContentObject = {
|
|
2
|
+
text: string;
|
|
3
|
+
exclude?: boolean;
|
|
4
|
+
cache?: boolean;
|
|
5
|
+
label?: string;
|
|
6
|
+
};
|
|
7
|
+
type ContentItem = string | ContentObject;
|
|
8
|
+
export type Content = ContentItem | ContentItem[];
|
|
9
|
+
export default function _translate(gt: {
|
|
10
|
+
baseURL: string;
|
|
11
|
+
apiKey: string;
|
|
12
|
+
}, content: Content, targetLanguage: string, metadata: {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}): Promise<{
|
|
15
|
+
translation: string;
|
|
16
|
+
error?: Error | unknown;
|
|
17
|
+
}>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,89 @@
|
|
|
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 = _translate;
|
|
13
|
+
// also handles errors
|
|
14
|
+
const _combine = (contentArray, redactedArray) => {
|
|
15
|
+
let result = '';
|
|
16
|
+
const _handleItem = (contentItem) => {
|
|
17
|
+
if (typeof contentItem === 'string')
|
|
18
|
+
result += contentItem;
|
|
19
|
+
else if ((contentItem === null || contentItem === void 0 ? void 0 : contentItem.text) && typeof contentItem.text === 'string')
|
|
20
|
+
result += contentItem.text;
|
|
21
|
+
else if ((contentItem === null || contentItem === void 0 ? void 0 : contentItem.exclude) && redactedArray && redactedArray.length > 0) {
|
|
22
|
+
const redactedItem = redactedArray.shift();
|
|
23
|
+
if (redactedItem && (redactedItem === null || redactedItem === void 0 ? void 0 : redactedItem.text)) {
|
|
24
|
+
result += redactedItem.text;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
contentArray.forEach(item => _handleItem(item));
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
const _redact = (content) => {
|
|
32
|
+
let contentArray = [];
|
|
33
|
+
let redactedArray = [];
|
|
34
|
+
const _handleItem = (contentItem) => {
|
|
35
|
+
if (typeof contentItem === 'string')
|
|
36
|
+
contentArray.push({
|
|
37
|
+
text: contentItem
|
|
38
|
+
});
|
|
39
|
+
else {
|
|
40
|
+
if (contentItem.exclude) {
|
|
41
|
+
contentArray.push(Object.assign(Object.assign({}, contentItem), { text: '', exclude: true }));
|
|
42
|
+
redactedArray.push(Object.assign({}, contentItem));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
contentArray.push(contentItem);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
if (Array.isArray(content))
|
|
50
|
+
content.forEach(item => _handleItem(item));
|
|
51
|
+
else
|
|
52
|
+
_handleItem(content);
|
|
53
|
+
return {
|
|
54
|
+
contentArray, redactedArray
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
function _translate(gt, content, targetLanguage, metadata) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const { contentArray, redactedArray } = _redact(content);
|
|
60
|
+
try {
|
|
61
|
+
const response = yield fetch(`${gt.baseURL}/text`, {
|
|
62
|
+
method: 'POST',
|
|
63
|
+
headers: {
|
|
64
|
+
'Content-Type': 'application/json',
|
|
65
|
+
'gtx-api-key': gt.apiKey,
|
|
66
|
+
},
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
content: content,
|
|
69
|
+
targetLanguage: targetLanguage,
|
|
70
|
+
metadata: metadata
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
if (!response.ok) {
|
|
74
|
+
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
75
|
+
}
|
|
76
|
+
const resultArray = yield response.json();
|
|
77
|
+
return {
|
|
78
|
+
translation: _combine(resultArray, redactedArray)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.error(error);
|
|
83
|
+
return {
|
|
84
|
+
translation: _combine(contentArray, redactedArray),
|
|
85
|
+
error: error
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Translates the given content into the target language using a specified API.
|
|
3
3
|
*
|
|
4
|
-
* @param {Object}
|
|
5
|
-
* @param {
|
|
4
|
+
* @param {Object} gt - An object containing baseURL and apiKey for the API.
|
|
5
|
+
* @param {JSON} content - The content to be translated. This can be of any type.
|
|
6
6
|
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
|
7
7
|
* @param {Object} metadata - Additional metadata to be sent with the translation request.
|
|
8
8
|
*
|
|
9
|
-
* @returns {Promise<
|
|
9
|
+
* @returns {Promise<any | null>} - A promise that resolves to the translated content as an object, or null if an error occurs.
|
|
10
10
|
*
|
|
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: {
|
|
18
18
|
[key: string]: any;
|
|
19
|
-
}): Promise<
|
|
19
|
+
}): Promise<{
|
|
20
|
+
translation: any | null;
|
|
21
|
+
error?: Error | unknown;
|
|
22
|
+
}>;
|
|
@@ -9,28 +9,28 @@ 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
|
*
|
|
16
|
-
* @param {Object}
|
|
17
|
-
* @param {
|
|
16
|
+
* @param {Object} gt - An object containing baseURL and apiKey for the API.
|
|
17
|
+
* @param {JSON} content - The content to be translated. This can be of any type.
|
|
18
18
|
* @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
|
|
19
19
|
* @param {Object} metadata - Additional metadata to be sent with the translation request.
|
|
20
20
|
*
|
|
21
|
-
* @returns {Promise<
|
|
21
|
+
* @returns {Promise<any | null>} - A promise that resolves to the translated content as an object, or null if an error occurs.
|
|
22
22
|
*
|
|
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
|
-
const response = yield fetch(`${
|
|
29
|
+
const response = yield fetch(`${gt.baseURL}/react`, {
|
|
30
30
|
method: 'POST',
|
|
31
31
|
headers: {
|
|
32
32
|
'Content-Type': 'application/json',
|
|
33
|
-
'gtx-api-key':
|
|
33
|
+
'gtx-api-key': gt.apiKey,
|
|
34
34
|
},
|
|
35
35
|
body: JSON.stringify({
|
|
36
36
|
content: content,
|
|
@@ -45,7 +45,10 @@ function _translateReactChildren(content, targetLanguage, metadata) {
|
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
47
|
console.error(error);
|
|
48
|
-
return
|
|
48
|
+
return {
|
|
49
|
+
translation: null,
|
|
50
|
+
error: error
|
|
51
|
+
};
|
|
49
52
|
}
|
|
50
53
|
});
|
|
51
54
|
}
|