generaltranslation 2.0.55 → 2.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{cjs/codes → codes}/codes.js +71 -67
- package/dist/{cjs/codes → codes}/getLanguageDirection.js +5 -5
- package/dist/format/_format.js +62 -0
- package/dist/{cjs/index.d.ts → index.d.ts} +48 -27
- package/dist/index.js +243 -0
- package/dist/translation/_translate.js +109 -0
- package/dist/{mjs/translation/_bundleTranslation.d.ts → translation/_translateBundle.d.ts} +2 -9
- package/dist/translation/_translateBundle.js +95 -0
- package/dist/translation/_translateReact.js +107 -0
- package/dist/{cjs/translation/_updateRemoteDictionary.d.ts → translation/_updateProjectDictionary.d.ts} +1 -1
- package/dist/translation/_updateProjectDictionary.js +83 -0
- package/package.json +7 -7
- package/dist/cjs/index.js +0 -143
- package/dist/cjs/translation/_bundleTranslation.d.ts +0 -22
- package/dist/cjs/translation/_bundleTranslation.js +0 -42
- package/dist/cjs/translation/_intl.js +0 -52
- package/dist/cjs/translation/_translate.js +0 -51
- package/dist/cjs/translation/_translateReactChildren.js +0 -56
- package/dist/cjs/translation/_updateRemoteDictionary.js +0 -30
- package/dist/mjs/codes/15924/CodeToScript.json +0 -215
- package/dist/mjs/codes/15924/ScriptToCode.json +0 -215
- package/dist/mjs/codes/3166/CodeToRegion.json +0 -296
- package/dist/mjs/codes/3166/RegionToCode.json +0 -296
- package/dist/mjs/codes/639-1/CodeToLanguage.json +0 -185
- package/dist/mjs/codes/639-1/LanguageToCode.json +0 -227
- package/dist/mjs/codes/639-3/CodeToLanguageTriletter.json +0 -186
- package/dist/mjs/codes/639-3/LanguageToCodeTriletter.json +0 -228
- package/dist/mjs/codes/codes.d.ts +0 -8
- package/dist/mjs/codes/codes.js +0 -305
- package/dist/mjs/codes/getLanguageDirection.d.ts +0 -1
- package/dist/mjs/codes/getLanguageDirection.js +0 -68
- package/dist/mjs/codes/predefined/Predefined.json +0 -28
- package/dist/mjs/index.d.ts +0 -153
- package/dist/mjs/index.js +0 -130
- package/dist/mjs/translation/_bundleTranslation.js +0 -39
- package/dist/mjs/translation/_intl.d.ts +0 -1
- package/dist/mjs/translation/_intl.js +0 -49
- package/dist/mjs/translation/_translate.d.ts +0 -1
- package/dist/mjs/translation/_translate.js +0 -48
- package/dist/mjs/translation/_translateReactChildren.d.ts +0 -1
- package/dist/mjs/translation/_translateReactChildren.js +0 -53
- package/dist/mjs/translation/_updateRemoteDictionary.d.ts +0 -17
- package/dist/mjs/translation/_updateRemoteDictionary.js +0 -27
- /package/dist/{cjs/codes → codes}/15924/CodeToScript.json +0 -0
- /package/dist/{cjs/codes → codes}/15924/ScriptToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/3166/CodeToRegion.json +0 -0
- /package/dist/{cjs/codes → codes}/3166/RegionToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/639-1/CodeToLanguage.json +0 -0
- /package/dist/{cjs/codes → codes}/639-1/LanguageToCode.json +0 -0
- /package/dist/{cjs/codes → codes}/639-3/CodeToLanguageTriletter.json +0 -0
- /package/dist/{cjs/codes → codes}/639-3/LanguageToCodeTriletter.json +0 -0
- /package/dist/{cjs/codes → codes}/codes.d.ts +0 -0
- /package/dist/{cjs/codes → codes}/getLanguageDirection.d.ts +0 -0
- /package/dist/{cjs/codes → codes}/predefined/Predefined.json +0 -0
- /package/dist/{cjs/translation/_intl.d.ts → format/_format.d.ts} +0 -0
- /package/dist/{cjs/translation → translation}/_translate.d.ts +0 -0
- /package/dist/{cjs/translation/_translateReactChildren.d.ts → translation/_translateReact.d.ts} +0 -0
package/dist/index.js
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
"use strict";
|
2
|
+
// `generaltranslation` language toolkit
|
3
|
+
// © 2024, General Translation, Inc.
|
4
|
+
var __assign = (this && this.__assign) || function () {
|
5
|
+
__assign = Object.assign || function(t) {
|
6
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
7
|
+
s = arguments[i];
|
8
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
9
|
+
t[p] = s[p];
|
10
|
+
}
|
11
|
+
return t;
|
12
|
+
};
|
13
|
+
return __assign.apply(this, arguments);
|
14
|
+
};
|
15
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
16
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
17
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
18
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
19
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
20
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
21
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
22
|
+
});
|
23
|
+
};
|
24
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
25
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
26
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
27
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
28
|
+
function step(op) {
|
29
|
+
if (f) throw new TypeError("Generator is already executing.");
|
30
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
31
|
+
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;
|
32
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
33
|
+
switch (op[0]) {
|
34
|
+
case 0: case 1: t = op; break;
|
35
|
+
case 4: _.label++; return { value: op[1], done: false };
|
36
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
37
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
38
|
+
default:
|
39
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
40
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
41
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
42
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
43
|
+
if (t[2]) _.ops.pop();
|
44
|
+
_.trys.pop(); continue;
|
45
|
+
}
|
46
|
+
op = body.call(thisArg, _);
|
47
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
48
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
49
|
+
}
|
50
|
+
};
|
51
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
52
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
53
|
+
};
|
54
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
55
|
+
exports.currency = exports.datetime = exports.num = exports.standardizeLanguageCode = exports.isValidLanguageCode = exports.getLanguageDirection = void 0;
|
56
|
+
exports.getLanguageObject = getLanguageObject;
|
57
|
+
exports.getLanguageCode = getLanguageCode;
|
58
|
+
exports.getLanguageName = getLanguageName;
|
59
|
+
exports.isSameLanguage = isSameLanguage;
|
60
|
+
// ----- IMPORTS ----- //
|
61
|
+
var codes_1 = require("./codes/codes");
|
62
|
+
var getLanguageDirection_1 = __importDefault(require("./codes/getLanguageDirection"));
|
63
|
+
var _translateBundle_1 = __importDefault(require("./translation/_translateBundle"));
|
64
|
+
var _translate_1 = __importDefault(require("./translation/_translate"));
|
65
|
+
var _translateReact_1 = __importDefault(require("./translation/_translateReact"));
|
66
|
+
var _updateProjectDictionary_1 = __importDefault(require("./translation/_updateProjectDictionary"));
|
67
|
+
var _format_1 = require("./format/_format");
|
68
|
+
// ----- CORE CLASS ----- //
|
69
|
+
var getDefaultFromEnv = function (VARIABLE) {
|
70
|
+
if (typeof process !== 'undefined' && process.env) {
|
71
|
+
return process.env[VARIABLE] || '';
|
72
|
+
}
|
73
|
+
return '';
|
74
|
+
};
|
75
|
+
/**
|
76
|
+
* GT is the core driver for the General Translation library.
|
77
|
+
*/
|
78
|
+
var GT = /** @class */ (function () {
|
79
|
+
/**
|
80
|
+
* Constructs an instance of the GT class.
|
81
|
+
*
|
82
|
+
* @param {GTConstructorParams} [params] - The parameters for initializing the GT instance.
|
83
|
+
* @param {string} [params.apiKey=''] - The API key for accessing the translation service.
|
84
|
+
* @param {string} [params.defaultLanguage='en'] - The default language for translations.
|
85
|
+
* @param {string} [params.projectID=''] - The project ID for the translation service.
|
86
|
+
* @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
|
87
|
+
*/
|
88
|
+
function GT(_a) {
|
89
|
+
var _b = _a === void 0 ? {} : _a, _c = _b.apiKey, apiKey = _c === void 0 ? '' : _c, _d = _b.defaultLanguage, defaultLanguage = _d === void 0 ? 'en' : _d, _e = _b.projectID, projectID = _e === void 0 ? '' : _e, _f = _b.baseURL, baseURL = _f === void 0 ? 'https://prod.gtx.dev' : _f;
|
90
|
+
this.apiKey = apiKey || getDefaultFromEnv('GT_API_KEY');
|
91
|
+
this.projectID = projectID || getDefaultFromEnv('GT_PROJECT_ID');
|
92
|
+
this.defaultLanguage = defaultLanguage.toLowerCase();
|
93
|
+
this.baseURL = baseURL;
|
94
|
+
}
|
95
|
+
/**
|
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
|
+
*/
|
107
|
+
GT.prototype.translate = function (content, targetLanguage, metadata) {
|
108
|
+
return __awaiter(this, void 0, void 0, function () {
|
109
|
+
return __generator(this, function (_a) {
|
110
|
+
switch (_a.label) {
|
111
|
+
case 0: return [4 /*yield*/, (0, _translate_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
|
112
|
+
case 1: return [2 /*return*/, _a.sent()];
|
113
|
+
}
|
114
|
+
});
|
115
|
+
});
|
116
|
+
};
|
117
|
+
/**
|
118
|
+
* Translates the content of React children elements.
|
119
|
+
*
|
120
|
+
* @param {Object} params - The parameters for the translation.
|
121
|
+
* @param {any} params.content - The React children content to be translated.
|
122
|
+
* @param {string} params.targetLanguage - The target language for the translation.
|
123
|
+
* @param {Object} params.metadata - Additional metadata for the translation process.
|
124
|
+
*
|
125
|
+
* @returns {Promise<any>} - A promise that resolves to the translated content.
|
126
|
+
*/
|
127
|
+
GT.prototype.translateReact = function (content, targetLanguage, metadata) {
|
128
|
+
return __awaiter(this, void 0, void 0, function () {
|
129
|
+
return __generator(this, function (_a) {
|
130
|
+
switch (_a.label) {
|
131
|
+
case 0: return [4 /*yield*/, (0, _translateReact_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
|
132
|
+
case 1: return [2 /*return*/, _a.sent()];
|
133
|
+
}
|
134
|
+
});
|
135
|
+
});
|
136
|
+
};
|
137
|
+
/**
|
138
|
+
* Bundles multiple translation requests and sends them to the server.
|
139
|
+
* @param requests - Array of requests to be processed and sent.
|
140
|
+
* @returns A promise that resolves to an array of processed results.
|
141
|
+
*/
|
142
|
+
GT.prototype.translateBundle = function (requests) {
|
143
|
+
return __awaiter(this, void 0, void 0, function () {
|
144
|
+
return __generator(this, function (_a) {
|
145
|
+
return [2 /*return*/, (0, _translateBundle_1.default)(this, requests)];
|
146
|
+
});
|
147
|
+
});
|
148
|
+
};
|
149
|
+
/**
|
150
|
+
* Pushes updates to a remotely cached translation dictionary.
|
151
|
+
* @param {Update[]} updates - Array of updates with optional targetLanguage.
|
152
|
+
* @param {string[]} [languages] - Array of languages to be updated.
|
153
|
+
* @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
|
154
|
+
* @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
|
155
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
|
156
|
+
*/
|
157
|
+
GT.prototype.updateProjectDictionary = function (updates_1) {
|
158
|
+
return __awaiter(this, arguments, void 0, function (updates, languages, projectID, replace) {
|
159
|
+
if (languages === void 0) { languages = []; }
|
160
|
+
if (projectID === void 0) { projectID = this.projectID; }
|
161
|
+
if (replace === void 0) { replace = false; }
|
162
|
+
return __generator(this, function (_a) {
|
163
|
+
return [2 /*return*/, (0, _updateProjectDictionary_1.default)(this, updates, languages, projectID, replace)];
|
164
|
+
});
|
165
|
+
});
|
166
|
+
};
|
167
|
+
return GT;
|
168
|
+
}());
|
169
|
+
// ----- EXPORTS ----- //
|
170
|
+
// Export the class
|
171
|
+
exports.default = GT;
|
172
|
+
// Export the functions
|
173
|
+
/**
|
174
|
+
* Gets the writing direction for a given BCP 47 language code.
|
175
|
+
* @param {string} code - The BCP 47 language code to check.
|
176
|
+
* @returns {string} The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
|
177
|
+
*/
|
178
|
+
var getLanguageDirection = function (code) { return (0, getLanguageDirection_1.default)(code); };
|
179
|
+
exports.getLanguageDirection = getLanguageDirection;
|
180
|
+
/**
|
181
|
+
* Checks if a given BCP 47 language code is valid.
|
182
|
+
* @param {string} code - The BCP 47 language code to validate.
|
183
|
+
* @returns {boolean} True if the BCP 47 code is valid, false otherwise.
|
184
|
+
*/
|
185
|
+
var isValidLanguageCode = function (code) { return (0, codes_1._isValidLanguageCode)(code); };
|
186
|
+
exports.isValidLanguageCode = isValidLanguageCode;
|
187
|
+
/**
|
188
|
+
* Standardizes a BCP 47 language code to ensure correct formatting.
|
189
|
+
* @param {string} code - The BCP 47 language code to standardize.
|
190
|
+
* @returns {string} The standardized BCP 47 language code.
|
191
|
+
*/
|
192
|
+
var standardizeLanguageCode = function (code) { return (0, codes_1._standardizeLanguageCode)(code); };
|
193
|
+
exports.standardizeLanguageCode = standardizeLanguageCode;
|
194
|
+
function getLanguageObject(codes) {
|
195
|
+
return Array.isArray(codes) ? (0, codes_1._getLanguageObject)(codes) : (0, codes_1._getLanguageObject)(codes);
|
196
|
+
}
|
197
|
+
function getLanguageCode(languages) {
|
198
|
+
return (0, codes_1._getLanguageCode)(languages);
|
199
|
+
}
|
200
|
+
function getLanguageName(codes) {
|
201
|
+
return (0, codes_1._getLanguageName)(codes);
|
202
|
+
}
|
203
|
+
/**
|
204
|
+
* Checks if multiple BCP 47 language codes represent the same language.
|
205
|
+
* @param {string[]} codes - The BCP 47 language codes to compare.
|
206
|
+
* @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
|
207
|
+
*/
|
208
|
+
function isSameLanguage() {
|
209
|
+
var codes = [];
|
210
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
211
|
+
codes[_i] = arguments[_i];
|
212
|
+
}
|
213
|
+
return codes_1._isSameLanguage.apply(void 0, codes);
|
214
|
+
}
|
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;
|
@@ -0,0 +1,109 @@
|
|
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 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).
|
51
|
+
* @internal
|
52
|
+
**/
|
53
|
+
function _translate(gt, content, targetLanguage, metadata) {
|
54
|
+
return __awaiter(this, void 0, void 0, function () {
|
55
|
+
var controller, signal, response, _a, _b, _c, result, 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, "/translate"), {
|
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:
|
89
|
+
result = _d.sent();
|
90
|
+
return [2 /*return*/, result];
|
91
|
+
case 6:
|
92
|
+
error_1 = _d.sent();
|
93
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
94
|
+
console.error('Request timed out');
|
95
|
+
return [2 /*return*/, {
|
96
|
+
translation: content,
|
97
|
+
error: 'Request timed out'
|
98
|
+
}];
|
99
|
+
}
|
100
|
+
console.error(error_1);
|
101
|
+
return [2 /*return*/, {
|
102
|
+
translation: content,
|
103
|
+
error: error_1
|
104
|
+
}];
|
105
|
+
case 7: return [2 /*return*/];
|
106
|
+
}
|
107
|
+
});
|
108
|
+
});
|
109
|
+
}
|
@@ -1,12 +1,5 @@
|
|
1
1
|
export type Request = {
|
2
|
-
type: '
|
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
|
-
|
11
|
+
content: string;
|
19
12
|
targetLanguage: string;
|
20
13
|
metadata: Record<string, any>;
|
21
14
|
};
|
@@ -0,0 +1,95 @@
|
|
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 = _translateBundle;
|
40
|
+
/**
|
41
|
+
* Bundles multiple requests and sends them to the server.
|
42
|
+
* @param {{ baseURL: string, apiKey: string }} gt - Contains the baseURL and apiKey for the server request.
|
43
|
+
* @param {Request[]} requests - Array of requests to be processed and sent.
|
44
|
+
* @param {{ timeout?: number }} options - Additional options for the request, including timeout.
|
45
|
+
* @returns {Promise<Array<any | null>>} A promise that resolves to an array of processed results.
|
46
|
+
* @internal
|
47
|
+
*/
|
48
|
+
function _translateBundle(gt, requests) {
|
49
|
+
return __awaiter(this, void 0, void 0, function () {
|
50
|
+
var controller, signal, response, _a, _b, _c, resultArray, error_1;
|
51
|
+
var _d, _e, _f;
|
52
|
+
return __generator(this, function (_g) {
|
53
|
+
switch (_g.label) {
|
54
|
+
case 0:
|
55
|
+
controller = new AbortController();
|
56
|
+
signal = controller.signal;
|
57
|
+
if ((_f = (_e = (_d = requests[0]) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.metadata) === null || _f === void 0 ? void 0 : _f.timeout) {
|
58
|
+
setTimeout(function () { return controller.abort(); }, requests[0].data.metadata.timeout);
|
59
|
+
}
|
60
|
+
_g.label = 1;
|
61
|
+
case 1:
|
62
|
+
_g.trys.push([1, 6, , 7]);
|
63
|
+
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/bundle"), {
|
64
|
+
method: 'POST',
|
65
|
+
headers: {
|
66
|
+
'Content-Type': 'application/json',
|
67
|
+
'gtx-api-key': gt.apiKey,
|
68
|
+
},
|
69
|
+
body: JSON.stringify(requests),
|
70
|
+
signal: signal
|
71
|
+
})];
|
72
|
+
case 2:
|
73
|
+
response = _g.sent();
|
74
|
+
if (!!response.ok) return [3 /*break*/, 4];
|
75
|
+
_a = Error.bind;
|
76
|
+
_c = (_b = "".concat(response.status, ": ")).concat;
|
77
|
+
return [4 /*yield*/, response.text()];
|
78
|
+
case 3: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_g.sent()])]))();
|
79
|
+
case 4: return [4 /*yield*/, response.json()];
|
80
|
+
case 5:
|
81
|
+
resultArray = _g.sent();
|
82
|
+
return [2 /*return*/, resultArray];
|
83
|
+
case 6:
|
84
|
+
error_1 = _g.sent();
|
85
|
+
if (error_1 instanceof Error && error_1.name === 'AbortError') {
|
86
|
+
console.error('Request timed out');
|
87
|
+
return [2 /*return*/, Array.from(requests, function () { return ({ translation: null, error: 'Request timed out' }); })];
|
88
|
+
}
|
89
|
+
console.error(error_1);
|
90
|
+
return [2 /*return*/, Array.from(requests, function () { return ({ translation: null, error: error_1 }); })];
|
91
|
+
case 7: return [2 /*return*/];
|
92
|
+
}
|
93
|
+
});
|
94
|
+
});
|
95
|
+
}
|
@@ -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 = _translateReact;
|
40
|
+
/**
|
41
|
+
* Translates the given React children 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} children - The React children 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).
|
51
|
+
* @internal
|
52
|
+
**/
|
53
|
+
function _translateReact(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
|
+
}
|
@@ -11,7 +11,7 @@ export type Update = {
|
|
11
11
|
metadata: Record<string, any>;
|
12
12
|
};
|
13
13
|
};
|
14
|
-
export default function
|
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[]>;
|