generaltranslation 2.0.54 → 2.0.56

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