generaltranslation 2.0.55 → 2.0.56
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/{cjs/codes → codes}/codes.js +71 -67
- package/dist/{cjs/codes → codes}/getLanguageDirection.js +5 -5
- package/dist/index.js +228 -0
- package/dist/translation/_bundleTranslation.js +95 -0
- package/dist/translation/_intl.js +107 -0
- package/dist/translation/_translate.js +105 -0
- package/dist/translation/_translateReactChildren.js +107 -0
- package/dist/translation/_updateRemoteDictionary.js +83 -0
- package/package.json +7 -7
- package/dist/cjs/index.js +0 -143
- 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.d.ts +0 -22
- 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/index.d.ts → index.d.ts} +0 -0
- /package/dist/{cjs/translation → translation}/_bundleTranslation.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_intl.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_translate.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_translateReactChildren.d.ts +0 -0
- /package/dist/{cjs/translation → translation}/_updateRemoteDictionary.d.ts +0 -0
@@ -10,28 +10,28 @@ exports._getLanguageObject = _getLanguageObject;
|
|
10
10
|
exports._isSameLanguage = _isSameLanguage;
|
11
11
|
// ----- IMPORTS ----- //
|
12
12
|
// Import modules for mapping ISO 639 codes to language names and vice versa
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
34
|
-
|
33
|
+
var Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
|
34
|
+
var Predefined = Predefined_json_1.default;
|
35
35
|
// ----- VALIDITY CHECKS ----- //
|
36
36
|
/**
|
37
37
|
* Ensures correct capitalization and formatting of a language code.
|
@@ -42,17 +42,17 @@ const Predefined = Predefined_json_1.default;
|
|
42
42
|
function _standardizeLanguageCode(code) {
|
43
43
|
if (!_isValidLanguageCode(code))
|
44
44
|
return '';
|
45
|
-
|
46
|
-
|
45
|
+
var codeParts = code.split('-');
|
46
|
+
var result = "".concat(codeParts[0].toLowerCase());
|
47
47
|
if (codeParts[1]) {
|
48
48
|
if (codeParts[1].length === 4) {
|
49
|
-
result +=
|
49
|
+
result += "-".concat(_capitalize(codeParts[1]));
|
50
50
|
if (codeParts[2] && codeParts[2].length === 2) {
|
51
|
-
result +=
|
51
|
+
result += "-".concat(codeParts[2].toUpperCase());
|
52
52
|
}
|
53
53
|
}
|
54
54
|
else if (codeParts[1].length === 2) {
|
55
|
-
result +=
|
55
|
+
result += "-".concat(codeParts[1].toUpperCase());
|
56
56
|
}
|
57
57
|
}
|
58
58
|
return result;
|
@@ -66,7 +66,7 @@ function _standardizeLanguageCode(code) {
|
|
66
66
|
function _isValidLanguageCode(code) {
|
67
67
|
if (!code || typeof code !== 'string')
|
68
68
|
return false;
|
69
|
-
|
69
|
+
var codeParts = code.split('-');
|
70
70
|
if (!_mapCodeToLanguage(codeParts[0]))
|
71
71
|
return false;
|
72
72
|
if (codeParts[1]) {
|
@@ -91,7 +91,7 @@ function _isValidLanguageCode(code) {
|
|
91
91
|
* @param {string} code - The code to capitalize.
|
92
92
|
* @returns {string} The capitalized code.
|
93
93
|
*/
|
94
|
-
|
94
|
+
var _capitalize = function (code) {
|
95
95
|
if (code.length === 0)
|
96
96
|
return code;
|
97
97
|
return code.charAt(0).toUpperCase() + code.slice(1).toLowerCase();
|
@@ -102,12 +102,12 @@ const _capitalize = (code) => {
|
|
102
102
|
* @param {string} code - The ISO 639 code.
|
103
103
|
* @returns {string} The language name.
|
104
104
|
*/
|
105
|
-
|
106
|
-
code = code
|
107
|
-
if (code
|
105
|
+
var _mapCodeToLanguage = function (code) {
|
106
|
+
code = code === null || code === void 0 ? void 0 : code.toLowerCase();
|
107
|
+
if ((code === null || code === void 0 ? void 0 : code.length) === 2) {
|
108
108
|
return CodeToLanguage[code];
|
109
109
|
}
|
110
|
-
if (code
|
110
|
+
if ((code === null || code === void 0 ? void 0 : code.length) === 3) {
|
111
111
|
return CodeToLanguageTriletter[code];
|
112
112
|
}
|
113
113
|
return '';
|
@@ -118,8 +118,8 @@ const _mapCodeToLanguage = (code) => {
|
|
118
118
|
* @param {string} language - The language name.
|
119
119
|
* @returns {string} BCP 47 language tag.
|
120
120
|
*/
|
121
|
-
|
122
|
-
language = language
|
121
|
+
var _mapLanguageToCode = function (language) {
|
122
|
+
language = language === null || language === void 0 ? void 0 : language.toLowerCase();
|
123
123
|
return LanguageToCode[language] || LanguageToCodeTriletter[language] || '';
|
124
124
|
};
|
125
125
|
/**
|
@@ -127,7 +127,7 @@ const _mapLanguageToCode = (language) => {
|
|
127
127
|
* @param {string} code - The ISO 15924 code.
|
128
128
|
* @returns {string} The script name.
|
129
129
|
*/
|
130
|
-
|
130
|
+
var _mapCodeToScript = function (code) {
|
131
131
|
code = _capitalize(code);
|
132
132
|
return CodeToScript[code] || '';
|
133
133
|
};
|
@@ -136,8 +136,8 @@ const _mapCodeToScript = (code) => {
|
|
136
136
|
* @param {string} script - The script name.
|
137
137
|
* @returns {string} The ISO 15924 code.
|
138
138
|
*/
|
139
|
-
|
140
|
-
script = script
|
139
|
+
var _mapScriptToCode = function (script) {
|
140
|
+
script = script === null || script === void 0 ? void 0 : script.toLowerCase();
|
141
141
|
return ScriptToCode[script] || '';
|
142
142
|
};
|
143
143
|
/**
|
@@ -145,8 +145,8 @@ const _mapScriptToCode = (script) => {
|
|
145
145
|
* @param {string} code - The ISO 3166 code.
|
146
146
|
* @returns {string} The region name.
|
147
147
|
*/
|
148
|
-
|
149
|
-
code = code
|
148
|
+
var _mapCodeToRegion = function (code) {
|
149
|
+
code = code === null || code === void 0 ? void 0 : code.toUpperCase();
|
150
150
|
return CodeToRegion[code] || '';
|
151
151
|
};
|
152
152
|
/**
|
@@ -154,8 +154,8 @@ const _mapCodeToRegion = (code) => {
|
|
154
154
|
* @param {string} region - The region name.
|
155
155
|
* @returns {string} The ISO 3166 code.
|
156
156
|
*/
|
157
|
-
|
158
|
-
region = region
|
157
|
+
var _mapRegionToCode = function (region) {
|
158
|
+
region = region === null || region === void 0 ? void 0 : region.toLowerCase();
|
159
159
|
return RegionToCode[region] || '';
|
160
160
|
};
|
161
161
|
/**
|
@@ -169,11 +169,11 @@ function _getLanguageObject(codes) {
|
|
169
169
|
* @param {string} code - The language code.
|
170
170
|
* @returns {LanguageObject|null} The language object.
|
171
171
|
*/
|
172
|
-
|
172
|
+
var _handleGetLanguageObject = function (code) {
|
173
173
|
if (!_isValidLanguageCode(code))
|
174
174
|
return null;
|
175
|
-
|
176
|
-
|
175
|
+
var codeParts = code.split('-');
|
176
|
+
var languageObject = {
|
177
177
|
language: _mapCodeToLanguage(codeParts[0]),
|
178
178
|
};
|
179
179
|
if (codeParts[1]) {
|
@@ -196,7 +196,7 @@ const _handleGetLanguageObject = (code) => {
|
|
196
196
|
* @returns {string|string[]} The language name(s).
|
197
197
|
* @internal
|
198
198
|
*/
|
199
|
-
|
199
|
+
var _getLanguageName = function (codes) {
|
200
200
|
return Array.isArray(codes) ? codes.map(_handleGetLanguageName) : _handleGetLanguageName(codes);
|
201
201
|
};
|
202
202
|
exports._getLanguageName = _getLanguageName;
|
@@ -205,20 +205,20 @@ exports._getLanguageName = _getLanguageName;
|
|
205
205
|
* @param {string} code - The language code.
|
206
206
|
* @returns {string} The language name.
|
207
207
|
*/
|
208
|
-
|
208
|
+
var _handleGetLanguageName = function (code) {
|
209
209
|
if (!_isValidLanguageCode(code))
|
210
210
|
return '';
|
211
211
|
if (Predefined[code])
|
212
212
|
return Predefined[code];
|
213
|
-
|
213
|
+
var languageObject = _handleGetLanguageObject(code);
|
214
214
|
if (!languageObject)
|
215
215
|
return '';
|
216
|
-
|
216
|
+
var result = languageObject.language;
|
217
217
|
if (languageObject.script) {
|
218
|
-
result +=
|
218
|
+
result += ", ".concat(languageObject.script);
|
219
219
|
}
|
220
220
|
if (languageObject.region) {
|
221
|
-
result +=
|
221
|
+
result += ", ".concat(languageObject.region);
|
222
222
|
}
|
223
223
|
return result;
|
224
224
|
};
|
@@ -229,7 +229,7 @@ const _handleGetLanguageName = (code) => {
|
|
229
229
|
* @returns {string|string[]} The language code(s).
|
230
230
|
* @internal
|
231
231
|
*/
|
232
|
-
|
232
|
+
var _getLanguageCode = function (languages) {
|
233
233
|
return Array.isArray(languages) ? languages.map(_handleGetLanguageCode) : _handleGetLanguageCode(languages);
|
234
234
|
};
|
235
235
|
exports._getLanguageCode = _getLanguageCode;
|
@@ -238,7 +238,7 @@ exports._getLanguageCode = _getLanguageCode;
|
|
238
238
|
* @param {string|LanguageObject} language - The language name or object.
|
239
239
|
* @returns {string} The language code.
|
240
240
|
*/
|
241
|
-
|
241
|
+
var _handleGetLanguageCode = function (language) {
|
242
242
|
if (typeof language === 'string')
|
243
243
|
return _handleGetLanguageCodeFromString(language);
|
244
244
|
return _handleGetLanguageCodeFromObject(language);
|
@@ -248,24 +248,24 @@ const _handleGetLanguageCode = (language) => {
|
|
248
248
|
* @param {string} language - The language name.
|
249
249
|
* @returns {string} The language code.
|
250
250
|
*/
|
251
|
-
|
252
|
-
|
253
|
-
|
251
|
+
var _handleGetLanguageCodeFromString = function (language) {
|
252
|
+
var subtagStrings = language.split(',').map(function (string) { return string.trim(); });
|
253
|
+
var code = _mapLanguageToCode(subtagStrings[0]);
|
254
254
|
if (code) {
|
255
255
|
if (subtagStrings.length === 3) {
|
256
|
-
code +=
|
257
|
-
code +=
|
256
|
+
code += "-".concat(_mapScriptToCode(subtagStrings[1]));
|
257
|
+
code += "-".concat(_mapRegionToCode(subtagStrings[2]));
|
258
258
|
}
|
259
259
|
else if (subtagStrings.length === 2) {
|
260
|
-
|
260
|
+
var tag = _mapScriptToCode(subtagStrings[1]);
|
261
261
|
if (!tag)
|
262
262
|
tag = _mapRegionToCode(subtagStrings[1]);
|
263
263
|
if (tag)
|
264
|
-
code +=
|
264
|
+
code += "-".concat(tag);
|
265
265
|
}
|
266
266
|
}
|
267
267
|
if (!code) {
|
268
|
-
for (
|
268
|
+
for (var key in Predefined) {
|
269
269
|
if (Predefined[key] === language) {
|
270
270
|
return key;
|
271
271
|
}
|
@@ -278,34 +278,38 @@ const _handleGetLanguageCodeFromString = (language) => {
|
|
278
278
|
* @param {LanguageObject} languageObject - The language object.
|
279
279
|
* @returns {string} The language code.
|
280
280
|
*/
|
281
|
-
|
282
|
-
if (!languageObject
|
281
|
+
var _handleGetLanguageCodeFromObject = function (languageObject) {
|
282
|
+
if (!(languageObject === null || languageObject === void 0 ? void 0 : languageObject.language))
|
283
283
|
return '';
|
284
|
-
|
284
|
+
var code = languageObject.language.toLowerCase();
|
285
285
|
if (languageObject.script) {
|
286
|
-
code +=
|
286
|
+
code += "-".concat(_capitalize(languageObject.script));
|
287
287
|
}
|
288
288
|
if (languageObject.region) {
|
289
|
-
code +=
|
289
|
+
code += "-".concat(languageObject.region.toUpperCase());
|
290
290
|
}
|
291
291
|
return _isValidLanguageCode(code) ? code : '';
|
292
292
|
};
|
293
293
|
/**
|
294
294
|
* @internal
|
295
295
|
*/
|
296
|
-
function _isSameLanguage(
|
296
|
+
function _isSameLanguage() {
|
297
|
+
var codes = [];
|
298
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
299
|
+
codes[_i] = arguments[_i];
|
300
|
+
}
|
297
301
|
// Flatten the array in case the codes are provided as an array
|
298
302
|
if (codes.length === 1 && Array.isArray(codes[0])) {
|
299
303
|
codes = codes[0];
|
300
304
|
}
|
301
305
|
if (codes.length < 2)
|
302
306
|
return false;
|
303
|
-
|
304
|
-
for (
|
307
|
+
var language = null;
|
308
|
+
for (var i = 0; i < codes.length; i++) {
|
305
309
|
if (typeof codes[i] !== 'string')
|
306
310
|
return false;
|
307
|
-
|
308
|
-
|
311
|
+
var languageCode = codes[i].split('-')[0];
|
312
|
+
var currentLanguage = _mapCodeToLanguage(languageCode);
|
309
313
|
if (language === null) {
|
310
314
|
language = currentLanguage;
|
311
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
|
-
|
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
|
-
|
53
|
+
var codes_js_1 = require("./codes.js");
|
54
54
|
/**
|
55
55
|
* Get the text direction for a given language code.
|
56
56
|
*
|
@@ -59,9 +59,9 @@ const codes_js_1 = require("./codes.js");
|
|
59
59
|
* @internal
|
60
60
|
*/
|
61
61
|
function _getLanguageDirection(code) {
|
62
|
-
return rtlLanguages.some(language
|
63
|
-
|
64
|
-
|
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)
|
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
|
+
}
|