generaltranslation 2.0.54 → 2.0.56

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. package/dist/{cjs/codes → codes}/codes.js +66 -63
  2. package/dist/{cjs/codes → codes}/getLanguageDirection.js +6 -6
  3. package/dist/index.js +228 -0
  4. package/dist/translation/_bundleTranslation.js +95 -0
  5. package/dist/translation/_intl.js +107 -0
  6. package/dist/translation/_translate.js +105 -0
  7. package/dist/translation/_translateReactChildren.js +107 -0
  8. package/dist/translation/_updateRemoteDictionary.js +83 -0
  9. package/package.json +7 -7
  10. package/dist/cjs/index.js +0 -162
  11. package/dist/cjs/translation/_bundleTranslation.js +0 -54
  12. package/dist/cjs/translation/_intl.js +0 -63
  13. package/dist/cjs/translation/_translate.js +0 -62
  14. package/dist/cjs/translation/_translateReactChildren.js +0 -67
  15. package/dist/cjs/translation/_updateRemoteDictionary.js +0 -41
  16. package/dist/mjs/codes/15924/CodeToScript.json +0 -215
  17. package/dist/mjs/codes/15924/ScriptToCode.json +0 -215
  18. package/dist/mjs/codes/3166/CodeToRegion.json +0 -296
  19. package/dist/mjs/codes/3166/RegionToCode.json +0 -296
  20. package/dist/mjs/codes/639-1/CodeToLanguage.json +0 -185
  21. package/dist/mjs/codes/639-1/LanguageToCode.json +0 -227
  22. package/dist/mjs/codes/639-3/CodeToLanguageTriletter.json +0 -186
  23. package/dist/mjs/codes/639-3/LanguageToCodeTriletter.json +0 -228
  24. package/dist/mjs/codes/codes.d.ts +0 -8
  25. package/dist/mjs/codes/codes.js +0 -306
  26. package/dist/mjs/codes/getLanguageDirection.d.ts +0 -1
  27. package/dist/mjs/codes/getLanguageDirection.js +0 -68
  28. package/dist/mjs/codes/predefined/Predefined.json +0 -28
  29. package/dist/mjs/index.d.ts +0 -153
  30. package/dist/mjs/index.js +0 -149
  31. package/dist/mjs/translation/_bundleTranslation.d.ts +0 -22
  32. package/dist/mjs/translation/_bundleTranslation.js +0 -51
  33. package/dist/mjs/translation/_intl.d.ts +0 -1
  34. package/dist/mjs/translation/_intl.js +0 -60
  35. package/dist/mjs/translation/_translate.d.ts +0 -1
  36. package/dist/mjs/translation/_translate.js +0 -59
  37. package/dist/mjs/translation/_translateReactChildren.d.ts +0 -1
  38. package/dist/mjs/translation/_translateReactChildren.js +0 -64
  39. package/dist/mjs/translation/_updateRemoteDictionary.d.ts +0 -17
  40. package/dist/mjs/translation/_updateRemoteDictionary.js +0 -38
  41. /package/dist/{cjs/codes → codes}/15924/CodeToScript.json +0 -0
  42. /package/dist/{cjs/codes → codes}/15924/ScriptToCode.json +0 -0
  43. /package/dist/{cjs/codes → codes}/3166/CodeToRegion.json +0 -0
  44. /package/dist/{cjs/codes → codes}/3166/RegionToCode.json +0 -0
  45. /package/dist/{cjs/codes → codes}/639-1/CodeToLanguage.json +0 -0
  46. /package/dist/{cjs/codes → codes}/639-1/LanguageToCode.json +0 -0
  47. /package/dist/{cjs/codes → codes}/639-3/CodeToLanguageTriletter.json +0 -0
  48. /package/dist/{cjs/codes → codes}/639-3/LanguageToCodeTriletter.json +0 -0
  49. /package/dist/{cjs/codes → codes}/codes.d.ts +0 -0
  50. /package/dist/{cjs/codes → codes}/getLanguageDirection.d.ts +0 -0
  51. /package/dist/{cjs/codes → codes}/predefined/Predefined.json +0 -0
  52. /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
  53. /package/dist/{cjs/translation → translation}/_bundleTranslation.d.ts +0 -0
  54. /package/dist/{cjs/translation → translation}/_intl.d.ts +0 -0
  55. /package/dist/{cjs/translation → translation}/_translate.d.ts +0 -0
  56. /package/dist/{cjs/translation → translation}/_translateReactChildren.d.ts +0 -0
  57. /package/dist/{cjs/translation → translation}/_updateRemoteDictionary.d.ts +0 -0
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- // ----- IMPORTS ----- //
3
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
4
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
4
  };
@@ -9,30 +8,30 @@ exports._standardizeLanguageCode = _standardizeLanguageCode;
9
8
  exports._isValidLanguageCode = _isValidLanguageCode;
10
9
  exports._getLanguageObject = _getLanguageObject;
11
10
  exports._isSameLanguage = _isSameLanguage;
11
+ // ----- IMPORTS ----- //
12
12
  // Import modules for mapping ISO 639 codes to language names and vice versa
13
- const CodeToLanguage_json_1 = __importDefault(require("./639-1/CodeToLanguage.json"));
14
- const LanguageToCode_json_1 = __importDefault(require("./639-1/LanguageToCode.json"));
15
- const CodeToLanguage = CodeToLanguage_json_1.default;
16
- const LanguageToCode = LanguageToCode_json_1.default;
13
+ var CodeToLanguage_json_1 = __importDefault(require("./639-1/CodeToLanguage.json"));
14
+ var LanguageToCode_json_1 = __importDefault(require("./639-1/LanguageToCode.json"));
15
+ var CodeToLanguage = CodeToLanguage_json_1.default;
16
+ var LanguageToCode = LanguageToCode_json_1.default;
17
17
  // Import modules for mapping ISO 639-3 codes (for languages without two-letter codes)
18
- const CodeToLanguageTriletter_json_1 = __importDefault(require("./639-3/CodeToLanguageTriletter.json"));
19
- const LanguageToCodeTriletter_json_1 = __importDefault(require("./639-3/LanguageToCodeTriletter.json"));
20
- const CodeToLanguageTriletter = CodeToLanguageTriletter_json_1.default;
21
- const LanguageToCodeTriletter = LanguageToCodeTriletter_json_1.default;
18
+ var CodeToLanguageTriletter_json_1 = __importDefault(require("./639-3/CodeToLanguageTriletter.json"));
19
+ var LanguageToCodeTriletter_json_1 = __importDefault(require("./639-3/LanguageToCodeTriletter.json"));
20
+ var CodeToLanguageTriletter = CodeToLanguageTriletter_json_1.default;
21
+ var LanguageToCodeTriletter = LanguageToCodeTriletter_json_1.default;
22
22
  // Import module for mapping ISO 15924 script codes to script names
23
- const ScriptToCode_json_1 = __importDefault(require("./15924/ScriptToCode.json"));
24
- const CodeToScript_json_1 = __importDefault(require("./15924/CodeToScript.json"));
25
- const ScriptToCode = ScriptToCode_json_1.default;
26
- const CodeToScript = CodeToScript_json_1.default;
23
+ var ScriptToCode_json_1 = __importDefault(require("./15924/ScriptToCode.json"));
24
+ var CodeToScript_json_1 = __importDefault(require("./15924/CodeToScript.json"));
25
+ var ScriptToCode = ScriptToCode_json_1.default;
26
+ var CodeToScript = CodeToScript_json_1.default;
27
27
  // Import module for mapping ISO 3166 region codes to region names
28
- const RegionToCode_json_1 = __importDefault(require("./3166/RegionToCode.json"));
29
- const CodeToRegion_json_1 = __importDefault(require("./3166/CodeToRegion.json"));
30
- const RegionToCode = RegionToCode_json_1.default;
31
- const CodeToRegion = CodeToRegion_json_1.default;
28
+ var RegionToCode_json_1 = __importDefault(require("./3166/RegionToCode.json"));
29
+ var CodeToRegion_json_1 = __importDefault(require("./3166/CodeToRegion.json"));
30
+ var RegionToCode = RegionToCode_json_1.default;
31
+ var CodeToRegion = CodeToRegion_json_1.default;
32
32
  // Import predefined common regions
33
- const Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
34
- const generaltranslation_1 = require("generaltranslation");
35
- const Predefined = Predefined_json_1.default;
33
+ var Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
34
+ var Predefined = Predefined_json_1.default;
36
35
  // ----- VALIDITY CHECKS ----- //
37
36
  /**
38
37
  * Ensures correct capitalization and formatting of a language code.
@@ -41,19 +40,19 @@ const Predefined = Predefined_json_1.default;
41
40
  * @internal
42
41
  */
43
42
  function _standardizeLanguageCode(code) {
44
- if (!(0, generaltranslation_1.isValidLanguageCode)(code))
43
+ if (!_isValidLanguageCode(code))
45
44
  return '';
46
- const codeParts = code.split('-');
47
- let result = `${codeParts[0].toLowerCase()}`;
45
+ var codeParts = code.split('-');
46
+ var result = "".concat(codeParts[0].toLowerCase());
48
47
  if (codeParts[1]) {
49
48
  if (codeParts[1].length === 4) {
50
- result += `-${_capitalize(codeParts[1])}`;
49
+ result += "-".concat(_capitalize(codeParts[1]));
51
50
  if (codeParts[2] && codeParts[2].length === 2) {
52
- result += `-${codeParts[2].toUpperCase()}`;
51
+ result += "-".concat(codeParts[2].toUpperCase());
53
52
  }
54
53
  }
55
54
  else if (codeParts[1].length === 2) {
56
- result += `-${codeParts[1].toUpperCase()}`;
55
+ result += "-".concat(codeParts[1].toUpperCase());
57
56
  }
58
57
  }
59
58
  return result;
@@ -67,7 +66,7 @@ function _standardizeLanguageCode(code) {
67
66
  function _isValidLanguageCode(code) {
68
67
  if (!code || typeof code !== 'string')
69
68
  return false;
70
- const codeParts = code.split('-');
69
+ var codeParts = code.split('-');
71
70
  if (!_mapCodeToLanguage(codeParts[0]))
72
71
  return false;
73
72
  if (codeParts[1]) {
@@ -92,7 +91,7 @@ function _isValidLanguageCode(code) {
92
91
  * @param {string} code - The code to capitalize.
93
92
  * @returns {string} The capitalized code.
94
93
  */
95
- const _capitalize = (code) => {
94
+ var _capitalize = function (code) {
96
95
  if (code.length === 0)
97
96
  return code;
98
97
  return code.charAt(0).toUpperCase() + code.slice(1).toLowerCase();
@@ -103,7 +102,7 @@ const _capitalize = (code) => {
103
102
  * @param {string} code - The ISO 639 code.
104
103
  * @returns {string} The language name.
105
104
  */
106
- const _mapCodeToLanguage = (code) => {
105
+ var _mapCodeToLanguage = function (code) {
107
106
  code = code === null || code === void 0 ? void 0 : code.toLowerCase();
108
107
  if ((code === null || code === void 0 ? void 0 : code.length) === 2) {
109
108
  return CodeToLanguage[code];
@@ -119,7 +118,7 @@ const _mapCodeToLanguage = (code) => {
119
118
  * @param {string} language - The language name.
120
119
  * @returns {string} BCP 47 language tag.
121
120
  */
122
- const _mapLanguageToCode = (language) => {
121
+ var _mapLanguageToCode = function (language) {
123
122
  language = language === null || language === void 0 ? void 0 : language.toLowerCase();
124
123
  return LanguageToCode[language] || LanguageToCodeTriletter[language] || '';
125
124
  };
@@ -128,7 +127,7 @@ const _mapLanguageToCode = (language) => {
128
127
  * @param {string} code - The ISO 15924 code.
129
128
  * @returns {string} The script name.
130
129
  */
131
- const _mapCodeToScript = (code) => {
130
+ var _mapCodeToScript = function (code) {
132
131
  code = _capitalize(code);
133
132
  return CodeToScript[code] || '';
134
133
  };
@@ -137,7 +136,7 @@ const _mapCodeToScript = (code) => {
137
136
  * @param {string} script - The script name.
138
137
  * @returns {string} The ISO 15924 code.
139
138
  */
140
- const _mapScriptToCode = (script) => {
139
+ var _mapScriptToCode = function (script) {
141
140
  script = script === null || script === void 0 ? void 0 : script.toLowerCase();
142
141
  return ScriptToCode[script] || '';
143
142
  };
@@ -146,7 +145,7 @@ const _mapScriptToCode = (script) => {
146
145
  * @param {string} code - The ISO 3166 code.
147
146
  * @returns {string} The region name.
148
147
  */
149
- const _mapCodeToRegion = (code) => {
148
+ var _mapCodeToRegion = function (code) {
150
149
  code = code === null || code === void 0 ? void 0 : code.toUpperCase();
151
150
  return CodeToRegion[code] || '';
152
151
  };
@@ -155,7 +154,7 @@ const _mapCodeToRegion = (code) => {
155
154
  * @param {string} region - The region name.
156
155
  * @returns {string} The ISO 3166 code.
157
156
  */
158
- const _mapRegionToCode = (region) => {
157
+ var _mapRegionToCode = function (region) {
159
158
  region = region === null || region === void 0 ? void 0 : region.toLowerCase();
160
159
  return RegionToCode[region] || '';
161
160
  };
@@ -170,11 +169,11 @@ function _getLanguageObject(codes) {
170
169
  * @param {string} code - The language code.
171
170
  * @returns {LanguageObject|null} The language object.
172
171
  */
173
- const _handleGetLanguageObject = (code) => {
174
- if (!(0, generaltranslation_1.isValidLanguageCode)(code))
172
+ var _handleGetLanguageObject = function (code) {
173
+ if (!_isValidLanguageCode(code))
175
174
  return null;
176
- const codeParts = code.split('-');
177
- let languageObject = {
175
+ var codeParts = code.split('-');
176
+ var languageObject = {
178
177
  language: _mapCodeToLanguage(codeParts[0]),
179
178
  };
180
179
  if (codeParts[1]) {
@@ -197,7 +196,7 @@ const _handleGetLanguageObject = (code) => {
197
196
  * @returns {string|string[]} The language name(s).
198
197
  * @internal
199
198
  */
200
- const _getLanguageName = (codes) => {
199
+ var _getLanguageName = function (codes) {
201
200
  return Array.isArray(codes) ? codes.map(_handleGetLanguageName) : _handleGetLanguageName(codes);
202
201
  };
203
202
  exports._getLanguageName = _getLanguageName;
@@ -206,20 +205,20 @@ exports._getLanguageName = _getLanguageName;
206
205
  * @param {string} code - The language code.
207
206
  * @returns {string} The language name.
208
207
  */
209
- const _handleGetLanguageName = (code) => {
208
+ var _handleGetLanguageName = function (code) {
210
209
  if (!_isValidLanguageCode(code))
211
210
  return '';
212
211
  if (Predefined[code])
213
212
  return Predefined[code];
214
- const languageObject = _handleGetLanguageObject(code);
213
+ var languageObject = _handleGetLanguageObject(code);
215
214
  if (!languageObject)
216
215
  return '';
217
- let result = languageObject.language;
216
+ var result = languageObject.language;
218
217
  if (languageObject.script) {
219
- result += `, ${languageObject.script}`;
218
+ result += ", ".concat(languageObject.script);
220
219
  }
221
220
  if (languageObject.region) {
222
- result += `, ${languageObject.region}`;
221
+ result += ", ".concat(languageObject.region);
223
222
  }
224
223
  return result;
225
224
  };
@@ -230,7 +229,7 @@ const _handleGetLanguageName = (code) => {
230
229
  * @returns {string|string[]} The language code(s).
231
230
  * @internal
232
231
  */
233
- const _getLanguageCode = (languages) => {
232
+ var _getLanguageCode = function (languages) {
234
233
  return Array.isArray(languages) ? languages.map(_handleGetLanguageCode) : _handleGetLanguageCode(languages);
235
234
  };
236
235
  exports._getLanguageCode = _getLanguageCode;
@@ -239,7 +238,7 @@ exports._getLanguageCode = _getLanguageCode;
239
238
  * @param {string|LanguageObject} language - The language name or object.
240
239
  * @returns {string} The language code.
241
240
  */
242
- const _handleGetLanguageCode = (language) => {
241
+ var _handleGetLanguageCode = function (language) {
243
242
  if (typeof language === 'string')
244
243
  return _handleGetLanguageCodeFromString(language);
245
244
  return _handleGetLanguageCodeFromObject(language);
@@ -249,24 +248,24 @@ const _handleGetLanguageCode = (language) => {
249
248
  * @param {string} language - The language name.
250
249
  * @returns {string} The language code.
251
250
  */
252
- const _handleGetLanguageCodeFromString = (language) => {
253
- const subtagStrings = language.split(',').map(string => string.trim());
254
- let code = _mapLanguageToCode(subtagStrings[0]);
251
+ var _handleGetLanguageCodeFromString = function (language) {
252
+ var subtagStrings = language.split(',').map(function (string) { return string.trim(); });
253
+ var code = _mapLanguageToCode(subtagStrings[0]);
255
254
  if (code) {
256
255
  if (subtagStrings.length === 3) {
257
- code += `-${_mapScriptToCode(subtagStrings[1])}`;
258
- code += `-${_mapRegionToCode(subtagStrings[2])}`;
256
+ code += "-".concat(_mapScriptToCode(subtagStrings[1]));
257
+ code += "-".concat(_mapRegionToCode(subtagStrings[2]));
259
258
  }
260
259
  else if (subtagStrings.length === 2) {
261
- let tag = _mapScriptToCode(subtagStrings[1]);
260
+ var tag = _mapScriptToCode(subtagStrings[1]);
262
261
  if (!tag)
263
262
  tag = _mapRegionToCode(subtagStrings[1]);
264
263
  if (tag)
265
- code += `-${tag}`;
264
+ code += "-".concat(tag);
266
265
  }
267
266
  }
268
267
  if (!code) {
269
- for (const key in Predefined) {
268
+ for (var key in Predefined) {
270
269
  if (Predefined[key] === language) {
271
270
  return key;
272
271
  }
@@ -279,34 +278,38 @@ const _handleGetLanguageCodeFromString = (language) => {
279
278
  * @param {LanguageObject} languageObject - The language object.
280
279
  * @returns {string} The language code.
281
280
  */
282
- const _handleGetLanguageCodeFromObject = (languageObject) => {
281
+ var _handleGetLanguageCodeFromObject = function (languageObject) {
283
282
  if (!(languageObject === null || languageObject === void 0 ? void 0 : languageObject.language))
284
283
  return '';
285
- let code = languageObject.language.toLowerCase();
284
+ var code = languageObject.language.toLowerCase();
286
285
  if (languageObject.script) {
287
- code += `-${_capitalize(languageObject.script)}`;
286
+ code += "-".concat(_capitalize(languageObject.script));
288
287
  }
289
288
  if (languageObject.region) {
290
- code += `-${languageObject.region.toUpperCase()}`;
289
+ code += "-".concat(languageObject.region.toUpperCase());
291
290
  }
292
291
  return _isValidLanguageCode(code) ? code : '';
293
292
  };
294
293
  /**
295
294
  * @internal
296
295
  */
297
- function _isSameLanguage(...codes) {
296
+ function _isSameLanguage() {
297
+ var codes = [];
298
+ for (var _i = 0; _i < arguments.length; _i++) {
299
+ codes[_i] = arguments[_i];
300
+ }
298
301
  // Flatten the array in case the codes are provided as an array
299
302
  if (codes.length === 1 && Array.isArray(codes[0])) {
300
303
  codes = codes[0];
301
304
  }
302
305
  if (codes.length < 2)
303
306
  return false;
304
- let language = null;
305
- for (let i = 0; i < codes.length; i++) {
307
+ var language = null;
308
+ for (var i = 0; i < codes.length; i++) {
306
309
  if (typeof codes[i] !== 'string')
307
310
  return false;
308
- const languageCode = codes[i].split('-')[0];
309
- const currentLanguage = _mapCodeToLanguage(languageCode);
311
+ var languageCode = codes[i].split('-')[0];
312
+ var currentLanguage = _mapCodeToLanguage(languageCode);
310
313
  if (language === null) {
311
314
  language = currentLanguage;
312
315
  }
@@ -27,7 +27,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27
27
  *
28
28
  * @type {string[]}
29
29
  */
30
- const rtlLanguages = [
30
+ var rtlLanguages = [
31
31
  'ae', /* Avestan */
32
32
  'ar', /* 'العربية', Arabic */
33
33
  'arc', /* Aramaic */
@@ -50,7 +50,7 @@ const rtlLanguages = [
50
50
  'yi' /* 'ייִדיש', Yiddish */
51
51
  ];
52
52
  /* ----- END YAHOO COPYRIGHT ----- */
53
- const codes_1 = require("./codes");
53
+ var codes_js_1 = require("./codes.js");
54
54
  /**
55
55
  * Get the text direction for a given language code.
56
56
  *
@@ -59,13 +59,13 @@ const codes_1 = require("./codes");
59
59
  * @internal
60
60
  */
61
61
  function _getLanguageDirection(code) {
62
- return rtlLanguages.some(language => {
63
- const lo = (0, codes_1._getLanguageObject)(language);
64
- const clo = (0, codes_1._getLanguageObject)(code);
62
+ return rtlLanguages.some(function (language) {
63
+ var lo = (0, codes_js_1._getLanguageObject)(language);
64
+ var clo = (0, codes_js_1._getLanguageObject)(code);
65
65
  if (!lo || !clo)
66
66
  return false;
67
67
  if (lo.script && clo.script && lo.script !== clo.script)
68
68
  return false;
69
- return (0, codes_1._isSameLanguage)(code, language);
69
+ return (0, codes_js_1._isSameLanguage)(code, language);
70
70
  }) ? 'rtl' : 'ltr';
71
71
  }
package/dist/index.js ADDED
@@ -0,0 +1,228 @@
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.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 _bundleTranslation_1 = __importDefault(require("./translation/_bundleTranslation"));
64
+ var _intl_1 = __importDefault(require("./translation/_intl"));
65
+ var _translate_1 = __importDefault(require("./translation/_translate"));
66
+ var _translateReactChildren_1 = __importDefault(require("./translation/_translateReactChildren"));
67
+ var _updateRemoteDictionary_1 = __importDefault(require("./translation/_updateRemoteDictionary"));
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
+ * @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
+ */
102
+ GT.prototype.translate = function (content, targetLanguage, metadata) {
103
+ return __awaiter(this, void 0, void 0, function () {
104
+ return __generator(this, function (_a) {
105
+ switch (_a.label) {
106
+ case 0: return [4 /*yield*/, (0, _translate_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
107
+ case 1: return [2 /*return*/, _a.sent()];
108
+ }
109
+ });
110
+ });
111
+ };
112
+ /**
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
+ * Translates the content of React children elements.
132
+ *
133
+ * @param {Object} params - The parameters for the translation.
134
+ * @param {any} params.content - The React children content to be translated.
135
+ * @param {string} params.targetLanguage - The target language for the translation.
136
+ * @param {Object} params.metadata - Additional metadata for the translation process.
137
+ *
138
+ * @returns {Promise<any>} - A promise that resolves to the translated content.
139
+ */
140
+ GT.prototype.translateReactChildren = function (content, targetLanguage, metadata) {
141
+ return __awaiter(this, void 0, void 0, function () {
142
+ return __generator(this, function (_a) {
143
+ switch (_a.label) {
144
+ case 0: return [4 /*yield*/, (0, _translateReactChildren_1.default)(this, content, targetLanguage, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
145
+ case 1: return [2 /*return*/, _a.sent()];
146
+ }
147
+ });
148
+ });
149
+ };
150
+ /**
151
+ * Bundles multiple translation requests and sends them to the server.
152
+ * @param requests - Array of requests to be processed and sent.
153
+ * @returns A promise that resolves to an array of processed results.
154
+ */
155
+ GT.prototype.bundleTranslation = function (requests) {
156
+ return __awaiter(this, void 0, void 0, function () {
157
+ return __generator(this, function (_a) {
158
+ return [2 /*return*/, (0, _bundleTranslation_1.default)(this, requests)];
159
+ });
160
+ });
161
+ };
162
+ /**
163
+ * Pushes updates to a remotely cached translation dictionary.
164
+ * @param {Update[]} updates - Array of updates with optional targetLanguage.
165
+ * @param {string[]} [languages] - Array of languages to be updated.
166
+ * @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
167
+ * @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
168
+ * @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
169
+ */
170
+ GT.prototype.updateRemoteDictionary = function (updates_1) {
171
+ return __awaiter(this, arguments, void 0, function (updates, languages, projectID, replace) {
172
+ if (languages === void 0) { languages = []; }
173
+ if (projectID === void 0) { projectID = this.projectID; }
174
+ if (replace === void 0) { replace = false; }
175
+ return __generator(this, function (_a) {
176
+ return [2 /*return*/, (0, _updateRemoteDictionary_1.default)(this, updates, languages, projectID, replace)];
177
+ });
178
+ });
179
+ };
180
+ return GT;
181
+ }());
182
+ // ----- EXPORTS ----- //
183
+ // Export the class
184
+ exports.default = GT;
185
+ // Export the functions
186
+ /**
187
+ * Gets the writing direction for a given BCP 47 language code.
188
+ * @param {string} code - The BCP 47 language code to check.
189
+ * @returns {string} The language direction ('ltr' for left-to-right or 'rtl' for right-to-left).
190
+ */
191
+ var getLanguageDirection = function (code) { return (0, getLanguageDirection_1.default)(code); };
192
+ exports.getLanguageDirection = getLanguageDirection;
193
+ /**
194
+ * Checks if a given BCP 47 language code is valid.
195
+ * @param {string} code - The BCP 47 language code to validate.
196
+ * @returns {boolean} True if the BCP 47 code is valid, false otherwise.
197
+ */
198
+ var isValidLanguageCode = function (code) { return (0, codes_1._isValidLanguageCode)(code); };
199
+ exports.isValidLanguageCode = isValidLanguageCode;
200
+ /**
201
+ * Standardizes a BCP 47 language code to ensure correct formatting.
202
+ * @param {string} code - The BCP 47 language code to standardize.
203
+ * @returns {string} The standardized BCP 47 language code.
204
+ */
205
+ var standardizeLanguageCode = function (code) { return (0, codes_1._standardizeLanguageCode)(code); };
206
+ exports.standardizeLanguageCode = standardizeLanguageCode;
207
+ function getLanguageObject(codes) {
208
+ return Array.isArray(codes) ? (0, codes_1._getLanguageObject)(codes) : (0, codes_1._getLanguageObject)(codes);
209
+ }
210
+ function getLanguageCode(languages) {
211
+ return (0, codes_1._getLanguageCode)(languages);
212
+ }
213
+ function getLanguageName(codes) {
214
+ return (0, codes_1._getLanguageName)(codes);
215
+ }
216
+ /**
217
+ * Checks if multiple BCP 47 language codes represent the same language.
218
+ * @param {string[]} codes - The BCP 47 language codes to compare.
219
+ * @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
220
+ */
221
+ function isSameLanguage() {
222
+ var codes = [];
223
+ for (var _i = 0; _i < arguments.length; _i++) {
224
+ codes[_i] = arguments[_i];
225
+ }
226
+ return codes_1._isSameLanguage.apply(void 0, codes);
227
+ }
228
+ ;
@@ -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 = _bundleTranslation;
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 _bundleTranslation(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
+ }