generaltranslation 2.0.55 → 2.0.57

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