generaltranslation 2.0.56 → 2.0.58

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,6 @@
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
4
  /**
5
5
  * Type representing the constructor parameters for the GT class.
6
6
  */
@@ -29,30 +29,20 @@ declare class GT {
29
29
  */
30
30
  constructor({ apiKey, defaultLanguage, projectID, baseURL }?: GTConstructorParams);
31
31
  /**
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
- */
32
+ * Translates a string into a target language.
33
+ * If `metadata.store` is provided, the translation is cached for use in a public project.
34
+ *
35
+ * @param {string} content - The string to be translated.
36
+ * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
37
+ * @param {{ context?: string, store?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
38
+ * @param {string} [metadata.context] - Contextual information to assist with the translation.
39
+ * @param {boolean} [metadata.store] - Whether to cache the translation for use in a public project.
40
+ *
41
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - A promise that resolves to the translated content, or an error if the translation fails.
42
+ */
38
43
  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
44
  context?: string;
45
+ store?: boolean;
56
46
  [key: string]: any;
57
47
  }): Promise<{
58
48
  translation: string;
@@ -68,7 +58,9 @@ declare class GT {
68
58
  *
69
59
  * @returns {Promise<any>} - A promise that resolves to the translated content.
70
60
  */
71
- translateReactChildren(content: any, targetLanguage: string, metadata?: {
61
+ translateReact(content: any, targetLanguage: string, metadata?: {
62
+ context?: string;
63
+ store?: boolean;
72
64
  [key: string]: any;
73
65
  }): Promise<{
74
66
  translation: any | null;
@@ -79,7 +71,7 @@ declare class GT {
79
71
  * @param requests - Array of requests to be processed and sent.
80
72
  * @returns A promise that resolves to an array of processed results.
81
73
  */
82
- bundleTranslation(requests: Request[]): Promise<Array<any | null>>;
74
+ translateBundle(requests: Request[]): Promise<Array<any | null>>;
83
75
  /**
84
76
  * Pushes updates to a remotely cached translation dictionary.
85
77
  * @param {Update[]} updates - Array of updates with optional targetLanguage.
@@ -88,7 +80,7 @@ declare class GT {
88
80
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
89
81
  * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
90
82
  */
91
- updateRemoteDictionary(updates: Update[], languages?: string[], projectID?: string, replace?: boolean): Promise<string[]>;
83
+ updateProjectDictionary(updates: Update[], languages?: string[], projectID?: string, replace?: boolean): Promise<string[]>;
92
84
  }
93
85
  export default GT;
94
86
  /**
@@ -151,3 +143,44 @@ export declare function getLanguageName(codes: string[]): string[];
151
143
  * @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
152
144
  */
153
145
  export declare function isSameLanguage(...codes: string[]): boolean;
146
+ /**
147
+ * Formats a number according to the specified languages and options.
148
+ * @param {Object} params - The parameters for the number formatting.
149
+ * @param {number} params.value - The number to format.
150
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
151
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
152
+ * @returns {string} The formatted number.
153
+ */
154
+ export declare function num({ value, languages, options }: {
155
+ value: number;
156
+ languages?: string | string[];
157
+ options?: Intl.NumberFormatOptions;
158
+ }): string;
159
+ /**
160
+ * Formats a date according to the specified languages and options.
161
+ * @param {Object} params - The parameters for the date formatting.
162
+ * @param {Date} params.value - The date to format.
163
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
164
+ * @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
165
+ * @returns {string} The formatted date.
166
+ */
167
+ export declare function datetime({ value, languages, options }: {
168
+ value: Date;
169
+ languages?: string | string[];
170
+ options?: Intl.DateTimeFormatOptions;
171
+ }): string;
172
+ /**
173
+ * Formats a currency value according to the specified languages, currency, and options.
174
+ * @param {Object} params - The parameters for the currency formatting.
175
+ * @param {number} params.value - The currency value to format.
176
+ * @param {string} params.currency - The currency code (e.g., 'USD').
177
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
178
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
179
+ * @returns {string} The formatted currency value.
180
+ */
181
+ export declare function currency({ value, languages, currency, options }: {
182
+ value: number;
183
+ languages?: string | string[];
184
+ currency?: string;
185
+ options?: Intl.NumberFormatOptions;
186
+ }): string;
package/dist/index.js CHANGED
@@ -57,14 +57,17 @@ exports.getLanguageObject = getLanguageObject;
57
57
  exports.getLanguageCode = getLanguageCode;
58
58
  exports.getLanguageName = getLanguageName;
59
59
  exports.isSameLanguage = isSameLanguage;
60
+ exports.num = num;
61
+ exports.datetime = datetime;
62
+ exports.currency = currency;
60
63
  // ----- IMPORTS ----- //
61
64
  var codes_1 = require("./codes/codes");
62
65
  var getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
63
- var _bundleTranslation_1 = __importDefault(require("./translation/_bundleTranslation"));
64
- var _intl_1 = __importDefault(require("./translation/_intl"));
66
+ var _translateBundle_1 = __importDefault(require("./translation/_translateBundle"));
65
67
  var _translate_1 = __importDefault(require("./translation/_translate"));
66
- var _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
67
- var _updateRemoteDictionary_1 = __importDefault(require("./translation/_updateRemoteDictionary"));
68
+ var _translateReact_1 = __importDefault(require("./translation/_translateReact"));
69
+ var _updateProjectDictionary_1 = __importDefault(require("./translation/_updateProjectDictionary"));
70
+ var _format_1 = require("./format/_format");
68
71
  // ----- CORE CLASS ----- //
69
72
  var getDefaultFromEnv = function (VARIABLE) {
70
73
  if (typeof process !== 'undefined' && process.env) {
@@ -93,12 +96,17 @@ var GT = /** @class */ (function () {
93
96
  this.baseURL = baseURL;
94
97
  }
95
98
  /**
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
- */
99
+ * Translates a string into a target language.
100
+ * If `metadata.store` is provided, the translation is cached for use in a public project.
101
+ *
102
+ * @param {string} content - The string to be translated.
103
+ * @param {string} targetLanguage - The target language code (e.g., 'en', 'fr') for the translation.
104
+ * @param {{ context?: string, store?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
105
+ * @param {string} [metadata.context] - Contextual information to assist with the translation.
106
+ * @param {boolean} [metadata.store] - Whether to cache the translation for use in a public project.
107
+ *
108
+ * @returns {Promise<{ translation: string, error?: Error | unknown }>} - A promise that resolves to the translated content, or an error if the translation fails.
109
+ */
102
110
  GT.prototype.translate = function (content, targetLanguage, metadata) {
103
111
  return __awaiter(this, void 0, void 0, function () {
104
112
  return __generator(this, function (_a) {
@@ -110,24 +118,6 @@ var GT = /** @class */ (function () {
110
118
  });
111
119
  };
112
120
  /**
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
121
  * Translates the content of React children elements.
132
122
  *
133
123
  * @param {Object} params - The parameters for the translation.
@@ -137,11 +127,11 @@ var GT = /** @class */ (function () {
137
127
  *
138
128
  * @returns {Promise<any>} - A promise that resolves to the translated content.
139
129
  */
140
- GT.prototype.translateReactChildren = function (content, targetLanguage, metadata) {
130
+ GT.prototype.translateReact = function (content, targetLanguage, metadata) {
141
131
  return __awaiter(this, void 0, void 0, function () {
142
132
  return __generator(this, function (_a) {
143
133
  switch (_a.label) {
144
- case 0: return [4 /*yield*/, (0, _translateReactChildren_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
134
+ case 0: return [4 /*yield*/, (0, _translateReact_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
145
135
  case 1: return [2 /*return*/, _a.sent()];
146
136
  }
147
137
  });
@@ -152,10 +142,10 @@ var GT = /** @class */ (function () {
152
142
  * @param requests - Array of requests to be processed and sent.
153
143
  * @returns A promise that resolves to an array of processed results.
154
144
  */
155
- GT.prototype.bundleTranslation = function (requests) {
145
+ GT.prototype.translateBundle = function (requests) {
156
146
  return __awaiter(this, void 0, void 0, function () {
157
147
  return __generator(this, function (_a) {
158
- return [2 /*return*/, (0, _bundleTranslation_1.default)(this, requests)];
148
+ return [2 /*return*/, (0, _translateBundle_1.default)(this, requests)];
159
149
  });
160
150
  });
161
151
  };
@@ -167,13 +157,13 @@ var GT = /** @class */ (function () {
167
157
  * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
168
158
  * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
169
159
  */
170
- GT.prototype.updateRemoteDictionary = function (updates_1) {
160
+ GT.prototype.updateProjectDictionary = function (updates_1) {
171
161
  return __awaiter(this, arguments, void 0, function (updates, languages, projectID, replace) {
172
162
  if (languages === void 0) { languages = []; }
173
163
  if (projectID === void 0) { projectID = this.projectID; }
174
164
  if (replace === void 0) { replace = false; }
175
165
  return __generator(this, function (_a) {
176
- return [2 /*return*/, (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace)];
166
+ return [2 /*return*/, (0, _updateProjectDictionary_1.default)(this, updates, languages, projectID, replace)];
177
167
  });
178
168
  });
179
169
  };
@@ -226,3 +216,40 @@ function isSameLanguage() {
226
216
  return codes_1._isSameLanguage.apply(void 0, codes);
227
217
  }
228
218
  ;
219
+ /**
220
+ * Formats a number according to the specified languages and options.
221
+ * @param {Object} params - The parameters for the number formatting.
222
+ * @param {number} params.value - The number to format.
223
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
224
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
225
+ * @returns {string} The formatted number.
226
+ */
227
+ function num(_a) {
228
+ var value = _a.value, languages = _a.languages, options = _a.options;
229
+ return (0, _format_1._num)({ value: value, languages: languages, options: options });
230
+ }
231
+ /**
232
+ * Formats a date according to the specified languages and options.
233
+ * @param {Object} params - The parameters for the date formatting.
234
+ * @param {Date} params.value - The date to format.
235
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
236
+ * @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
237
+ * @returns {string} The formatted date.
238
+ */
239
+ function datetime(_a) {
240
+ var value = _a.value, languages = _a.languages, options = _a.options;
241
+ return (0, _format_1._datetime)({ value: value, languages: languages, options: options });
242
+ }
243
+ /**
244
+ * Formats a currency value according to the specified languages, currency, and options.
245
+ * @param {Object} params - The parameters for the currency formatting.
246
+ * @param {number} params.value - The currency value to format.
247
+ * @param {string} params.currency - The currency code (e.g., 'USD').
248
+ * @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
249
+ * @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
250
+ * @returns {string} The formatted currency value.
251
+ */
252
+ function currency(_a) {
253
+ var value = _a.value, languages = _a.languages, currency = _a.currency, options = _a.options;
254
+ return (0, _format_1._currency)({ value: value, languages: languages, currency: currency, options: options });
255
+ }
@@ -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.58",
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
- }