generaltranslation 2.0.55 → 2.0.56
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/{cjs/codes → codes}/codes.js +71 -67
- package/dist/{cjs/codes → codes}/getLanguageDirection.js +5 -5
- package/dist/index.js +228 -0
- package/dist/translation/_bundleTranslation.js +95 -0
- package/dist/translation/_intl.js +107 -0
- package/dist/translation/_translate.js +105 -0
- package/dist/translation/_translateReactChildren.js +107 -0
- package/dist/translation/_updateRemoteDictionary.js +83 -0
- package/package.json +7 -7
- package/dist/cjs/index.js +0 -143
- package/dist/cjs/translation/_bundleTranslation.js +0 -42
- package/dist/cjs/translation/_intl.js +0 -52
- package/dist/cjs/translation/_translate.js +0 -51
- package/dist/cjs/translation/_translateReactChildren.js +0 -56
- package/dist/cjs/translation/_updateRemoteDictionary.js +0 -30
- package/dist/mjs/codes/15924/CodeToScript.json +0 -215
- package/dist/mjs/codes/15924/ScriptToCode.json +0 -215
- package/dist/mjs/codes/3166/CodeToRegion.json +0 -296
- package/dist/mjs/codes/3166/RegionToCode.json +0 -296
- package/dist/mjs/codes/639-1/CodeToLanguage.json +0 -185
- package/dist/mjs/codes/639-1/LanguageToCode.json +0 -227
- package/dist/mjs/codes/639-3/CodeToLanguageTriletter.json +0 -186
- package/dist/mjs/codes/639-3/LanguageToCodeTriletter.json +0 -228
- package/dist/mjs/codes/codes.d.ts +0 -8
- package/dist/mjs/codes/codes.js +0 -305
- package/dist/mjs/codes/getLanguageDirection.d.ts +0 -1
- package/dist/mjs/codes/getLanguageDirection.js +0 -68
- package/dist/mjs/codes/predefined/Predefined.json +0 -28
- package/dist/mjs/index.d.ts +0 -153
- package/dist/mjs/index.js +0 -130
- package/dist/mjs/translation/_bundleTranslation.d.ts +0 -22
- package/dist/mjs/translation/_bundleTranslation.js +0 -39
- package/dist/mjs/translation/_intl.d.ts +0 -1
- package/dist/mjs/translation/_intl.js +0 -49
- package/dist/mjs/translation/_translate.d.ts +0 -1
- package/dist/mjs/translation/_translate.js +0 -48
- package/dist/mjs/translation/_translateReactChildren.d.ts +0 -1
- package/dist/mjs/translation/_translateReactChildren.js +0 -53
- package/dist/mjs/translation/_updateRemoteDictionary.d.ts +0 -17
- package/dist/mjs/translation/_updateRemoteDictionary.js +0 -27
- /package/dist/{cjs/codes → codes}/15924/CodeToScript.json +0 -0
- /package/dist/{cjs/codes → codes}/15924/ScriptToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/3166/CodeToRegion.json +0 -0
- /package/dist/{cjs/codes → codes}/3166/RegionToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/639-1/CodeToLanguage.json +0 -0
- /package/dist/{cjs/codes → codes}/639-1/LanguageToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/639-3/CodeToLanguageTriletter.json +0 -0
- /package/dist/{cjs/codes → codes}/639-3/LanguageToCodeTriletter.json +0 -0
- /package/dist/{cjs/codes → codes}/codes.d.ts +0 -0
- /package/dist/{cjs/codes → codes}/getLanguageDirection.d.ts +0 -0
- /package/dist/{cjs/codes → codes}/predefined/Predefined.json +0 -0
- /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
- /package/dist/{cjs/translation → translation}/_bundleTranslation.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_intl.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_translate.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_translateReactChildren.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_updateRemoteDictionary.d.ts +0 -0
@@ -0,0 +1,107 @@
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.default = _translate;
|
40
|
+
/**
|
41
|
+
* Translates a single piece of content and caches for use in a public project.
|
42
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
43
|
+
* @param {string} content - The content to translate.
|
44
|
+
* @param {string} targetLanguage - The target language for the translation.
|
45
|
+
* @param {string} projectID - The ID of the project
|
46
|
+
* @param {{ dictionaryName?: string, notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
|
47
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
48
|
+
* @internal
|
49
|
+
*/
|
50
|
+
function _translate(gt, content, targetLanguage, projectID, metadata) {
|
51
|
+
return __awaiter(this, void 0, void 0, function () {
|
52
|
+
var controller, signal, response, _a, _b, _c, result, error_1;
|
53
|
+
return __generator(this, function (_d) {
|
54
|
+
switch (_d.label) {
|
55
|
+
case 0:
|
56
|
+
controller = new AbortController();
|
57
|
+
signal = controller.signal;
|
58
|
+
if (metadata.timeout) {
|
59
|
+
setTimeout(function () { return controller.abort(); }, metadata.timeout);
|
60
|
+
}
|
61
|
+
_d.label = 1;
|
62
|
+
case 1:
|
63
|
+
_d.trys.push([1, 6, , 7]);
|
64
|
+
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/translate"), {
|
65
|
+
method: 'POST',
|
66
|
+
headers: {
|
67
|
+
'Content-Type': 'application/json',
|
68
|
+
'gtx-api-key': gt.apiKey,
|
69
|
+
},
|
70
|
+
body: JSON.stringify({
|
71
|
+
content: content,
|
72
|
+
targetLanguage: targetLanguage,
|
73
|
+
projectID: projectID,
|
74
|
+
metadata: metadata
|
75
|
+
}),
|
76
|
+
signal: signal
|
77
|
+
})];
|
78
|
+
case 2:
|
79
|
+
response = _d.sent();
|
80
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
81
|
+
_a = Error.bind;
|
82
|
+
_c = (_b = "".concat(response.status, ": ")).concat;
|
83
|
+
return [4 /*yield*/, response.text()];
|
84
|
+
case 3: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
85
|
+
case 4: return [4 /*yield*/, response.json()];
|
86
|
+
case 5:
|
87
|
+
result = _d.sent();
|
88
|
+
return [2 /*return*/, result];
|
89
|
+
case 6:
|
90
|
+
error_1 = _d.sent();
|
91
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
92
|
+
console.error('Request timed out');
|
93
|
+
return [2 /*return*/, {
|
94
|
+
translation: content,
|
95
|
+
error: 'Request timed out'
|
96
|
+
}];
|
97
|
+
}
|
98
|
+
console.error(error_1);
|
99
|
+
return [2 /*return*/, {
|
100
|
+
translation: content,
|
101
|
+
error: error_1
|
102
|
+
}];
|
103
|
+
case 7: return [2 /*return*/];
|
104
|
+
}
|
105
|
+
});
|
106
|
+
});
|
107
|
+
}
|
@@ -0,0 +1,105 @@
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.default = _translate;
|
40
|
+
/**
|
41
|
+
* Translates a string.
|
42
|
+
* @param {{ baseURL: string, apiKey: string }} gt - The translation service configuration.
|
43
|
+
* @param {string} content - The content to translate.
|
44
|
+
* @param {string} targetLanguage - The target language for the translation.
|
45
|
+
* @param {{ notes?: string, timeout?: number, [key: string]: any }} metadata - Additional metadata for the translation request.
|
46
|
+
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
47
|
+
* @internal
|
48
|
+
*/
|
49
|
+
function _translate(gt, content, targetLanguage, metadata) {
|
50
|
+
return __awaiter(this, void 0, void 0, function () {
|
51
|
+
var controller, signal, response, _a, _b, _c, result, error_1;
|
52
|
+
return __generator(this, function (_d) {
|
53
|
+
switch (_d.label) {
|
54
|
+
case 0:
|
55
|
+
controller = new AbortController();
|
56
|
+
signal = controller.signal;
|
57
|
+
if (metadata.timeout) {
|
58
|
+
setTimeout(function () { return controller.abort(); }, metadata.timeout);
|
59
|
+
}
|
60
|
+
_d.label = 1;
|
61
|
+
case 1:
|
62
|
+
_d.trys.push([1, 6, , 7]);
|
63
|
+
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/translate"), {
|
64
|
+
method: 'POST',
|
65
|
+
headers: {
|
66
|
+
'Content-Type': 'application/json',
|
67
|
+
'gtx-api-key': gt.apiKey,
|
68
|
+
},
|
69
|
+
body: JSON.stringify({
|
70
|
+
content: content,
|
71
|
+
targetLanguage: targetLanguage,
|
72
|
+
metadata: metadata
|
73
|
+
}),
|
74
|
+
signal: signal
|
75
|
+
})];
|
76
|
+
case 2:
|
77
|
+
response = _d.sent();
|
78
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
79
|
+
_a = Error.bind;
|
80
|
+
_c = (_b = "".concat(response.status, ": ")).concat;
|
81
|
+
return [4 /*yield*/, response.text()];
|
82
|
+
case 3: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
83
|
+
case 4: return [4 /*yield*/, response.json()];
|
84
|
+
case 5:
|
85
|
+
result = _d.sent();
|
86
|
+
return [2 /*return*/, result];
|
87
|
+
case 6:
|
88
|
+
error_1 = _d.sent();
|
89
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
90
|
+
console.error('Request timed out');
|
91
|
+
return [2 /*return*/, {
|
92
|
+
translation: content,
|
93
|
+
error: 'Request timed out'
|
94
|
+
}];
|
95
|
+
}
|
96
|
+
console.error(error_1);
|
97
|
+
return [2 /*return*/, {
|
98
|
+
translation: content,
|
99
|
+
error: error_1
|
100
|
+
}];
|
101
|
+
case 7: return [2 /*return*/];
|
102
|
+
}
|
103
|
+
});
|
104
|
+
});
|
105
|
+
}
|
@@ -0,0 +1,107 @@
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.default = _translateReactChildren;
|
40
|
+
/**
|
41
|
+
* Translates the given content 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 content to be translated. This can be of any type.
|
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
|
+
* @internal
|
52
|
+
**/
|
53
|
+
function _translateReactChildren(gt, content, targetLanguage, metadata) {
|
54
|
+
return __awaiter(this, void 0, void 0, function () {
|
55
|
+
var controller, signal, response, _a, _b, _c, error_1;
|
56
|
+
return __generator(this, function (_d) {
|
57
|
+
switch (_d.label) {
|
58
|
+
case 0:
|
59
|
+
controller = new AbortController();
|
60
|
+
signal = controller.signal;
|
61
|
+
if (metadata.timeout) {
|
62
|
+
setTimeout(function () { return controller.abort(); }, metadata.timeout);
|
63
|
+
}
|
64
|
+
_d.label = 1;
|
65
|
+
case 1:
|
66
|
+
_d.trys.push([1, 6, , 7]);
|
67
|
+
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/react"), {
|
68
|
+
method: 'POST',
|
69
|
+
headers: {
|
70
|
+
'Content-Type': 'application/json',
|
71
|
+
'gtx-api-key': gt.apiKey,
|
72
|
+
},
|
73
|
+
body: JSON.stringify({
|
74
|
+
content: content,
|
75
|
+
targetLanguage: targetLanguage,
|
76
|
+
metadata: metadata
|
77
|
+
}),
|
78
|
+
signal: signal
|
79
|
+
})];
|
80
|
+
case 2:
|
81
|
+
response = _d.sent();
|
82
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
83
|
+
_a = Error.bind;
|
84
|
+
_c = (_b = "".concat(response.status, ": ")).concat;
|
85
|
+
return [4 /*yield*/, response.text()];
|
86
|
+
case 3: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
87
|
+
case 4: return [4 /*yield*/, response.json()];
|
88
|
+
case 5: return [2 /*return*/, _d.sent()];
|
89
|
+
case 6:
|
90
|
+
error_1 = _d.sent();
|
91
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
92
|
+
console.error('Request timed out');
|
93
|
+
return [2 /*return*/, {
|
94
|
+
translation: null,
|
95
|
+
error: 'Request timed out'
|
96
|
+
}];
|
97
|
+
}
|
98
|
+
console.error(error_1);
|
99
|
+
return [2 /*return*/, {
|
100
|
+
translation: null,
|
101
|
+
error: error_1
|
102
|
+
}];
|
103
|
+
case 7: return [2 /*return*/];
|
104
|
+
}
|
105
|
+
});
|
106
|
+
});
|
107
|
+
}
|
@@ -0,0 +1,83 @@
|
|
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
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
15
|
+
function step(op) {
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
20
|
+
switch (op[0]) {
|
21
|
+
case 0: case 1: t = op; break;
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
25
|
+
default:
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
30
|
+
if (t[2]) _.ops.pop();
|
31
|
+
_.trys.pop(); continue;
|
32
|
+
}
|
33
|
+
op = body.call(thisArg, _);
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
36
|
+
}
|
37
|
+
};
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
39
|
+
exports.default = _updateRemoteDictionary;
|
40
|
+
function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
|
41
|
+
return __awaiter(this, void 0, void 0, function () {
|
42
|
+
var response, _a, _b, _c, result, error_1;
|
43
|
+
return __generator(this, function (_d) {
|
44
|
+
switch (_d.label) {
|
45
|
+
case 0:
|
46
|
+
_d.trys.push([0, 5, , 6]);
|
47
|
+
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/update"), {
|
48
|
+
method: 'POST',
|
49
|
+
headers: {
|
50
|
+
'Content-Type': 'application/json',
|
51
|
+
'gtx-api-key': gt.apiKey,
|
52
|
+
},
|
53
|
+
body: JSON.stringify({
|
54
|
+
updates: updates,
|
55
|
+
languages: languages,
|
56
|
+
projectID: projectID,
|
57
|
+
replace: replace
|
58
|
+
})
|
59
|
+
})];
|
60
|
+
case 1:
|
61
|
+
response = _d.sent();
|
62
|
+
if (!!response.ok) return [3 /*break*/, 3];
|
63
|
+
_a = Error.bind;
|
64
|
+
_c = (_b = "".concat(response.status, ": ")).concat;
|
65
|
+
return [4 /*yield*/, response.text()];
|
66
|
+
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
67
|
+
case 3: return [4 /*yield*/, response.json()];
|
68
|
+
case 4:
|
69
|
+
result = _d.sent();
|
70
|
+
return [2 /*return*/, result.languages];
|
71
|
+
case 5:
|
72
|
+
error_1 = _d.sent();
|
73
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
74
|
+
console.error('Request timed out');
|
75
|
+
return [2 /*return*/, []];
|
76
|
+
}
|
77
|
+
console.error(error_1);
|
78
|
+
return [2 /*return*/, []];
|
79
|
+
case 6: return [2 /*return*/];
|
80
|
+
}
|
81
|
+
});
|
82
|
+
});
|
83
|
+
}
|
package/package.json
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
{
|
2
2
|
"name": "generaltranslation",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.56",
|
4
4
|
"description": "A language toolkit for AI developers",
|
5
|
-
"main": "dist/
|
6
|
-
"types": "dist/
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"types": "dist/index.d.ts",
|
7
7
|
"files": [
|
8
8
|
"dist"
|
9
9
|
],
|
10
10
|
"scripts": {
|
11
|
-
"transpile": "rm -r -f dist; tsc;
|
11
|
+
"transpile": "rm -r -f dist; tsc;",
|
12
12
|
"prepublishOnly": "npm run transpile"
|
13
13
|
},
|
14
14
|
"exports": {
|
15
15
|
".": {
|
16
|
-
"import": "./dist/
|
17
|
-
"require": "./dist/
|
18
|
-
"types": "./dist/
|
16
|
+
"import": "./dist/index.js",
|
17
|
+
"require": "./dist/index.js",
|
18
|
+
"types": "./dist/index.d.ts"
|
19
19
|
}
|
20
20
|
},
|
21
21
|
"repository": {
|
package/dist/cjs/index.js
DELETED
@@ -1,143 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
// `generaltranslation` language toolkit
|
3
|
-
// © 2024, General Translation, Inc.
|
4
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
5
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
6
|
-
};
|
7
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
8
|
-
exports.standardizeLanguageCode = exports.isValidLanguageCode = exports.getLanguageDirection = void 0;
|
9
|
-
exports.getLanguageObject = getLanguageObject;
|
10
|
-
exports.getLanguageCode = getLanguageCode;
|
11
|
-
exports.getLanguageName = getLanguageName;
|
12
|
-
exports.isSameLanguage = isSameLanguage;
|
13
|
-
// ----- IMPORTS ----- //
|
14
|
-
const codes_1 = require("./codes/codes");
|
15
|
-
const getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
|
16
|
-
const _bundleTranslation_1 = __importDefault(require("./translation/_bundleTranslation"));
|
17
|
-
const _intl_1 = __importDefault(require("./translation/_intl"));
|
18
|
-
const _translate_1 = __importDefault(require("./translation/_translate"));
|
19
|
-
const _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
|
20
|
-
const _updateRemoteDictionary_1 = __importDefault(require("./translation/_updateRemoteDictionary"));
|
21
|
-
// ----- CORE CLASS ----- //
|
22
|
-
const getDefaultFromEnv = (VARIABLE) => {
|
23
|
-
if (typeof process !== 'undefined' && process.env) {
|
24
|
-
return process.env[VARIABLE] || '';
|
25
|
-
}
|
26
|
-
return '';
|
27
|
-
};
|
28
|
-
/**
|
29
|
-
* GT is the core driver for the General Translation library.
|
30
|
-
*/
|
31
|
-
class GT {
|
32
|
-
/**
|
33
|
-
* Constructs an instance of the GT class.
|
34
|
-
*
|
35
|
-
* @param {GTConstructorParams} [params] - The parameters for initializing the GT instance.
|
36
|
-
* @param {string} [params.apiKey=''] - The API key for accessing the translation service.
|
37
|
-
* @param {string} [params.defaultLanguage='en'] - The default language for translations.
|
38
|
-
* @param {string} [params.projectID=''] - The project ID for the translation service.
|
39
|
-
* @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
|
40
|
-
*/
|
41
|
-
constructor({ apiKey = '', defaultLanguage = 'en', projectID = '', baseURL = 'https://prod.gtx.dev' } = {}) {
|
42
|
-
this.apiKey = apiKey || getDefaultFromEnv('GT_API_KEY');
|
43
|
-
this.projectID = projectID || getDefaultFromEnv('GT_PROJECT_ID');
|
44
|
-
this.defaultLanguage = defaultLanguage.toLowerCase();
|
45
|
-
this.baseURL = baseURL;
|
46
|
-
}
|
47
|
-
/**
|
48
|
-
* Translates a string into a target language.
|
49
|
-
* @param {string} content - A string to translate.
|
50
|
-
* @param {string} targetLanguage - The target language for the translation.
|
51
|
-
* @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
52
|
-
* @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
|
53
|
-
*/
|
54
|
-
async translate(content, targetLanguage, metadata) {
|
55
|
-
return await (0, _translate_1.default)(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
56
|
-
}
|
57
|
-
/**
|
58
|
-
* Translates a string and caches for use in a public project.
|
59
|
-
* @param {string} content - A string to translate.
|
60
|
-
* @param {string} targetLanguage - The target language for the translation.
|
61
|
-
* @param {string} projectID - The ID of the project.
|
62
|
-
* @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
|
63
|
-
* @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
|
64
|
-
*/
|
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 });
|
67
|
-
}
|
68
|
-
/**
|
69
|
-
* Translates the content of React children elements.
|
70
|
-
*
|
71
|
-
* @param {Object} params - The parameters for the translation.
|
72
|
-
* @param {any} params.content - The React children content to be translated.
|
73
|
-
* @param {string} params.targetLanguage - The target language for the translation.
|
74
|
-
* @param {Object} params.metadata - Additional metadata for the translation process.
|
75
|
-
*
|
76
|
-
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
77
|
-
*/
|
78
|
-
async translateReactChildren(content, targetLanguage, metadata) {
|
79
|
-
return await (0, _translateReactChildren_1.default)(this, content, targetLanguage, { projectID: this.projectID, defaultLanguage: this.defaultLanguage, ...metadata });
|
80
|
-
}
|
81
|
-
/**
|
82
|
-
* Bundles multiple translation requests and sends them to the server.
|
83
|
-
* @param requests - Array of requests to be processed and sent.
|
84
|
-
* @returns A promise that resolves to an array of processed results.
|
85
|
-
*/
|
86
|
-
async bundleTranslation(requests) {
|
87
|
-
return (0, _bundleTranslation_1.default)(this, requests);
|
88
|
-
}
|
89
|
-
/**
|
90
|
-
* Pushes updates to a remotely cached translation dictionary.
|
91
|
-
* @param {Update[]} updates - Array of updates with optional targetLanguage.
|
92
|
-
* @param {string[]} [languages] - Array of languages to be updated.
|
93
|
-
* @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
|
94
|
-
* @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
|
95
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
|
96
|
-
*/
|
97
|
-
async updateRemoteDictionary(updates, languages = [], projectID = this.projectID, replace = false) {
|
98
|
-
return (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace);
|
99
|
-
}
|
100
|
-
}
|
101
|
-
// ----- EXPORTS ----- //
|
102
|
-
// Export the class
|
103
|
-
exports.default = GT;
|
104
|
-
// Export the functions
|
105
|
-
/**
|
106
|
-
* Gets the writing direction for a given BCP 47 language code.
|
107
|
-
* @param {string} code - The BCP 47 language code to check.
|
108
|
-
* @returns {string} The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
|
109
|
-
*/
|
110
|
-
const getLanguageDirection = (code) => (0, getLanguageDirection_1.default)(code);
|
111
|
-
exports.getLanguageDirection = getLanguageDirection;
|
112
|
-
/**
|
113
|
-
* Checks if a given BCP 47 language code is valid.
|
114
|
-
* @param {string} code - The BCP 47 language code to validate.
|
115
|
-
* @returns {boolean} True if the BCP 47 code is valid, false otherwise.
|
116
|
-
*/
|
117
|
-
const isValidLanguageCode = (code) => (0, codes_1._isValidLanguageCode)(code);
|
118
|
-
exports.isValidLanguageCode = isValidLanguageCode;
|
119
|
-
/**
|
120
|
-
* Standardizes a BCP 47 language code to ensure correct formatting.
|
121
|
-
* @param {string} code - The BCP 47 language code to standardize.
|
122
|
-
* @returns {string} The standardized BCP 47 language code.
|
123
|
-
*/
|
124
|
-
const standardizeLanguageCode = (code) => (0, codes_1._standardizeLanguageCode)(code);
|
125
|
-
exports.standardizeLanguageCode = standardizeLanguageCode;
|
126
|
-
function getLanguageObject(codes) {
|
127
|
-
return Array.isArray(codes) ? (0, codes_1._getLanguageObject)(codes) : (0, codes_1._getLanguageObject)(codes);
|
128
|
-
}
|
129
|
-
function getLanguageCode(languages) {
|
130
|
-
return (0, codes_1._getLanguageCode)(languages);
|
131
|
-
}
|
132
|
-
function getLanguageName(codes) {
|
133
|
-
return (0, codes_1._getLanguageName)(codes);
|
134
|
-
}
|
135
|
-
/**
|
136
|
-
* Checks if multiple BCP 47 language codes represent the same language.
|
137
|
-
* @param {string[]} codes - The BCP 47 language codes to compare.
|
138
|
-
* @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
|
139
|
-
*/
|
140
|
-
function isSameLanguage(...codes) {
|
141
|
-
return (0, codes_1._isSameLanguage)(...codes);
|
142
|
-
}
|
143
|
-
;
|
@@ -1,42 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.default = _bundleTranslation;
|
4
|
-
/**
|
5
|
-
* Bundles multiple requests and sends them to the server.
|
6
|
-
* @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
|
7
|
-
* @param {Request[]} requests - Array of requests to be processed and sent.
|
8
|
-
* @param {{ timeout?: number }} options - Additional options for the request, including timeout.
|
9
|
-
* @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
|
10
|
-
* @internal
|
11
|
-
*/
|
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()}`);
|
30
|
-
}
|
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' }));
|
38
|
-
}
|
39
|
-
console.error(error);
|
40
|
-
return Array.from(requests, () => ({ translation: null, error: error }));
|
41
|
-
}
|
42
|
-
}
|