generaltranslation 2.0.54 → 2.0.55
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/codes/codes.js +11 -12
- package/dist/cjs/codes/getLanguageDirection.js +4 -4
- package/dist/cjs/index.js +10 -29
- package/dist/cjs/translation/_bundleTranslation.js +28 -40
- package/dist/cjs/translation/_intl.js +33 -44
- package/dist/cjs/translation/_translate.js +33 -44
- package/dist/cjs/translation/_translateReactChildren.js +34 -45
- package/dist/cjs/translation/_updateRemoteDictionary.js +23 -34
- package/dist/mjs/codes/codes.js +10 -11
- package/dist/mjs/codes/getLanguageDirection.js +1 -1
- package/dist/mjs/index.js +10 -29
- package/dist/mjs/translation/_bundleTranslation.js +28 -40
- package/dist/mjs/translation/_intl.js +33 -44
- package/dist/mjs/translation/_translate.js +33 -44
- package/dist/mjs/translation/_translateReactChildren.js +34 -45
- package/dist/mjs/translation/_updateRemoteDictionary.js +23 -34
- package/package.json +5 -5
package/dist/cjs/codes/codes.js
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
// ----- IMPORTS ----- //
|
3
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
4
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
5
4
|
};
|
@@ -9,6 +8,7 @@ exports._standardizeLanguageCode = _standardizeLanguageCode;
|
|
9
8
|
exports._isValidLanguageCode = _isValidLanguageCode;
|
10
9
|
exports._getLanguageObject = _getLanguageObject;
|
11
10
|
exports._isSameLanguage = _isSameLanguage;
|
11
|
+
// ----- IMPORTS ----- //
|
12
12
|
// Import modules for mapping ISO 639 codes to language names and vice versa
|
13
13
|
const CodeToLanguage_json_1 = __importDefault(require("./639-1/CodeToLanguage.json"));
|
14
14
|
const LanguageToCode_json_1 = __importDefault(require("./639-1/LanguageToCode.json"));
|
@@ -31,7 +31,6 @@ const RegionToCode = RegionToCode_json_1.default;
|
|
31
31
|
const CodeToRegion = CodeToRegion_json_1.default;
|
32
32
|
// Import predefined common regions
|
33
33
|
const Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
|
34
|
-
const generaltranslation_1 = require("generaltranslation");
|
35
34
|
const Predefined = Predefined_json_1.default;
|
36
35
|
// ----- VALIDITY CHECKS ----- //
|
37
36
|
/**
|
@@ -41,7 +40,7 @@ const Predefined = Predefined_json_1.default;
|
|
41
40
|
* @internal
|
42
41
|
*/
|
43
42
|
function _standardizeLanguageCode(code) {
|
44
|
-
if (!(
|
43
|
+
if (!_isValidLanguageCode(code))
|
45
44
|
return '';
|
46
45
|
const codeParts = code.split('-');
|
47
46
|
let result = `${codeParts[0].toLowerCase()}`;
|
@@ -104,11 +103,11 @@ const _capitalize = (code) => {
|
|
104
103
|
* @returns {string} The language name.
|
105
104
|
*/
|
106
105
|
const _mapCodeToLanguage = (code) => {
|
107
|
-
code = code
|
108
|
-
if (
|
106
|
+
code = code?.toLowerCase();
|
107
|
+
if (code?.length === 2) {
|
109
108
|
return CodeToLanguage[code];
|
110
109
|
}
|
111
|
-
if (
|
110
|
+
if (code?.length === 3) {
|
112
111
|
return CodeToLanguageTriletter[code];
|
113
112
|
}
|
114
113
|
return '';
|
@@ -120,7 +119,7 @@ const _mapCodeToLanguage = (code) => {
|
|
120
119
|
* @returns {string} BCP 47 language tag.
|
121
120
|
*/
|
122
121
|
const _mapLanguageToCode = (language) => {
|
123
|
-
language = language
|
122
|
+
language = language?.toLowerCase();
|
124
123
|
return LanguageToCode[language] || LanguageToCodeTriletter[language] || '';
|
125
124
|
};
|
126
125
|
/**
|
@@ -138,7 +137,7 @@ const _mapCodeToScript = (code) => {
|
|
138
137
|
* @returns {string} The ISO 15924 code.
|
139
138
|
*/
|
140
139
|
const _mapScriptToCode = (script) => {
|
141
|
-
script = script
|
140
|
+
script = script?.toLowerCase();
|
142
141
|
return ScriptToCode[script] || '';
|
143
142
|
};
|
144
143
|
/**
|
@@ -147,7 +146,7 @@ const _mapScriptToCode = (script) => {
|
|
147
146
|
* @returns {string} The region name.
|
148
147
|
*/
|
149
148
|
const _mapCodeToRegion = (code) => {
|
150
|
-
code = code
|
149
|
+
code = code?.toUpperCase();
|
151
150
|
return CodeToRegion[code] || '';
|
152
151
|
};
|
153
152
|
/**
|
@@ -156,7 +155,7 @@ const _mapCodeToRegion = (code) => {
|
|
156
155
|
* @returns {string} The ISO 3166 code.
|
157
156
|
*/
|
158
157
|
const _mapRegionToCode = (region) => {
|
159
|
-
region = region
|
158
|
+
region = region?.toLowerCase();
|
160
159
|
return RegionToCode[region] || '';
|
161
160
|
};
|
162
161
|
/**
|
@@ -171,7 +170,7 @@ function _getLanguageObject(codes) {
|
|
171
170
|
* @returns {LanguageObject|null} The language object.
|
172
171
|
*/
|
173
172
|
const _handleGetLanguageObject = (code) => {
|
174
|
-
if (!(
|
173
|
+
if (!_isValidLanguageCode(code))
|
175
174
|
return null;
|
176
175
|
const codeParts = code.split('-');
|
177
176
|
let languageObject = {
|
@@ -280,7 +279,7 @@ const _handleGetLanguageCodeFromString = (language) => {
|
|
280
279
|
* @returns {string} The language code.
|
281
280
|
*/
|
282
281
|
const _handleGetLanguageCodeFromObject = (languageObject) => {
|
283
|
-
if (!
|
282
|
+
if (!languageObject?.language)
|
284
283
|
return '';
|
285
284
|
let code = languageObject.language.toLowerCase();
|
286
285
|
if (languageObject.script) {
|
@@ -50,7 +50,7 @@ const rtlLanguages = [
|
|
50
50
|
'yi' /* 'ייִדיש', Yiddish */
|
51
51
|
];
|
52
52
|
/* ----- END YAHOO COPYRIGHT ----- */
|
53
|
-
const
|
53
|
+
const codes_js_1 = require("./codes.js");
|
54
54
|
/**
|
55
55
|
* Get the text direction for a given language code.
|
56
56
|
*
|
@@ -60,12 +60,12 @@ const codes_1 = require("./codes");
|
|
60
60
|
*/
|
61
61
|
function _getLanguageDirection(code) {
|
62
62
|
return rtlLanguages.some(language => {
|
63
|
-
const lo = (0,
|
64
|
-
const clo = (0,
|
63
|
+
const lo = (0, codes_js_1._getLanguageObject)(language);
|
64
|
+
const clo = (0, codes_js_1._getLanguageObject)(code);
|
65
65
|
if (!lo || !clo)
|
66
66
|
return false;
|
67
67
|
if (lo.script && clo.script && lo.script !== clo.script)
|
68
68
|
return false;
|
69
|
-
return (0,
|
69
|
+
return (0, codes_js_1._isSameLanguage)(code, language);
|
70
70
|
}) ? 'rtl' : 'ltr';
|
71
71
|
}
|
package/dist/cjs/index.js
CHANGED
@@ -1,15 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
// `generaltranslation` language toolkit
|
3
3
|
// © 2024, General Translation, Inc.
|
4
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
5
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
6
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
7
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
8
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
9
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
10
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
11
|
-
});
|
12
|
-
};
|
13
4
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
5
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
6
|
};
|
@@ -60,10 +51,8 @@ class GT {
|
|
60
51
|
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
61
52
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
62
53
|
*/
|
63
|
-
translate(content, targetLanguage, metadata) {
|
64
|
-
return
|
65
|
-
return yield (0, _translate_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
66
|
-
});
|
54
|
+
async translate(content, targetLanguage, metadata) {
|
55
|
+
return await (0, _translate_1.default)(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
67
56
|
}
|
68
57
|
/**
|
69
58
|
* Translates a string and caches for use in a public project.
|
@@ -73,10 +62,8 @@ class GT {
|
|
73
62
|
* @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
74
63
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
75
64
|
*/
|
76
|
-
intl(content, targetLanguage, projectID, metadata) {
|
77
|
-
return
|
78
|
-
return yield (0, _intl_1.default)(this, content, targetLanguage, projectID || this.projectID, Object.assign({ projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
79
|
-
});
|
65
|
+
async intl(content, targetLanguage, projectID, metadata) {
|
66
|
+
return await (0, _intl_1.default)(this, content, targetLanguage, projectID || this.projectID, { projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
80
67
|
}
|
81
68
|
/**
|
82
69
|
* Translates the content of React children elements.
|
@@ -88,20 +75,16 @@ class GT {
|
|
88
75
|
*
|
89
76
|
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
90
77
|
*/
|
91
|
-
translateReactChildren(content, targetLanguage, metadata) {
|
92
|
-
return
|
93
|
-
return yield (0, _translateReactChildren_1.default)(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
94
|
-
});
|
78
|
+
async translateReactChildren(content, targetLanguage, metadata) {
|
79
|
+
return await (0, _translateReactChildren_1.default)(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
95
80
|
}
|
96
81
|
/**
|
97
82
|
* Bundles multiple translation requests and sends them to the server.
|
98
83
|
* @param requests - Array of requests to be processed and sent.
|
99
84
|
* @returns A promise that resolves to an array of processed results.
|
100
85
|
*/
|
101
|
-
bundleTranslation(requests) {
|
102
|
-
return
|
103
|
-
return (0, _bundleTranslation_1.default)(this, requests);
|
104
|
-
});
|
86
|
+
async bundleTranslation(requests) {
|
87
|
+
return (0, _bundleTranslation_1.default)(this, requests);
|
105
88
|
}
|
106
89
|
/**
|
107
90
|
* Pushes updates to a remotely cached translation dictionary.
|
@@ -111,10 +94,8 @@ class GT {
|
|
111
94
|
* @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
|
112
95
|
* @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
|
113
96
|
*/
|
114
|
-
updateRemoteDictionary(
|
115
|
-
return
|
116
|
-
return (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace);
|
117
|
-
});
|
97
|
+
async updateRemoteDictionary(updates, languages = [], projectID = this.projectID, replace = false) {
|
98
|
+
return (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace);
|
118
99
|
}
|
119
100
|
}
|
120
101
|
// ----- EXPORTS ----- //
|
@@ -1,13 +1,4 @@
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.default = _bundleTranslation;
|
13
4
|
/**
|
@@ -18,37 +9,34 @@ exports.default = _bundleTranslation;
|
|
18
9
|
* @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
|
19
10
|
* @internal
|
20
11
|
*/
|
21
|
-
function _bundleTranslation(gt, requests) {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
12
|
+
async function _bundleTranslation(gt, requests) {
|
13
|
+
const controller = new AbortController();
|
14
|
+
const signal = controller.signal;
|
15
|
+
if (requests[0]?.data?.metadata?.timeout) {
|
16
|
+
setTimeout(() => controller.abort(), requests[0].data.metadata.timeout);
|
17
|
+
}
|
18
|
+
try {
|
19
|
+
const response = await fetch(`${gt.baseURL}/bundle`, {
|
20
|
+
method: 'POST',
|
21
|
+
headers: {
|
22
|
+
'Content-Type': 'application/json',
|
23
|
+
'gtx-api-key': gt.apiKey,
|
24
|
+
},
|
25
|
+
body: JSON.stringify(requests),
|
26
|
+
signal
|
27
|
+
});
|
28
|
+
if (!response.ok) {
|
29
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
28
30
|
}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
body: JSON.stringify(requests),
|
37
|
-
signal
|
38
|
-
});
|
39
|
-
if (!response.ok) {
|
40
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
41
|
-
}
|
42
|
-
const resultArray = yield response.json();
|
43
|
-
return resultArray;
|
31
|
+
const resultArray = await response.json();
|
32
|
+
return resultArray;
|
33
|
+
}
|
34
|
+
catch (error) {
|
35
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
36
|
+
console.error('Request timed out');
|
37
|
+
return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
|
44
38
|
}
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
|
49
|
-
}
|
50
|
-
console.error(error);
|
51
|
-
return Array.from(requests, () => ({ translation: null, error: error }));
|
52
|
-
}
|
53
|
-
});
|
39
|
+
console.error(error);
|
40
|
+
return Array.from(requests, () => ({ translation: null, error: error }));
|
41
|
+
}
|
54
42
|
}
|
@@ -1,13 +1,4 @@
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.default = _translate;
|
13
4
|
/**
|
@@ -20,44 +11,42 @@ exports.default = _translate;
|
|
20
11
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
21
12
|
* @internal
|
22
13
|
*/
|
23
|
-
function _translate(gt, content, targetLanguage, projectID, metadata) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
async function _translate(gt, content, targetLanguage, projectID, metadata) {
|
15
|
+
const controller = new AbortController();
|
16
|
+
const signal = controller.signal;
|
17
|
+
if (metadata.timeout) {
|
18
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
19
|
+
}
|
20
|
+
try {
|
21
|
+
const response = await fetch(`${gt.baseURL}/translate`, {
|
22
|
+
method: 'POST',
|
23
|
+
headers: {
|
24
|
+
'Content-Type': 'application/json',
|
25
|
+
'gtx-api-key': gt.apiKey,
|
26
|
+
},
|
27
|
+
body: JSON.stringify({
|
28
|
+
content, targetLanguage, projectID, metadata
|
29
|
+
}),
|
30
|
+
signal
|
31
|
+
});
|
32
|
+
if (!response.ok) {
|
33
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
29
34
|
}
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
},
|
37
|
-
body: JSON.stringify({
|
38
|
-
content, targetLanguage, projectID, metadata
|
39
|
-
}),
|
40
|
-
signal
|
41
|
-
});
|
42
|
-
if (!response.ok) {
|
43
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
44
|
-
}
|
45
|
-
const result = yield response.json();
|
46
|
-
return result;
|
47
|
-
}
|
48
|
-
catch (error) {
|
49
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
50
|
-
console.error('Request timed out');
|
51
|
-
return {
|
52
|
-
translation: content,
|
53
|
-
error: 'Request timed out'
|
54
|
-
};
|
55
|
-
}
|
56
|
-
console.error(error);
|
35
|
+
const result = await response.json();
|
36
|
+
return result;
|
37
|
+
}
|
38
|
+
catch (error) {
|
39
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
40
|
+
console.error('Request timed out');
|
57
41
|
return {
|
58
42
|
translation: content,
|
59
|
-
error:
|
43
|
+
error: 'Request timed out'
|
60
44
|
};
|
61
45
|
}
|
62
|
-
|
46
|
+
console.error(error);
|
47
|
+
return {
|
48
|
+
translation: content,
|
49
|
+
error: error
|
50
|
+
};
|
51
|
+
}
|
63
52
|
}
|
@@ -1,13 +1,4 @@
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.default = _translate;
|
13
4
|
/**
|
@@ -19,44 +10,42 @@ exports.default = _translate;
|
|
19
10
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
20
11
|
* @internal
|
21
12
|
*/
|
22
|
-
function _translate(gt, content, targetLanguage, metadata) {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
13
|
+
async function _translate(gt, content, targetLanguage, metadata) {
|
14
|
+
const controller = new AbortController();
|
15
|
+
const signal = controller.signal;
|
16
|
+
if (metadata.timeout) {
|
17
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
18
|
+
}
|
19
|
+
try {
|
20
|
+
const response = await fetch(`${gt.baseURL}/translate`, {
|
21
|
+
method: 'POST',
|
22
|
+
headers: {
|
23
|
+
'Content-Type': 'application/json',
|
24
|
+
'gtx-api-key': gt.apiKey,
|
25
|
+
},
|
26
|
+
body: JSON.stringify({
|
27
|
+
content, targetLanguage, metadata
|
28
|
+
}),
|
29
|
+
signal
|
30
|
+
});
|
31
|
+
if (!response.ok) {
|
32
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
28
33
|
}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
},
|
36
|
-
body: JSON.stringify({
|
37
|
-
content, targetLanguage, metadata
|
38
|
-
}),
|
39
|
-
signal
|
40
|
-
});
|
41
|
-
if (!response.ok) {
|
42
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
43
|
-
}
|
44
|
-
const result = yield response.json();
|
45
|
-
return result;
|
46
|
-
}
|
47
|
-
catch (error) {
|
48
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
49
|
-
console.error('Request timed out');
|
50
|
-
return {
|
51
|
-
translation: content,
|
52
|
-
error: 'Request timed out'
|
53
|
-
};
|
54
|
-
}
|
55
|
-
console.error(error);
|
34
|
+
const result = await response.json();
|
35
|
+
return result;
|
36
|
+
}
|
37
|
+
catch (error) {
|
38
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
39
|
+
console.error('Request timed out');
|
56
40
|
return {
|
57
41
|
translation: content,
|
58
|
-
error:
|
42
|
+
error: 'Request timed out'
|
59
43
|
};
|
60
44
|
}
|
61
|
-
|
45
|
+
console.error(error);
|
46
|
+
return {
|
47
|
+
translation: content,
|
48
|
+
error: error
|
49
|
+
};
|
50
|
+
}
|
62
51
|
}
|
@@ -1,13 +1,4 @@
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.default = _translateReactChildren;
|
13
4
|
/**
|
@@ -23,45 +14,43 @@ exports.default = _translateReactChildren;
|
|
23
14
|
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
24
15
|
* @internal
|
25
16
|
**/
|
26
|
-
function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
17
|
+
async function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
18
|
+
const controller = new AbortController();
|
19
|
+
const signal = controller.signal;
|
20
|
+
if (metadata.timeout) {
|
21
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
22
|
+
}
|
23
|
+
try {
|
24
|
+
const response = await fetch(`${gt.baseURL}/react`, {
|
25
|
+
method: 'POST',
|
26
|
+
headers: {
|
27
|
+
'Content-Type': 'application/json',
|
28
|
+
'gtx-api-key': gt.apiKey,
|
29
|
+
},
|
30
|
+
body: JSON.stringify({
|
31
|
+
content: content,
|
32
|
+
targetLanguage: targetLanguage,
|
33
|
+
metadata: metadata
|
34
|
+
}),
|
35
|
+
signal
|
36
|
+
});
|
37
|
+
if (!response.ok) {
|
38
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
32
39
|
}
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
'gtx-api-key': gt.apiKey,
|
39
|
-
},
|
40
|
-
body: JSON.stringify({
|
41
|
-
content: content,
|
42
|
-
targetLanguage: targetLanguage,
|
43
|
-
metadata: metadata
|
44
|
-
}),
|
45
|
-
signal
|
46
|
-
});
|
47
|
-
if (!response.ok) {
|
48
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
49
|
-
}
|
50
|
-
return yield response.json();
|
51
|
-
}
|
52
|
-
catch (error) {
|
53
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
54
|
-
console.error('Request timed out');
|
55
|
-
return {
|
56
|
-
translation: null,
|
57
|
-
error: 'Request timed out'
|
58
|
-
};
|
59
|
-
}
|
60
|
-
console.error(error);
|
40
|
+
return await response.json();
|
41
|
+
}
|
42
|
+
catch (error) {
|
43
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
44
|
+
console.error('Request timed out');
|
61
45
|
return {
|
62
46
|
translation: null,
|
63
|
-
error:
|
47
|
+
error: 'Request timed out'
|
64
48
|
};
|
65
49
|
}
|
66
|
-
|
50
|
+
console.error(error);
|
51
|
+
return {
|
52
|
+
translation: null,
|
53
|
+
error: error
|
54
|
+
};
|
55
|
+
}
|
67
56
|
}
|
@@ -1,41 +1,30 @@
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
3
|
exports.default = _updateRemoteDictionary;
|
13
|
-
function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
28
|
-
}
|
29
|
-
const result = yield response.json();
|
30
|
-
return result.languages;
|
4
|
+
async function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
|
5
|
+
try {
|
6
|
+
const response = await fetch(`${gt.baseURL}/update`, {
|
7
|
+
method: 'POST',
|
8
|
+
headers: {
|
9
|
+
'Content-Type': 'application/json',
|
10
|
+
'gtx-api-key': gt.apiKey,
|
11
|
+
},
|
12
|
+
body: JSON.stringify({
|
13
|
+
updates, languages, projectID, replace
|
14
|
+
})
|
15
|
+
});
|
16
|
+
if (!response.ok) {
|
17
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
31
18
|
}
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
console.error(
|
19
|
+
const result = await response.json();
|
20
|
+
return result.languages;
|
21
|
+
}
|
22
|
+
catch (error) {
|
23
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
24
|
+
console.error('Request timed out');
|
38
25
|
return [];
|
39
26
|
}
|
40
|
-
|
27
|
+
console.error(error);
|
28
|
+
return [];
|
29
|
+
}
|
41
30
|
}
|
package/dist/mjs/codes/codes.js
CHANGED
@@ -21,7 +21,6 @@ const RegionToCode = RegionToCodeJSON;
|
|
21
21
|
const CodeToRegion = CodeToRegionJSON;
|
22
22
|
// Import predefined common regions
|
23
23
|
import PredefinedJSON from './predefined/Predefined.json';
|
24
|
-
import { isValidLanguageCode } from 'generaltranslation';
|
25
24
|
const Predefined = PredefinedJSON;
|
26
25
|
// ----- VALIDITY CHECKS ----- //
|
27
26
|
/**
|
@@ -31,7 +30,7 @@ const Predefined = PredefinedJSON;
|
|
31
30
|
* @internal
|
32
31
|
*/
|
33
32
|
export function _standardizeLanguageCode(code) {
|
34
|
-
if (!
|
33
|
+
if (!_isValidLanguageCode(code))
|
35
34
|
return '';
|
36
35
|
const codeParts = code.split('-');
|
37
36
|
let result = `${codeParts[0].toLowerCase()}`;
|
@@ -94,11 +93,11 @@ const _capitalize = (code) => {
|
|
94
93
|
* @returns {string} The language name.
|
95
94
|
*/
|
96
95
|
const _mapCodeToLanguage = (code) => {
|
97
|
-
code = code
|
98
|
-
if (
|
96
|
+
code = code?.toLowerCase();
|
97
|
+
if (code?.length === 2) {
|
99
98
|
return CodeToLanguage[code];
|
100
99
|
}
|
101
|
-
if (
|
100
|
+
if (code?.length === 3) {
|
102
101
|
return CodeToLanguageTriletter[code];
|
103
102
|
}
|
104
103
|
return '';
|
@@ -110,7 +109,7 @@ const _mapCodeToLanguage = (code) => {
|
|
110
109
|
* @returns {string} BCP 47 language tag.
|
111
110
|
*/
|
112
111
|
const _mapLanguageToCode = (language) => {
|
113
|
-
language = language
|
112
|
+
language = language?.toLowerCase();
|
114
113
|
return LanguageToCode[language] || LanguageToCodeTriletter[language] || '';
|
115
114
|
};
|
116
115
|
/**
|
@@ -128,7 +127,7 @@ const _mapCodeToScript = (code) => {
|
|
128
127
|
* @returns {string} The ISO 15924 code.
|
129
128
|
*/
|
130
129
|
const _mapScriptToCode = (script) => {
|
131
|
-
script = script
|
130
|
+
script = script?.toLowerCase();
|
132
131
|
return ScriptToCode[script] || '';
|
133
132
|
};
|
134
133
|
/**
|
@@ -137,7 +136,7 @@ const _mapScriptToCode = (script) => {
|
|
137
136
|
* @returns {string} The region name.
|
138
137
|
*/
|
139
138
|
const _mapCodeToRegion = (code) => {
|
140
|
-
code = code
|
139
|
+
code = code?.toUpperCase();
|
141
140
|
return CodeToRegion[code] || '';
|
142
141
|
};
|
143
142
|
/**
|
@@ -146,7 +145,7 @@ const _mapCodeToRegion = (code) => {
|
|
146
145
|
* @returns {string} The ISO 3166 code.
|
147
146
|
*/
|
148
147
|
const _mapRegionToCode = (region) => {
|
149
|
-
region = region
|
148
|
+
region = region?.toLowerCase();
|
150
149
|
return RegionToCode[region] || '';
|
151
150
|
};
|
152
151
|
/**
|
@@ -161,7 +160,7 @@ export function _getLanguageObject(codes) {
|
|
161
160
|
* @returns {LanguageObject|null} The language object.
|
162
161
|
*/
|
163
162
|
const _handleGetLanguageObject = (code) => {
|
164
|
-
if (!
|
163
|
+
if (!_isValidLanguageCode(code))
|
165
164
|
return null;
|
166
165
|
const codeParts = code.split('-');
|
167
166
|
let languageObject = {
|
@@ -268,7 +267,7 @@ const _handleGetLanguageCodeFromString = (language) => {
|
|
268
267
|
* @returns {string} The language code.
|
269
268
|
*/
|
270
269
|
const _handleGetLanguageCodeFromObject = (languageObject) => {
|
271
|
-
if (!
|
270
|
+
if (!languageObject?.language)
|
272
271
|
return '';
|
273
272
|
let code = languageObject.language.toLowerCase();
|
274
273
|
if (languageObject.script) {
|
@@ -47,7 +47,7 @@ const rtlLanguages = [
|
|
47
47
|
'yi' /* 'ייִדיש', Yiddish */
|
48
48
|
];
|
49
49
|
/* ----- END YAHOO COPYRIGHT ----- */
|
50
|
-
import { _getLanguageObject, _isSameLanguage } from "./codes";
|
50
|
+
import { _getLanguageObject, _isSameLanguage } from "./codes.js";
|
51
51
|
/**
|
52
52
|
* Get the text direction for a given language code.
|
53
53
|
*
|
package/dist/mjs/index.js
CHANGED
@@ -1,14 +1,5 @@
|
|
1
1
|
// `generaltranslation` language toolkit
|
2
2
|
// © 2024, General Translation, Inc.
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
10
|
-
});
|
11
|
-
};
|
12
3
|
// ----- IMPORTS ----- //
|
13
4
|
import { _isValidLanguageCode, _standardizeLanguageCode, _getLanguageObject, _getLanguageCode, _getLanguageName, _isSameLanguage } from './codes/codes';
|
14
5
|
import _getLanguageDirection from './codes/getLanguageDirection';
|
@@ -50,10 +41,8 @@ class GT {
|
|
50
41
|
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
51
42
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
52
43
|
*/
|
53
|
-
translate(content, targetLanguage, metadata) {
|
54
|
-
return
|
55
|
-
return yield _translate(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
56
|
-
});
|
44
|
+
async translate(content, targetLanguage, metadata) {
|
45
|
+
return await _translate(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
57
46
|
}
|
58
47
|
/**
|
59
48
|
* Translates a string and caches for use in a public project.
|
@@ -63,10 +52,8 @@ class GT {
|
|
63
52
|
* @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
64
53
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
65
54
|
*/
|
66
|
-
intl(content, targetLanguage, projectID, metadata) {
|
67
|
-
return
|
68
|
-
return yield _intl(this, content, targetLanguage, projectID || this.projectID, Object.assign({ projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
69
|
-
});
|
55
|
+
async intl(content, targetLanguage, projectID, metadata) {
|
56
|
+
return await _intl(this, content, targetLanguage, projectID || this.projectID, { projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
70
57
|
}
|
71
58
|
/**
|
72
59
|
* Translates the content of React children elements.
|
@@ -78,20 +65,16 @@ class GT {
|
|
78
65
|
*
|
79
66
|
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
80
67
|
*/
|
81
|
-
translateReactChildren(content, targetLanguage, metadata) {
|
82
|
-
return
|
83
|
-
return yield _translateReactChildren(this, content, targetLanguage, Object.assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata));
|
84
|
-
});
|
68
|
+
async translateReactChildren(content, targetLanguage, metadata) {
|
69
|
+
return await _translateReactChildren(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
85
70
|
}
|
86
71
|
/**
|
87
72
|
* Bundles multiple translation requests and sends them to the server.
|
88
73
|
* @param requests - Array of requests to be processed and sent.
|
89
74
|
* @returns A promise that resolves to an array of processed results.
|
90
75
|
*/
|
91
|
-
bundleTranslation(requests) {
|
92
|
-
return
|
93
|
-
return _bundleTranslation(this, requests);
|
94
|
-
});
|
76
|
+
async bundleTranslation(requests) {
|
77
|
+
return _bundleTranslation(this, requests);
|
95
78
|
}
|
96
79
|
/**
|
97
80
|
* Pushes updates to a remotely cached translation dictionary.
|
@@ -101,10 +84,8 @@ class GT {
|
|
101
84
|
* @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
|
102
85
|
* @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
|
103
86
|
*/
|
104
|
-
updateRemoteDictionary(
|
105
|
-
return
|
106
|
-
return _updateRemoteDictionary(this, updates, languages, projectID, replace);
|
107
|
-
});
|
87
|
+
async updateRemoteDictionary(updates, languages = [], projectID = this.projectID, replace = false) {
|
88
|
+
return _updateRemoteDictionary(this, updates, languages, projectID, replace);
|
108
89
|
}
|
109
90
|
}
|
110
91
|
// ----- EXPORTS ----- //
|
@@ -1,12 +1,3 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
1
|
/**
|
11
2
|
* Bundles multiple requests and sends them to the server.
|
12
3
|
* @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
|
@@ -15,37 +6,34 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
15
6
|
* @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
|
16
7
|
* @internal
|
17
8
|
*/
|
18
|
-
export default function _bundleTranslation(gt, requests) {
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
9
|
+
export default async function _bundleTranslation(gt, requests) {
|
10
|
+
const controller = new AbortController();
|
11
|
+
const signal = controller.signal;
|
12
|
+
if (requests[0]?.data?.metadata?.timeout) {
|
13
|
+
setTimeout(() => controller.abort(), requests[0].data.metadata.timeout);
|
14
|
+
}
|
15
|
+
try {
|
16
|
+
const response = await fetch(`${gt.baseURL}/bundle`, {
|
17
|
+
method: 'POST',
|
18
|
+
headers: {
|
19
|
+
'Content-Type': 'application/json',
|
20
|
+
'gtx-api-key': gt.apiKey,
|
21
|
+
},
|
22
|
+
body: JSON.stringify(requests),
|
23
|
+
signal
|
24
|
+
});
|
25
|
+
if (!response.ok) {
|
26
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
25
27
|
}
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
body: JSON.stringify(requests),
|
34
|
-
signal
|
35
|
-
});
|
36
|
-
if (!response.ok) {
|
37
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
38
|
-
}
|
39
|
-
const resultArray = yield response.json();
|
40
|
-
return resultArray;
|
28
|
+
const resultArray = await response.json();
|
29
|
+
return resultArray;
|
30
|
+
}
|
31
|
+
catch (error) {
|
32
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
33
|
+
console.error('Request timed out');
|
34
|
+
return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
|
41
35
|
}
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
return Array.from(requests, () => ({ translation: null, error: 'Request timed out' }));
|
46
|
-
}
|
47
|
-
console.error(error);
|
48
|
-
return Array.from(requests, () => ({ translation: null, error: error }));
|
49
|
-
}
|
50
|
-
});
|
36
|
+
console.error(error);
|
37
|
+
return Array.from(requests, () => ({ translation: null, error: error }));
|
38
|
+
}
|
51
39
|
}
|
@@ -1,12 +1,3 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
1
|
/**
|
11
2
|
* Translates a single piece of content and caches for use in a public project.
|
12
3
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
@@ -17,44 +8,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
17
8
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
18
9
|
* @internal
|
19
10
|
*/
|
20
|
-
export default function _translate(gt, content, targetLanguage, projectID, metadata) {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
11
|
+
export default async function _translate(gt, content, targetLanguage, projectID, metadata) {
|
12
|
+
const controller = new AbortController();
|
13
|
+
const signal = controller.signal;
|
14
|
+
if (metadata.timeout) {
|
15
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
16
|
+
}
|
17
|
+
try {
|
18
|
+
const response = await fetch(`${gt.baseURL}/translate`, {
|
19
|
+
method: 'POST',
|
20
|
+
headers: {
|
21
|
+
'Content-Type': 'application/json',
|
22
|
+
'gtx-api-key': gt.apiKey,
|
23
|
+
},
|
24
|
+
body: JSON.stringify({
|
25
|
+
content, targetLanguage, projectID, metadata
|
26
|
+
}),
|
27
|
+
signal
|
28
|
+
});
|
29
|
+
if (!response.ok) {
|
30
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
26
31
|
}
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
},
|
34
|
-
body: JSON.stringify({
|
35
|
-
content, targetLanguage, projectID, metadata
|
36
|
-
}),
|
37
|
-
signal
|
38
|
-
});
|
39
|
-
if (!response.ok) {
|
40
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
41
|
-
}
|
42
|
-
const result = yield response.json();
|
43
|
-
return result;
|
44
|
-
}
|
45
|
-
catch (error) {
|
46
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
47
|
-
console.error('Request timed out');
|
48
|
-
return {
|
49
|
-
translation: content,
|
50
|
-
error: 'Request timed out'
|
51
|
-
};
|
52
|
-
}
|
53
|
-
console.error(error);
|
32
|
+
const result = await response.json();
|
33
|
+
return result;
|
34
|
+
}
|
35
|
+
catch (error) {
|
36
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
37
|
+
console.error('Request timed out');
|
54
38
|
return {
|
55
39
|
translation: content,
|
56
|
-
error:
|
40
|
+
error: 'Request timed out'
|
57
41
|
};
|
58
42
|
}
|
59
|
-
|
43
|
+
console.error(error);
|
44
|
+
return {
|
45
|
+
translation: content,
|
46
|
+
error: error
|
47
|
+
};
|
48
|
+
}
|
60
49
|
}
|
@@ -1,12 +1,3 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
1
|
/**
|
11
2
|
* Translates a string.
|
12
3
|
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
@@ -16,44 +7,42 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
16
7
|
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
17
8
|
* @internal
|
18
9
|
*/
|
19
|
-
export default function _translate(gt, content, targetLanguage, metadata) {
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
10
|
+
export default async function _translate(gt, content, targetLanguage, metadata) {
|
11
|
+
const controller = new AbortController();
|
12
|
+
const signal = controller.signal;
|
13
|
+
if (metadata.timeout) {
|
14
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
15
|
+
}
|
16
|
+
try {
|
17
|
+
const response = await fetch(`${gt.baseURL}/translate`, {
|
18
|
+
method: 'POST',
|
19
|
+
headers: {
|
20
|
+
'Content-Type': 'application/json',
|
21
|
+
'gtx-api-key': gt.apiKey,
|
22
|
+
},
|
23
|
+
body: JSON.stringify({
|
24
|
+
content, targetLanguage, metadata
|
25
|
+
}),
|
26
|
+
signal
|
27
|
+
});
|
28
|
+
if (!response.ok) {
|
29
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
25
30
|
}
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
},
|
33
|
-
body: JSON.stringify({
|
34
|
-
content, targetLanguage, metadata
|
35
|
-
}),
|
36
|
-
signal
|
37
|
-
});
|
38
|
-
if (!response.ok) {
|
39
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
40
|
-
}
|
41
|
-
const result = yield response.json();
|
42
|
-
return result;
|
43
|
-
}
|
44
|
-
catch (error) {
|
45
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
46
|
-
console.error('Request timed out');
|
47
|
-
return {
|
48
|
-
translation: content,
|
49
|
-
error: 'Request timed out'
|
50
|
-
};
|
51
|
-
}
|
52
|
-
console.error(error);
|
31
|
+
const result = await response.json();
|
32
|
+
return result;
|
33
|
+
}
|
34
|
+
catch (error) {
|
35
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
36
|
+
console.error('Request timed out');
|
53
37
|
return {
|
54
38
|
translation: content,
|
55
|
-
error:
|
39
|
+
error: 'Request timed out'
|
56
40
|
};
|
57
41
|
}
|
58
|
-
|
42
|
+
console.error(error);
|
43
|
+
return {
|
44
|
+
translation: content,
|
45
|
+
error: error
|
46
|
+
};
|
47
|
+
}
|
59
48
|
}
|
@@ -1,12 +1,3 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
1
|
/**
|
11
2
|
* Translates the given content into the target language using a specified API.
|
12
3
|
*
|
@@ -20,45 +11,43 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
20
11
|
* @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
|
21
12
|
* @internal
|
22
13
|
**/
|
23
|
-
export default function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
14
|
+
export default async function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
15
|
+
const controller = new AbortController();
|
16
|
+
const signal = controller.signal;
|
17
|
+
if (metadata.timeout) {
|
18
|
+
setTimeout(() => controller.abort(), metadata.timeout);
|
19
|
+
}
|
20
|
+
try {
|
21
|
+
const response = await fetch(`${gt.baseURL}/react`, {
|
22
|
+
method: 'POST',
|
23
|
+
headers: {
|
24
|
+
'Content-Type': 'application/json',
|
25
|
+
'gtx-api-key': gt.apiKey,
|
26
|
+
},
|
27
|
+
body: JSON.stringify({
|
28
|
+
content: content,
|
29
|
+
targetLanguage: targetLanguage,
|
30
|
+
metadata: metadata
|
31
|
+
}),
|
32
|
+
signal
|
33
|
+
});
|
34
|
+
if (!response.ok) {
|
35
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
29
36
|
}
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
'gtx-api-key': gt.apiKey,
|
36
|
-
},
|
37
|
-
body: JSON.stringify({
|
38
|
-
content: content,
|
39
|
-
targetLanguage: targetLanguage,
|
40
|
-
metadata: metadata
|
41
|
-
}),
|
42
|
-
signal
|
43
|
-
});
|
44
|
-
if (!response.ok) {
|
45
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
46
|
-
}
|
47
|
-
return yield response.json();
|
48
|
-
}
|
49
|
-
catch (error) {
|
50
|
-
if (error instanceof Error && error.name === 'AbortError') {
|
51
|
-
console.error('Request timed out');
|
52
|
-
return {
|
53
|
-
translation: null,
|
54
|
-
error: 'Request timed out'
|
55
|
-
};
|
56
|
-
}
|
57
|
-
console.error(error);
|
37
|
+
return await response.json();
|
38
|
+
}
|
39
|
+
catch (error) {
|
40
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
41
|
+
console.error('Request timed out');
|
58
42
|
return {
|
59
43
|
translation: null,
|
60
|
-
error:
|
44
|
+
error: 'Request timed out'
|
61
45
|
};
|
62
46
|
}
|
63
|
-
|
47
|
+
console.error(error);
|
48
|
+
return {
|
49
|
+
translation: null,
|
50
|
+
error: error
|
51
|
+
};
|
52
|
+
}
|
64
53
|
}
|
@@ -1,38 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
headers: {
|
16
|
-
'Content-Type': 'application/json',
|
17
|
-
'gtx-api-key': gt.apiKey,
|
18
|
-
},
|
19
|
-
body: JSON.stringify({
|
20
|
-
updates, languages, projectID, replace
|
21
|
-
})
|
22
|
-
});
|
23
|
-
if (!response.ok) {
|
24
|
-
throw new Error(`${response.status}: ${yield response.text()}`);
|
25
|
-
}
|
26
|
-
const result = yield response.json();
|
27
|
-
return result.languages;
|
1
|
+
export default async function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
|
2
|
+
try {
|
3
|
+
const response = await fetch(`${gt.baseURL}/update`, {
|
4
|
+
method: 'POST',
|
5
|
+
headers: {
|
6
|
+
'Content-Type': 'application/json',
|
7
|
+
'gtx-api-key': gt.apiKey,
|
8
|
+
},
|
9
|
+
body: JSON.stringify({
|
10
|
+
updates, languages, projectID, replace
|
11
|
+
})
|
12
|
+
});
|
13
|
+
if (!response.ok) {
|
14
|
+
throw new Error(`${response.status}: ${await response.text()}`);
|
28
15
|
}
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
console.error(
|
16
|
+
const result = await response.json();
|
17
|
+
return result.languages;
|
18
|
+
}
|
19
|
+
catch (error) {
|
20
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
21
|
+
console.error('Request timed out');
|
35
22
|
return [];
|
36
23
|
}
|
37
|
-
|
24
|
+
console.error(error);
|
25
|
+
return [];
|
26
|
+
}
|
38
27
|
}
|
package/package.json
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
{
|
2
2
|
"name": "generaltranslation",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.55",
|
4
4
|
"description": "A language toolkit for AI developers",
|
5
|
-
"main": "dist/
|
6
|
-
"types": "dist/
|
5
|
+
"main": "dist/cjs/index.js",
|
6
|
+
"types": "dist/cjs/index.d.ts",
|
7
7
|
"files": [
|
8
8
|
"dist"
|
9
9
|
],
|
10
10
|
"scripts": {
|
11
|
-
"transpile": "tsc; tsc --project tsconfig.cjs.json;",
|
11
|
+
"transpile": "rm -r -f dist; tsc; tsc --project tsconfig.cjs.json;",
|
12
12
|
"prepublishOnly": "npm run transpile"
|
13
13
|
},
|
14
14
|
"exports": {
|
15
15
|
".": {
|
16
|
-
"import": "./dist/
|
16
|
+
"import": "./dist/cjs/index.js",
|
17
17
|
"require": "./dist/cjs/index.js",
|
18
18
|
"types": "./dist/mjs/index.d.ts"
|
19
19
|
}
|