generaltranslation 2.0.56 → 2.0.57

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports._num = _num;
15
+ exports._datetime = _datetime;
16
+ exports._currency = _currency;
17
+ /**
18
+ * Formats a number according to the specified languages and options.
19
+ *
20
+ * @param {Object} params - The parameters for the number formatting.
21
+ * @param {number} params.value - The number to format.
22
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
23
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
24
+ *
25
+ * @returns {string} The formatted number.
26
+ * @internal
27
+ */
28
+ function _num(_a) {
29
+ var value = _a.value, _b = _a.languages, languages = _b === void 0 ? ['en'] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
30
+ return new Intl.NumberFormat(languages, __assign({ numberingSystem: 'latn' }, options)).format(value);
31
+ }
32
+ /**
33
+ * Formats a date according to the specified languages and options.
34
+ *
35
+ * @param {Object} params - The parameters for the date formatting.
36
+ * @param {Date} params.value - The date to format.
37
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
38
+ * @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
39
+ *
40
+ * @returns {string} The formatted date.
41
+ * @internal
42
+ */
43
+ function _datetime(_a) {
44
+ var value = _a.value, _b = _a.languages, languages = _b === void 0 ? ['en'] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
45
+ return new Intl.DateTimeFormat(languages, __assign({ calendar: "gregory", numberingSystem: "latn" }, options)).format(value);
46
+ }
47
+ /**
48
+ * Formats a currency value according to the specified languages, currency, and options.
49
+ *
50
+ * @param {Object} params - The parameters for the currency formatting.
51
+ * @param {number} params.value - The currency value to format.
52
+ * @param {string} params.currency - The currency code (e.g., 'USD').
53
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
54
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
55
+ *
56
+ * @returns {string} The formatted currency value.
57
+ * @internal
58
+ */
59
+ function _currency(_a) {
60
+ var value = _a.value, _b = _a.languages, languages = _b === void 0 ? ['en'] : _b, _c = _a.currency, currency = _c === void 0 ? 'USD' : _c, _d = _a.options, options = _d === void 0 ? {} : _d;
61
+ return new Intl.NumberFormat(languages, __assign({ style: 'currency', currency: currency, numberingSystem: 'latn' }, options)).format(value);
62
+ }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { LanguageObject } from './codes/codes';
2
- import { Request } from './translation/_bundleTranslation';
3
- import { Update } from './translation/_updateRemoteDictionary';
2
+ import { Request } from './translation/_translateBundle';
3
+ import { Update } from './translation/_updateProjectDictionary';
4
+ import { _num, _datetime, _currency } from './format/_format';
4
5
  /**
5
6
  * Type representing the constructor parameters for the GT class.
6
7
  */
@@ -29,30 +30,20 @@ declare class GT {
29
30
  */
30
31
  constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
31
32
  /**
32
- * Translates a string into a target language.
33
- * @param {string} content - A string to translate.
34
- * @param {string} targetLanguage - The target language for the translation.
35
- * @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
36
- * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
37
- */
33
+ * Translates a string into a target language.
34
+ * If `metadata.store` is provided, the translation is cached for use in a public project.
35
+ *
36
+ * @param {string} content - The string to be translated.
37
+ * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
38
+ * @param {{ context?: string, store?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
39
+ * @param {string} [metadata.context] - Contextual information to assist with the translation.
40
+ * @param {boolean} [metadata.store] - Whether to cache the translation for use in a public project.
41
+ *
42
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - A promise that resolves to the translated content, or an error if the translation fails.
43
+ */
38
44
  translate(content: string, targetLanguage: string, metadata?: {
39
- notes?: string;
40
- [key: string]: any;
41
- }): Promise<{
42
- translation: string;
43
- error?: Error | unknown;
44
- }>;
45
- /**
46
- * Translates a string and caches for use in a public project.
47
- * @param {string} content - A string to translate.
48
- * @param {string} targetLanguage - The target language for the translation.
49
- * @param {string} projectID - The ID of the project.
50
- * @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
51
- * @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
52
- */
53
- intl(content: string, targetLanguage: string, projectID?: string, metadata?: {
54
- dictionaryName?: string;
55
45
  context?: string;
46
+ store?: boolean;
56
47
  [key: string]: any;
57
48
  }): Promise<{
58
49
  translation: string;
@@ -68,7 +59,9 @@ declare class GT {
68
59
  *
69
60
  * @returns {Promise<any>} - A promise that resolves to the translated content.
70
61
  */
71
- translateReactChildren(content: any, targetLanguage: string, metadata?: {
62
+ translateReact(content: any, targetLanguage: string, metadata?: {
63
+ context?: string;
64
+ store?: boolean;
72
65
  [key: string]: any;
73
66
  }): Promise<{
74
67
  translation: any | null;
@@ -79,7 +72,7 @@ declare class GT {
79
72
  * @param requests - Array of requests to be processed and sent.
80
73
  * @returns A promise that resolves to an array of processed results.
81
74
  */
82
- bundleTranslation(requests: Request[]): Promise<Array<any | null>>;
75
+ translateBundle(requests: Request[]): Promise<Array<any | null>>;
83
76
  /**
84
77
  * Pushes updates to a remotely cached translation dictionary.
85
78
  * @param {Update[]} updates - Array of updates with optional targetLanguage.
@@ -88,7 +81,7 @@ declare class GT {
88
81
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
89
82
  * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
90
83
  */
91
- updateRemoteDictionary(updates: Update[], languages?: string[], projectID?: string, replace?: boolean): Promise<string[]>;
84
+ updateProjectDictionary(updates: Update[], languages?: string[], projectID?: string, replace?: boolean): Promise<string[]>;
92
85
  }
93
86
  export default GT;
94
87
  /**
@@ -151,3 +144,31 @@ export declare function getLanguageName(codes: string[]): string[];
151
144
  * @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
152
145
  */
153
146
  export declare function isSameLanguage(...codes: string[]): boolean;
147
+ /**
148
+ * Formats a number according to the specified languages and options.
149
+ * @param {Object} params - The parameters for the number formatting.
150
+ * @param {number} params.value - The number to format.
151
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
152
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
153
+ * @returns {string} The formatted number.
154
+ */
155
+ export declare const num: typeof _num;
156
+ /**
157
+ * Formats a date according to the specified languages and options.
158
+ * @param {Object} params - The parameters for the date formatting.
159
+ * @param {Date} params.value - The date to format.
160
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
161
+ * @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
162
+ * @returns {string} The formatted date.
163
+ */
164
+ export declare const datetime: typeof _datetime;
165
+ /**
166
+ * Formats a currency value according to the specified languages, currency, and options.
167
+ * @param {Object} params - The parameters for the currency formatting.
168
+ * @param {number} params.value - The currency value to format.
169
+ * @param {string} params.currency - The currency code (e.g., 'USD').
170
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
171
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
172
+ * @returns {string} The formatted currency value.
173
+ */
174
+ export declare const currency: typeof _currency;
package/dist/index.js CHANGED
@@ -52,7 +52,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
52
52
  return (mod && mod.__esModule) ? mod : { "default": mod };
53
53
  };
54
54
  Object.defineProperty(exports, "__esModule", { value: true });
55
- exports.standardizeLanguageCode = exports.isValidLanguageCode = exports.getLanguageDirection = void 0;
55
+ exports.currency = exports.datetime = exports.num = exports.standardizeLanguageCode = exports.isValidLanguageCode = exports.getLanguageDirection = void 0;
56
56
  exports.getLanguageObject = getLanguageObject;
57
57
  exports.getLanguageCode = getLanguageCode;
58
58
  exports.getLanguageName = getLanguageName;
@@ -60,11 +60,11 @@ exports.isSameLanguage = isSameLanguage;
60
60
  // ----- IMPORTS ----- //
61
61
  var codes_1 = require("./codes/codes");
62
62
  var getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
63
- var _bundleTranslation_1 = __importDefault(require("./translation/_bundleTranslation"));
64
- var _intl_1 = __importDefault(require("./translation/_intl"));
63
+ var _translateBundle_1 = __importDefault(require("./translation/_translateBundle"));
65
64
  var _translate_1 = __importDefault(require("./translation/_translate"));
66
- var _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
67
- var _updateRemoteDictionary_1 = __importDefault(require("./translation/_updateRemoteDictionary"));
65
+ var _translateReact_1 = __importDefault(require("./translation/_translateReact"));
66
+ var _updateProjectDictionary_1 = __importDefault(require("./translation/_updateProjectDictionary"));
67
+ var _format_1 = require("./format/_format");
68
68
  // ----- CORE CLASS ----- //
69
69
  var getDefaultFromEnv = function (VARIABLE) {
70
70
  if (typeof process !== 'undefined' && process.env) {
@@ -93,12 +93,17 @@ var GT = /** @class */ (function () {
93
93
  this.baseURL = baseURL;
94
94
  }
95
95
  /**
96
- * Translates a string into a target language.
97
- * @param {string} content - A string to translate.
98
- * @param {string} targetLanguage - The target language for the translation.
99
- * @param {{ notes?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
100
- * @returns {Promise<{ translation: string, error?: Error | unknown }>} - The translated content with optional error information.
101
- */
96
+ * Translates a string into a target language.
97
+ * If `metadata.store` is provided, the translation is cached for use in a public project.
98
+ *
99
+ * @param {string} content - The string to be translated.
100
+ * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
101
+ * @param {{ context?: string, store?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
102
+ * @param {string} [metadata.context] - Contextual information to assist with the translation.
103
+ * @param {boolean} [metadata.store] - Whether to cache the translation for use in a public project.
104
+ *
105
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - A promise that resolves to the translated content, or an error if the translation fails.
106
+ */
102
107
  GT.prototype.translate = function (content, targetLanguage, metadata) {
103
108
  return __awaiter(this, void 0, void 0, function () {
104
109
  return __generator(this, function (_a) {
@@ -110,24 +115,6 @@ var GT = /** @class */ (function () {
110
115
  });
111
116
  };
112
117
  /**
113
- * Translates a string and caches for use in a public project.
114
- * @param {string} content - A string to translate.
115
- * @param {string} targetLanguage - The target language for the translation.
116
- * @param {string} projectID - The ID of the project.
117
- * @param {dictionaryName?: string, context?: string, [key: string]: any }} metadata - Additional metadata for the translation request.
118
- * @returns {Promise<{ translation: string, error?: Error | unknown }>} The translated content with optional error information.
119
- */
120
- GT.prototype.intl = function (content, targetLanguage, projectID, metadata) {
121
- return __awaiter(this, void 0, void 0, function () {
122
- return __generator(this, function (_a) {
123
- switch (_a.label) {
124
- case 0: return [4 /*yield*/, (0, _intl_1.default)(this, content, targetLanguage, projectID || this.projectID, __assign({ projectID: projectID || this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
125
- case 1: return [2 /*return*/, _a.sent()];
126
- }
127
- });
128
- });
129
- };
130
- /**
131
118
  * Translates the content of React children elements.
132
119
  *
133
120
  * @param {Object} params - The parameters for the translation.
@@ -137,11 +124,11 @@ var GT = /** @class */ (function () {
137
124
  *
138
125
  * @returns {Promise<any>} - A promise that resolves to the translated content.
139
126
  */
140
- GT.prototype.translateReactChildren = function (content, targetLanguage, metadata) {
127
+ GT.prototype.translateReact = function (content, targetLanguage, metadata) {
141
128
  return __awaiter(this, void 0, void 0, function () {
142
129
  return __generator(this, function (_a) {
143
130
  switch (_a.label) {
144
- case 0: return [4 /*yield*/, (0, _translateReactChildren_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
131
+ case 0: return [4 /*yield*/, (0, _translateReact_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
145
132
  case 1: return [2 /*return*/, _a.sent()];
146
133
  }
147
134
  });
@@ -152,10 +139,10 @@ var GT = /** @class */ (function () {
152
139
  * @param requests - Array of requests to be processed and sent.
153
140
  * @returns A promise that resolves to an array of processed results.
154
141
  */
155
- GT.prototype.bundleTranslation = function (requests) {
142
+ GT.prototype.translateBundle = function (requests) {
156
143
  return __awaiter(this, void 0, void 0, function () {
157
144
  return __generator(this, function (_a) {
158
- return [2 /*return*/, (0, _bundleTranslation_1.default)(this, requests)];
145
+ return [2 /*return*/, (0, _translateBundle_1.default)(this, requests)];
159
146
  });
160
147
  });
161
148
  };
@@ -167,13 +154,13 @@ var GT = /** @class */ (function () {
167
154
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
168
155
  * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
169
156
  */
170
- GT.prototype.updateRemoteDictionary = function (updates_1) {
157
+ GT.prototype.updateProjectDictionary = function (updates_1) {
171
158
  return __awaiter(this, arguments, void 0, function (updates, languages, projectID, replace) {
172
159
  if (languages === void 0) { languages = []; }
173
160
  if (projectID === void 0) { projectID = this.projectID; }
174
161
  if (replace === void 0) { replace = false; }
175
162
  return __generator(this, function (_a) {
176
- return [2 /*return*/, (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace)];
163
+ return [2 /*return*/, (0, _updateProjectDictionary_1.default)(this, updates, languages, projectID, replace)];
177
164
  });
178
165
  });
179
166
  };
@@ -226,3 +213,31 @@ function isSameLanguage() {
226
213
  return codes_1._isSameLanguage.apply(void 0, codes);
227
214
  }
228
215
  ;
216
+ /**
217
+ * Formats a number according to the specified languages and options.
218
+ * @param {Object} params - The parameters for the number formatting.
219
+ * @param {number} params.value - The number to format.
220
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
221
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
222
+ * @returns {string} The formatted number.
223
+ */
224
+ exports.num = _format_1._num;
225
+ /**
226
+ * Formats a date according to the specified languages and options.
227
+ * @param {Object} params - The parameters for the date formatting.
228
+ * @param {Date} params.value - The date to format.
229
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
230
+ * @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
231
+ * @returns {string} The formatted date.
232
+ */
233
+ exports.datetime = _format_1._datetime;
234
+ /**
235
+ * Formats a currency value according to the specified languages, currency, and options.
236
+ * @param {Object} params - The parameters for the currency formatting.
237
+ * @param {number} params.value - The currency value to format.
238
+ * @param {string} params.currency - The currency code (e.g., 'USD').
239
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
240
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
241
+ * @returns {string} The formatted currency value.
242
+ */
243
+ exports.currency = _format_1._currency;
@@ -38,14 +38,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.default = _translate;
40
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.
41
+ * Translates the given string 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 string to be translated.
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).
47
51
  * @internal
48
- */
52
+ **/
49
53
  function _translate(gt, content, targetLanguage, metadata) {
50
54
  return __awaiter(this, void 0, void 0, function () {
51
55
  var controller, signal, response, _a, _b, _c, result, error_1;
@@ -1,12 +1,5 @@
1
1
  export type Request = {
2
- type: 'translate';
3
- data: {
4
- content: string;
5
- targetLanguage: string;
6
- metadata: Record<string, any>;
7
- };
8
- } | {
9
- type: 'intl';
2
+ type: 'string';
10
3
  data: {
11
4
  content: string;
12
5
  targetLanguage: string;
@@ -15,7 +8,7 @@ export type Request = {
15
8
  } | {
16
9
  type: 'react';
17
10
  data: {
18
- children: object | string;
11
+ content: string;
19
12
  targetLanguage: string;
20
13
  metadata: Record<string, any>;
21
14
  };
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.default = _bundleTranslation;
39
+ exports.default = _translateBundle;
40
40
  /**
41
41
  * Bundles multiple requests and sends them to the server.
42
42
  * @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
@@ -45,7 +45,7 @@ exports.default = _bundleTranslation;
45
45
  * @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
46
46
  * @internal
47
47
  */
48
- function _bundleTranslation(gt, requests) {
48
+ function _translateBundle(gt, requests) {
49
49
  return __awaiter(this, void 0, void 0, function () {
50
50
  var controller, signal, response, _a, _b, _c, resultArray, error_1;
51
51
  var _d, _e, _f;
@@ -36,12 +36,12 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.default = _translateReactChildren;
39
+ exports.default = _translateReact;
40
40
  /**
41
- * Translates the given content into the target language using a specified API.
41
+ * Translates the given React children into the target language using a specified API.
42
42
  *
43
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.
44
+ * @param {any} children - The React children to be translated.
45
45
  * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
46
46
  * @param {{ [key: string]: any }} metadata - Additional metadata to be sent with the translation request.
47
47
  *
@@ -50,7 +50,7 @@ exports.default = _translateReactChildren;
50
50
  * @throws {Error} - Throws an error if the response from the API is not ok (status code not in the range 200-299).
51
51
  * @internal
52
52
  **/
53
- function _translateReactChildren(gt, content, targetLanguage, metadata) {
53
+ function _translateReact(gt, content, targetLanguage, metadata) {
54
54
  return __awaiter(this, void 0, void 0, function () {
55
55
  var controller, signal, response, _a, _b, _c, error_1;
56
56
  return __generator(this, function (_d) {
@@ -11,7 +11,7 @@ export type Update = {
11
11
  metadata: Record<string, any>;
12
12
  };
13
13
  };
14
- export default function _updateRemoteDictionary(gt: {
14
+ export default function _updateProjectDictionary(gt: {
15
15
  baseURL: string;
16
16
  apiKey: string;
17
17
  }, updates: Update[], languages: string[], projectID: string, replace: boolean): Promise<string[]>;
@@ -36,8 +36,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.default = _updateRemoteDictionary;
40
- function _updateRemoteDictionary(gt, updates, languages, projectID, replace) {
39
+ exports.default = _updateProjectDictionary;
40
+ function _updateProjectDictionary(gt, updates, languages, projectID, replace) {
41
41
  return __awaiter(this, void 0, void 0, function () {
42
42
  var response, _a, _b, _c, result, error_1;
43
43
  return __generator(this, function (_d) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generaltranslation",
3
- "version": "2.0.56",
3
+ "version": "2.0.57",
4
4
  "description": "A language toolkit for AI developers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,107 +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
- 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
- }