generaltranslation 2.0.10 → 2.0.11
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 +12 -10
- package/dist/index.js +7 -7
- package/dist/translation/_bundleRequests.js +3 -62
- package/dist/translation/_intl.d.ts +6 -5
- package/dist/translation/_intl.js +16 -14
- package/dist/translation/_translate.d.ts +5 -24
- package/dist/translation/_translate.js +15 -67
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { _getLanguageObject, _isSameLanguage } from './codes/codes';
|
|
2
|
-
import { Content } from './translation/_translate';
|
|
3
2
|
/**
|
|
4
3
|
* Type representing the constructor parameters for the GT class.
|
|
5
4
|
*/
|
|
@@ -28,27 +27,30 @@ declare class GT {
|
|
|
28
27
|
*/
|
|
29
28
|
constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
|
|
30
29
|
/**
|
|
31
|
-
* Translates a string
|
|
32
|
-
* @param {
|
|
30
|
+
* Translates a string into a target language.
|
|
31
|
+
* @param {string} content - A string to translate.
|
|
33
32
|
* @param {string} targetLanguage - The target language for the translation.
|
|
34
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
33
|
+
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
35
34
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
36
35
|
*/
|
|
37
|
-
translate(content:
|
|
36
|
+
translate(content: string, targetLanguage: string, metadata?: {
|
|
37
|
+
notes?: string;
|
|
38
38
|
[key: string]: any;
|
|
39
39
|
}): Promise<{
|
|
40
40
|
translation: string;
|
|
41
41
|
error?: Error | unknown;
|
|
42
42
|
}>;
|
|
43
43
|
/**
|
|
44
|
-
* Translates a
|
|
45
|
-
* @param {
|
|
44
|
+
* Translates a string and caches for use in a public project.
|
|
45
|
+
* @param {string} content - A string to translate.
|
|
46
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.
|
|
47
|
+
* @param {string} projectID - The ID of the project.
|
|
48
|
+
* @param {{ page?: string, notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
49
49
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
|
50
50
|
*/
|
|
51
|
-
intl(content:
|
|
51
|
+
intl(content: string, targetLanguage: string, projectID?: string, metadata?: {
|
|
52
|
+
page?: string;
|
|
53
|
+
notes?: string;
|
|
52
54
|
[key: string]: any;
|
|
53
55
|
}): Promise<{
|
|
54
56
|
translation: string;
|
package/dist/index.js
CHANGED
|
@@ -51,10 +51,10 @@ class GT {
|
|
|
51
51
|
this.baseURL = baseURL;
|
|
52
52
|
}
|
|
53
53
|
/**
|
|
54
|
-
* Translates a string
|
|
55
|
-
* @param {
|
|
54
|
+
* Translates a string into a target language.
|
|
55
|
+
* @param {string} content - A string to translate.
|
|
56
56
|
* @param {string} targetLanguage - The target language for the translation.
|
|
57
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
57
|
+
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
58
58
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
59
59
|
*/
|
|
60
60
|
translate(content, targetLanguage, metadata) {
|
|
@@ -63,11 +63,11 @@ class GT {
|
|
|
63
63
|
});
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
|
-
* Translates a
|
|
67
|
-
* @param {
|
|
66
|
+
* Translates a string and caches for use in a public project.
|
|
67
|
+
* @param {string} content - A string to translate.
|
|
68
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.
|
|
69
|
+
* @param {string} projectID - The ID of the project.
|
|
70
|
+
* @param {{ page?: string, notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
71
71
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
|
72
72
|
*/
|
|
73
73
|
intl(content, targetLanguage, projectID, metadata) {
|
|
@@ -10,55 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
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
13
|
/**
|
|
63
14
|
* Bundles multiple requests and sends them to the server.
|
|
64
15
|
* @param gt - Contains the baseURL and apiKey for the server request.
|
|
@@ -67,14 +18,6 @@ const _postprocess = (result, additional) => {
|
|
|
67
18
|
*/
|
|
68
19
|
function _bundleRequests(gt, requests) {
|
|
69
20
|
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
21
|
try {
|
|
79
22
|
// Send the processed requests to the server as a bundled request.
|
|
80
23
|
const response = yield fetch(`${gt.baseURL}/bundle`, {
|
|
@@ -83,20 +26,18 @@ function _bundleRequests(gt, requests) {
|
|
|
83
26
|
'Content-Type': 'application/json',
|
|
84
27
|
'gtx-api-key': gt.apiKey,
|
|
85
28
|
},
|
|
86
|
-
body: JSON.stringify(
|
|
29
|
+
body: JSON.stringify(requests)
|
|
87
30
|
});
|
|
88
31
|
// Check for response errors.
|
|
89
32
|
if (!response.ok) {
|
|
90
33
|
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
91
34
|
}
|
|
92
|
-
// Parse the response and postprocess each result.
|
|
93
35
|
const resultArray = yield response.json();
|
|
94
|
-
return resultArray
|
|
36
|
+
return resultArray;
|
|
95
37
|
}
|
|
96
38
|
catch (error) {
|
|
97
39
|
console.error(error);
|
|
98
|
-
|
|
99
|
-
return processedArray.map((item, index) => _postprocess(item, Object.assign({ error: error }, additionalArray[index])));
|
|
40
|
+
return Array.from(requests, () => ({ translation: null, error: error }));
|
|
100
41
|
}
|
|
101
42
|
});
|
|
102
43
|
}
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import { Content } from "./_translate";
|
|
2
1
|
/**
|
|
3
2
|
* Translates a single piece of content and caches for use in a public project.
|
|
4
3
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
5
|
-
* @param {
|
|
4
|
+
* @param {string} content - The content to translate.
|
|
6
5
|
* @param {string} targetLanguage - The target language for the translation.
|
|
7
6
|
* @param {string} projectID - The ID of the project
|
|
8
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
7
|
+
* @param {{ page?: string, notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
9
8
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
10
9
|
*/
|
|
11
|
-
export default function
|
|
10
|
+
export default function _translate(gt: {
|
|
12
11
|
baseURL: string;
|
|
13
12
|
apiKey: string;
|
|
14
|
-
}, content:
|
|
13
|
+
}, content: string, targetLanguage: string, projectID: string, metadata: {
|
|
14
|
+
page?: string;
|
|
15
|
+
notes?: string;
|
|
15
16
|
[key: string]: any;
|
|
16
17
|
}): Promise<{
|
|
17
18
|
translation: string;
|
|
@@ -9,39 +9,41 @@ 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 =
|
|
13
|
-
const _translate_1 = require("./_translate");
|
|
12
|
+
exports.default = _translate;
|
|
14
13
|
/**
|
|
15
14
|
* Translates a single piece of content and caches for use in a public project.
|
|
16
15
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
17
|
-
* @param {
|
|
16
|
+
* @param {string} content - The content to translate.
|
|
18
17
|
* @param {string} targetLanguage - The target language for the translation.
|
|
19
18
|
* @param {string} projectID - The ID of the project
|
|
20
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
19
|
+
* @param {{ page?: string, notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
21
20
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
22
21
|
*/
|
|
23
|
-
function
|
|
22
|
+
function _translate(gt, content, targetLanguage, projectID, metadata) {
|
|
24
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
-
|
|
26
|
-
const response = yield fetch(`${gt.baseURL}/
|
|
24
|
+
try {
|
|
25
|
+
const response = yield fetch(`${gt.baseURL}/translate`, {
|
|
27
26
|
method: 'POST',
|
|
28
27
|
headers: {
|
|
29
28
|
'Content-Type': 'application/json',
|
|
30
29
|
'gtx-api-key': gt.apiKey,
|
|
31
30
|
},
|
|
32
31
|
body: JSON.stringify({
|
|
33
|
-
content
|
|
34
|
-
targetLanguage: targetLanguage,
|
|
35
|
-
projectID: projectID,
|
|
36
|
-
metadata: metadata
|
|
32
|
+
content, targetLanguage, projectID, metadata
|
|
37
33
|
})
|
|
38
34
|
});
|
|
39
35
|
if (!response.ok) {
|
|
40
36
|
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
41
37
|
}
|
|
42
38
|
const result = yield response.json();
|
|
43
|
-
return result
|
|
44
|
-
}
|
|
45
|
-
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error(error);
|
|
43
|
+
return {
|
|
44
|
+
translation: content,
|
|
45
|
+
error: error
|
|
46
|
+
};
|
|
47
|
+
}
|
|
46
48
|
});
|
|
47
49
|
}
|
|
@@ -1,37 +1,18 @@
|
|
|
1
|
-
export type ContentObject = {
|
|
2
|
-
text: string;
|
|
3
|
-
exclude?: boolean;
|
|
4
|
-
cache?: boolean;
|
|
5
|
-
};
|
|
6
|
-
type ContentItem = ContentObject | string;
|
|
7
|
-
export type Content = ContentItem | ContentItem[];
|
|
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<{
|
|
14
|
-
translation: string;
|
|
15
|
-
error?: undefined;
|
|
16
|
-
} | {
|
|
17
|
-
translation: string;
|
|
18
|
-
error: unknown;
|
|
19
|
-
}>;
|
|
20
1
|
/**
|
|
21
|
-
* Translates a
|
|
2
|
+
* Translates a string.
|
|
22
3
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
23
|
-
* @param {
|
|
4
|
+
* @param {string} content - The content to translate.
|
|
24
5
|
* @param {string} targetLanguage - The target language for the translation.
|
|
25
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
6
|
+
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
26
7
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
27
8
|
*/
|
|
28
9
|
export default function _translate(gt: {
|
|
29
10
|
baseURL: string;
|
|
30
11
|
apiKey: string;
|
|
31
|
-
}, content:
|
|
12
|
+
}, content: string, targetLanguage: string, metadata: {
|
|
13
|
+
notes?: string;
|
|
32
14
|
[key: string]: any;
|
|
33
15
|
}): Promise<{
|
|
34
16
|
translation: string;
|
|
35
17
|
error?: Error | unknown;
|
|
36
18
|
}>;
|
|
37
|
-
export {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// TYPE DEFINITIONS
|
|
3
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -10,91 +9,40 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
9
|
});
|
|
11
10
|
};
|
|
12
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports._createTranslation = exports._recombine = exports._process = void 0;
|
|
14
12
|
exports.default = _translate;
|
|
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 });
|
|
22
|
-
}
|
|
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);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
if (Array.isArray(content))
|
|
32
|
-
content.map(handleSingleItem);
|
|
33
|
-
else
|
|
34
|
-
handleSingleItem(content);
|
|
35
|
-
return { array, excludedArray };
|
|
36
|
-
};
|
|
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
13
|
/**
|
|
70
|
-
* Translates a
|
|
14
|
+
* Translates a string.
|
|
71
15
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
|
72
|
-
* @param {
|
|
16
|
+
* @param {string} content - The content to translate.
|
|
73
17
|
* @param {string} targetLanguage - The target language for the translation.
|
|
74
|
-
* @param {{ [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
18
|
+
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
|
75
19
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
|
76
20
|
*/
|
|
77
21
|
function _translate(gt, content, targetLanguage, metadata) {
|
|
78
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
|
|
80
|
-
const response = yield fetch(`${gt.baseURL}/
|
|
23
|
+
try {
|
|
24
|
+
const response = yield fetch(`${gt.baseURL}/translate`, {
|
|
81
25
|
method: 'POST',
|
|
82
26
|
headers: {
|
|
83
27
|
'Content-Type': 'application/json',
|
|
84
28
|
'gtx-api-key': gt.apiKey,
|
|
85
29
|
},
|
|
86
30
|
body: JSON.stringify({
|
|
87
|
-
content
|
|
88
|
-
targetLanguage: targetLanguage,
|
|
89
|
-
metadata: metadata
|
|
31
|
+
content, targetLanguage, metadata
|
|
90
32
|
})
|
|
91
33
|
});
|
|
92
34
|
if (!response.ok) {
|
|
93
35
|
throw new Error(`${response.status}: ${yield response.text()}`);
|
|
94
36
|
}
|
|
95
37
|
const result = yield response.json();
|
|
96
|
-
return result
|
|
97
|
-
}
|
|
98
|
-
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.error(error);
|
|
42
|
+
return {
|
|
43
|
+
translation: content,
|
|
44
|
+
error: error
|
|
45
|
+
};
|
|
46
|
+
}
|
|
99
47
|
});
|
|
100
48
|
}
|