generaltranslation 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.cjs.min.cjs +2 -0
- package/dist/index.cjs.min.cjs.map +1 -0
- package/dist/index.esm.min.mjs +2 -0
- package/dist/index.esm.min.mjs.map +1 -0
- package/dist/internal.cjs.min.cjs +2 -0
- package/dist/internal.cjs.min.cjs.map +1 -0
- package/dist/internal.d.ts +3 -1
- package/dist/internal.esm.min.mjs +2 -0
- package/dist/internal.esm.min.mjs.map +1 -0
- package/package.json +2 -4
- package/dist/codes/_determineLanguage.js +0 -27
- package/dist/codes/_isSameLanguage.js +0 -19
- package/dist/codes/codes.js +0 -98
- package/dist/formatting/format.js +0 -89
- package/dist/formatting/string_content.js +0 -106
- package/dist/index.js +0 -308
- package/dist/internal.js +0 -11
- package/dist/settings/libraryDefaultLanguage.js +0 -2
- package/dist/translation/dictionaries/_translateBundle.js +0 -75
- package/dist/translation/dictionaries/_updateProjectDictionary.js +0 -71
- package/dist/translation/react/_translateReact.js +0 -76
- package/dist/translation/strings/_translate.js +0 -78
- package/dist/types/types.js +0 -1
@@ -1,89 +0,0 @@
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
2
|
-
__assign = Object.assign || function(t) {
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4
|
-
s = arguments[i];
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6
|
-
t[p] = s[p];
|
7
|
-
}
|
8
|
-
return t;
|
9
|
-
};
|
10
|
-
return __assign.apply(this, arguments);
|
11
|
-
};
|
12
|
-
import libraryDefaultLanguage from '../settings/libraryDefaultLanguage';
|
13
|
-
/**
|
14
|
-
* Formats a number according to the specified languages and options.
|
15
|
-
*
|
16
|
-
* @param {Object} params - The parameters for the number formatting.
|
17
|
-
* @param {number} params.value - The number to format.
|
18
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
19
|
-
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
|
20
|
-
*
|
21
|
-
* @returns {string} The formatted number.
|
22
|
-
* @internal
|
23
|
-
*/
|
24
|
-
export function _formatNum(_a) {
|
25
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
26
|
-
return new Intl.NumberFormat(languages, __assign({ numberingSystem: 'latn' }, options)).format(value);
|
27
|
-
}
|
28
|
-
/**
|
29
|
-
* Formats a date according to the specified languages and options.
|
30
|
-
*
|
31
|
-
* @param {Object} params - The parameters for the date formatting.
|
32
|
-
* @param {Date} params.value - The date to format.
|
33
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
34
|
-
* @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
|
35
|
-
*
|
36
|
-
* @returns {string} The formatted date.
|
37
|
-
* @internal
|
38
|
-
*/
|
39
|
-
export function _formatDateTime(_a) {
|
40
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
41
|
-
return new Intl.DateTimeFormat(languages, __assign({ calendar: "gregory", numberingSystem: "latn" }, options)).format(value);
|
42
|
-
}
|
43
|
-
/**
|
44
|
-
* Formats a currency value according to the specified languages, currency, and options.
|
45
|
-
*
|
46
|
-
* @param {Object} params - The parameters for the currency formatting.
|
47
|
-
* @param {number} params.value - The currency value to format.
|
48
|
-
* @param {string} params.currency - The currency code (e.g., 'USD').
|
49
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
50
|
-
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
|
51
|
-
*
|
52
|
-
* @returns {string} The formatted currency value.
|
53
|
-
* @internal
|
54
|
-
*/
|
55
|
-
export function _formatCurrency(_a) {
|
56
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage] : _b, _c = _a.currency, currency = _c === void 0 ? 'USD' : _c, _d = _a.options, options = _d === void 0 ? {} : _d;
|
57
|
-
return new Intl.NumberFormat(languages, __assign({ style: 'currency', currency: currency, numberingSystem: 'latn' }, options)).format(value);
|
58
|
-
}
|
59
|
-
/**
|
60
|
-
* Formats a list of items according to the specified languages and options.
|
61
|
-
*
|
62
|
-
* @param {Object} params - The parameters for the list formatting.
|
63
|
-
* @param {Array<string | number>} params.value - The list of items to format.
|
64
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
65
|
-
* @param {Intl.ListFormatOptions} [params.options={}] - Additional options for list formatting.
|
66
|
-
*
|
67
|
-
* @returns {string} The formatted list.
|
68
|
-
* @internal
|
69
|
-
*/
|
70
|
-
export function _formatList(_a) {
|
71
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
72
|
-
return new Intl.ListFormat(languages, __assign({ type: 'conjunction', style: 'long' }, options)).format(value);
|
73
|
-
}
|
74
|
-
/**
|
75
|
-
* Formats a relative time value according to the specified languages and options.
|
76
|
-
*
|
77
|
-
* @param {Object} params - The parameters for the relative time formatting.
|
78
|
-
* @param {number} params.value - The relative time value to format.
|
79
|
-
* @param {Intl.RelativeTimeFormatUnit} params.unit - The unit of time (e.g., 'second', 'minute', 'hour', 'day', 'week', 'month', 'year').
|
80
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
81
|
-
* @param {Intl.RelativeTimeFormatOptions} [params.options={}] - Additional options for relative time formatting.
|
82
|
-
*
|
83
|
-
* @returns {string} The formatted relative time string.
|
84
|
-
* @internal
|
85
|
-
*/
|
86
|
-
export function _formatRelativeTime(_a) {
|
87
|
-
var value = _a.value, unit = _a.unit, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
88
|
-
return new Intl.RelativeTimeFormat(languages, __assign({ style: "long", numeric: 'auto' }, options)).format(value, unit);
|
89
|
-
}
|
@@ -1,106 +0,0 @@
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
2
|
-
__assign = Object.assign || function(t) {
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4
|
-
s = arguments[i];
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6
|
-
t[p] = s[p];
|
7
|
-
}
|
8
|
-
return t;
|
9
|
-
};
|
10
|
-
return __assign.apply(this, arguments);
|
11
|
-
};
|
12
|
-
import libraryDefaultLanguage from '../settings/libraryDefaultLanguage';
|
13
|
-
import { _formatCurrency, _formatDateTime, _formatNum, _formatList } from './format';
|
14
|
-
// Variable types mapping
|
15
|
-
var variableTypeMap = {
|
16
|
-
var: "variable",
|
17
|
-
num: "number",
|
18
|
-
datetime: "datetime",
|
19
|
-
currency: "currency"
|
20
|
-
};
|
21
|
-
/**
|
22
|
-
* @internal
|
23
|
-
*/
|
24
|
-
/**
|
25
|
-
* @internal
|
26
|
-
*/
|
27
|
-
export function _splitStringToContent(string) {
|
28
|
-
if (typeof string !== 'string')
|
29
|
-
throw new Error("splitStringToContent: ".concat(string, " is not a string!"));
|
30
|
-
var result = [];
|
31
|
-
var regex = /{([^}]+)}/g;
|
32
|
-
var lastIndex = 0;
|
33
|
-
var match;
|
34
|
-
while ((match = regex.exec(string)) !== null) {
|
35
|
-
var fullMatch = match[0], content = match[1];
|
36
|
-
var startIndex = match.index;
|
37
|
-
// Check for escaped braces with '^' right before the opening brace
|
38
|
-
if (string[startIndex - 1] === "^") {
|
39
|
-
// Add text before the escape sequence
|
40
|
-
if (startIndex - 1 > lastIndex) {
|
41
|
-
result.push(string.slice(lastIndex, startIndex - 1));
|
42
|
-
}
|
43
|
-
// Add the escaped content as literal text
|
44
|
-
result.push(fullMatch);
|
45
|
-
lastIndex = startIndex + fullMatch.length;
|
46
|
-
continue;
|
47
|
-
}
|
48
|
-
// Add text before the match
|
49
|
-
if (startIndex > lastIndex) {
|
50
|
-
result.push(string.slice(lastIndex, startIndex));
|
51
|
-
}
|
52
|
-
// Handle the variable substitution inside the braces
|
53
|
-
var parts = content.split(",").map(function (part) { return part.trim(); });
|
54
|
-
var key = parts[0];
|
55
|
-
var variableType = parts[1] ? variableTypeMap[parts[1]] : undefined;
|
56
|
-
var variableObject = __assign({ key: key }, (variableType && { variable: variableType }));
|
57
|
-
result.push(variableObject);
|
58
|
-
lastIndex = startIndex + fullMatch.length;
|
59
|
-
}
|
60
|
-
// Add the remaining part of the string after the last match
|
61
|
-
if (lastIndex < string.length) {
|
62
|
-
result.push(string.slice(lastIndex));
|
63
|
-
}
|
64
|
-
return result;
|
65
|
-
}
|
66
|
-
/**
|
67
|
-
* @internal
|
68
|
-
*/
|
69
|
-
export function _renderContentToString(content, languages, variables, variableOptions) {
|
70
|
-
if (languages === void 0) { languages = libraryDefaultLanguage; }
|
71
|
-
if (variables === void 0) { variables = {}; }
|
72
|
-
if (variableOptions === void 0) { variableOptions = {}; }
|
73
|
-
if (typeof content === 'string')
|
74
|
-
content = _splitStringToContent(content);
|
75
|
-
if (typeof content === 'string')
|
76
|
-
return content;
|
77
|
-
if (!Array.isArray(content))
|
78
|
-
throw new Error("renderContentToString: content ".concat(content, " is invalid"));
|
79
|
-
return content.map(function (item) {
|
80
|
-
var _a;
|
81
|
-
if (typeof item === 'string')
|
82
|
-
return item;
|
83
|
-
if (typeof item === 'object') {
|
84
|
-
var value = variables[item.key];
|
85
|
-
if (!item.variable)
|
86
|
-
return value;
|
87
|
-
else if (item.variable === "number") {
|
88
|
-
return _formatNum({
|
89
|
-
value: value,
|
90
|
-
languages: languages,
|
91
|
-
options: variableOptions[item.key]
|
92
|
-
});
|
93
|
-
}
|
94
|
-
else if (item.variable === "currency") {
|
95
|
-
return _formatCurrency(__assign(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })), (((_a = variableOptions[item.key]) === null || _a === void 0 ? void 0 : _a.currency) && { currency: variableOptions[item.key].currency })));
|
96
|
-
}
|
97
|
-
else if (item.variable === "datetime") {
|
98
|
-
return _formatDateTime(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })));
|
99
|
-
}
|
100
|
-
else if (item.variable === "list") {
|
101
|
-
return _formatList(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })));
|
102
|
-
}
|
103
|
-
return value;
|
104
|
-
}
|
105
|
-
}).join('');
|
106
|
-
}
|
package/dist/index.js
DELETED
@@ -1,308 +0,0 @@
|
|
1
|
-
// `generaltranslation` language toolkit
|
2
|
-
// © 2024, General Translation, Inc.
|
3
|
-
var __assign = (this && this.__assign) || function () {
|
4
|
-
__assign = Object.assign || function(t) {
|
5
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
6
|
-
s = arguments[i];
|
7
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
8
|
-
t[p] = s[p];
|
9
|
-
}
|
10
|
-
return t;
|
11
|
-
};
|
12
|
-
return __assign.apply(this, arguments);
|
13
|
-
};
|
14
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
15
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
16
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
17
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
18
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
19
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
20
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
21
|
-
});
|
22
|
-
};
|
23
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
24
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
25
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
26
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
27
|
-
function step(op) {
|
28
|
-
if (f) throw new TypeError("Generator is already executing.");
|
29
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
30
|
-
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;
|
31
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
32
|
-
switch (op[0]) {
|
33
|
-
case 0: case 1: t = op; break;
|
34
|
-
case 4: _.label++; return { value: op[1], done: false };
|
35
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
36
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
37
|
-
default:
|
38
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
39
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
40
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
41
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
42
|
-
if (t[2]) _.ops.pop();
|
43
|
-
_.trys.pop(); continue;
|
44
|
-
}
|
45
|
-
op = body.call(thisArg, _);
|
46
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
47
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
48
|
-
}
|
49
|
-
};
|
50
|
-
// ----- IMPORTS ----- //
|
51
|
-
import _translateBundle from './translation/dictionaries/_translateBundle';
|
52
|
-
import _translate from './translation/strings/_translate';
|
53
|
-
import _translateReact from './translation/react/_translateReact';
|
54
|
-
import _updateProjectDictionary from './translation/dictionaries/_updateProjectDictionary';
|
55
|
-
import _determineLanguage from './codes/_determineLanguage';
|
56
|
-
import { _formatNum, _formatCurrency, _formatList, _formatRelativeTime, _formatDateTime } from './formatting/format';
|
57
|
-
import { _splitStringToContent, _renderContentToString } from './formatting/string_content';
|
58
|
-
import { _getLanguageName, _isValidLanguageCode, _getLanguageDirection, _standardizeLanguageCode } from './codes/codes';
|
59
|
-
import _isSameLanguage from './codes/_isSameLanguage';
|
60
|
-
import libraryDefaultLanguage from './settings/libraryDefaultLanguage';
|
61
|
-
// ----- CORE CLASS ----- //
|
62
|
-
var getDefaultFromEnv = function (VARIABLE) {
|
63
|
-
if (typeof process !== 'undefined' && process.env) {
|
64
|
-
return process.env[VARIABLE] || '';
|
65
|
-
}
|
66
|
-
return '';
|
67
|
-
};
|
68
|
-
/**
|
69
|
-
* GT is the core driver for the General Translation library.
|
70
|
-
*/
|
71
|
-
var GT = /** @class */ (function () {
|
72
|
-
/**
|
73
|
-
* Constructs an instance of the GT class.
|
74
|
-
*
|
75
|
-
* @param {GTConstructorParams} [params] - The parameters for initializing the GT instance.
|
76
|
-
* @param {string} [params.apiKey=''] - The API key for accessing the translation service.
|
77
|
-
* @param {string} [params.defaultLanguage='en'] - The default language for translations.
|
78
|
-
* @param {string} [params.projectID=''] - The project ID for the translation service.
|
79
|
-
* @param {string} [params.baseURL='https://prod.gtx.dev'] - The base URL for the translation service.
|
80
|
-
*/
|
81
|
-
function GT(_a) {
|
82
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.apiKey, apiKey = _c === void 0 ? '' : _c, _d = _b.defaultLanguage, defaultLanguage = _d === void 0 ? libraryDefaultLanguage : _d, _e = _b.projectID, projectID = _e === void 0 ? '' : _e, _f = _b.baseURL, baseURL = _f === void 0 ? 'https://prod.gtx.dev' : _f;
|
83
|
-
this.apiKey = apiKey || getDefaultFromEnv('GT_API_KEY');
|
84
|
-
this.projectID = projectID || getDefaultFromEnv('GT_PROJECT_ID');
|
85
|
-
this.defaultLanguage = defaultLanguage.toLowerCase();
|
86
|
-
this.baseURL = baseURL;
|
87
|
-
}
|
88
|
-
/**
|
89
|
-
* Translates a string or an array of strings/variables into a target language.
|
90
|
-
* If `metadata.save` is provided, the translation is cached for use in a public project.
|
91
|
-
*
|
92
|
-
* @param {Content} content - The string or array of strings/variables to be translated.
|
93
|
-
* @param {string} language - The target language code (e.g., 'en', 'fr') for the translation.
|
94
|
-
* @param {{ context?: string, save?: boolean, [key: string]: any }} [metadata] - Additional metadata for the translation request.
|
95
|
-
* @param {string} [metadata.context] - Contextual information to assist with the translation.
|
96
|
-
* @param {boolean} [metadata.save] - Whether to cache the translation for use in a public project.
|
97
|
-
*
|
98
|
-
* @returns {Promise<ContentTranslationResult>} A promise that resolves to the translated content, or an error if the translation fails.
|
99
|
-
*/
|
100
|
-
GT.prototype.translate = function (content, language, metadata) {
|
101
|
-
return __awaiter(this, void 0, void 0, function () {
|
102
|
-
return __generator(this, function (_a) {
|
103
|
-
switch (_a.label) {
|
104
|
-
case 0: return [4 /*yield*/, _translate(this, content, language, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
|
105
|
-
case 1: return [2 /*return*/, _a.sent()];
|
106
|
-
}
|
107
|
-
});
|
108
|
-
});
|
109
|
-
};
|
110
|
-
/**
|
111
|
-
* Translates the content of React children elements.
|
112
|
-
*
|
113
|
-
* @param {Object} params - The parameters for the translation.
|
114
|
-
* @param {ReactChildrenAsObject} params.children - The React children content to be translated.
|
115
|
-
* @param {string} params.language - The target language for the translation.
|
116
|
-
* @param {Object} params.metadata - Additional metadata for the translation process.
|
117
|
-
*
|
118
|
-
* @returns {Promise<ReactTranslationResult>} - A promise that resolves to the translated content.
|
119
|
-
*/
|
120
|
-
GT.prototype.translateReact = function (children, language, 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*/, _translateReact(this, children, language, __assign({ projectID: this.projectID, defaultLanguage: this.defaultLanguage }, metadata))];
|
125
|
-
case 1: return [2 /*return*/, _a.sent()];
|
126
|
-
}
|
127
|
-
});
|
128
|
-
});
|
129
|
-
};
|
130
|
-
/**
|
131
|
-
* Bundles multiple translation requests and sends them to the server.
|
132
|
-
* @param requests - Array of requests to be processed and sent.
|
133
|
-
* @returns A promise that resolves to an array of processed results.
|
134
|
-
*/
|
135
|
-
GT.prototype.translateBundle = function (requests) {
|
136
|
-
return __awaiter(this, void 0, void 0, function () {
|
137
|
-
return __generator(this, function (_a) {
|
138
|
-
return [2 /*return*/, _translateBundle(this, requests)];
|
139
|
-
});
|
140
|
-
});
|
141
|
-
};
|
142
|
-
/**
|
143
|
-
* Pushes updates to a remotely cached translation dictionary.
|
144
|
-
* @param {Update[]} updates - Array of updates.
|
145
|
-
* @param {string[]} [languages] - Array of languages to be updated.
|
146
|
-
* @param {string} [projectID=this.projectID] - The ID of the project. Defaults to the instance's projectID.
|
147
|
-
* @param {boolean} [replace=false] - Whether to replace the existing dictionary. Defaults to false.
|
148
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of strings indicating the languages which have been updated.
|
149
|
-
*/
|
150
|
-
GT.prototype.updateProjectDictionary = function (updates_1) {
|
151
|
-
return __awaiter(this, arguments, void 0, function (updates, languages, replace) {
|
152
|
-
if (languages === void 0) { languages = []; }
|
153
|
-
if (replace === void 0) { replace = false; }
|
154
|
-
return __generator(this, function (_a) {
|
155
|
-
return [2 /*return*/, _updateProjectDictionary(this, updates, languages, replace)];
|
156
|
-
});
|
157
|
-
});
|
158
|
-
};
|
159
|
-
return GT;
|
160
|
-
}());
|
161
|
-
// ----- EXPORTS ----- //
|
162
|
-
/**
|
163
|
-
* Get the text direction for a given language code using the Intl.Locale API.
|
164
|
-
*
|
165
|
-
* @param {string} code - The language code to check.
|
166
|
-
* @returns {string} - 'rtl' if the language is right-to-left, otherwise 'ltr'.
|
167
|
-
*/
|
168
|
-
export function getLanguageDirection(code) {
|
169
|
-
return _getLanguageDirection(code);
|
170
|
-
}
|
171
|
-
;
|
172
|
-
/**
|
173
|
-
* Retrieves the display name(s) of language code(s) using Intl.DisplayNames.
|
174
|
-
*
|
175
|
-
* @param {string | string[]} code - A language code or an array of codes.
|
176
|
-
* @param {string} [language = 'en'] - The language for display names.
|
177
|
-
* @returns {string | string[]} The display name(s) corresponding to the code(s), or empty string(s) if invalid.
|
178
|
-
*/
|
179
|
-
export function getLanguageName(code, language) {
|
180
|
-
return _getLanguageName(code, language);
|
181
|
-
}
|
182
|
-
;
|
183
|
-
/**
|
184
|
-
* Checks if a given BCP 47 language code is valid.
|
185
|
-
* @param {string} code - The BCP 47 language code to validate.
|
186
|
-
* @returns {boolean} True if the BCP 47 code is valid, false otherwise.
|
187
|
-
*/
|
188
|
-
export function isValidLanguageCode(code) {
|
189
|
-
return _isValidLanguageCode(code);
|
190
|
-
}
|
191
|
-
;
|
192
|
-
/**
|
193
|
-
* Standardizes a BCP 47 language code to ensure correct formatting.
|
194
|
-
* @param {string} code - The BCP 47 language code to standardize.
|
195
|
-
* @returns {string} The standardized BCP 47 language code.
|
196
|
-
*/
|
197
|
-
export function standardizeLanguageCode(code) {
|
198
|
-
return _standardizeLanguageCode(code);
|
199
|
-
}
|
200
|
-
;
|
201
|
-
/**
|
202
|
-
* Checks if multiple BCP 47 language codes represent the same language.
|
203
|
-
* @param {string[]} codes - The BCP 47 language codes to compare.
|
204
|
-
* @returns {boolean} True if all BCP 47 codes represent the same language, false otherwise.
|
205
|
-
*/
|
206
|
-
export function isSameLanguage() {
|
207
|
-
var codes = [];
|
208
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
209
|
-
codes[_i] = arguments[_i];
|
210
|
-
}
|
211
|
-
return _isSameLanguage.apply(void 0, codes);
|
212
|
-
}
|
213
|
-
;
|
214
|
-
/**
|
215
|
-
* Formats a number according to the specified languages and options.
|
216
|
-
* @param {Object} params - The parameters for the number formatting.
|
217
|
-
* @param {number} params.value - The number to format.
|
218
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
219
|
-
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for number formatting.
|
220
|
-
* @returns {string} The formatted number.
|
221
|
-
*/
|
222
|
-
export function formatNum(params) {
|
223
|
-
return _formatNum(params);
|
224
|
-
}
|
225
|
-
;
|
226
|
-
/**
|
227
|
-
* Formats a date according to the specified languages and options.
|
228
|
-
* @param {Object} params - The parameters for the date formatting.
|
229
|
-
* @param {Date} params.value - The date to format.
|
230
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
231
|
-
* @param {Intl.DateTimeFormatOptions} [params.options={}] - Additional options for date formatting.
|
232
|
-
* @returns {string} The formatted date.
|
233
|
-
*/
|
234
|
-
export function formatDateTime(params) {
|
235
|
-
return _formatDateTime(params);
|
236
|
-
}
|
237
|
-
;
|
238
|
-
/**
|
239
|
-
* Formats a currency value according to the specified languages, currency, and options.
|
240
|
-
* @param {Object} params - The parameters for the currency formatting.
|
241
|
-
* @param {number} params.value - The currency value to format.
|
242
|
-
* @param {string} params.currency - The currency code (e.g., 'USD').
|
243
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
244
|
-
* @param {Intl.NumberFormatOptions} [params.options={}] - Additional options for currency formatting.
|
245
|
-
* @returns {string} The formatted currency value.
|
246
|
-
*/
|
247
|
-
export function formatCurrency(params) {
|
248
|
-
return _formatCurrency(params);
|
249
|
-
}
|
250
|
-
;
|
251
|
-
/**
|
252
|
-
* Formats a list of items according to the specified languages and options.
|
253
|
-
* @param {Object} params - The parameters for the list formatting.
|
254
|
-
* @param {Array<string | number>} params.value - The list of items to format.
|
255
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
256
|
-
* @param {Intl.ListFormatOptions} [params.options={}] - Additional options for list formatting.
|
257
|
-
* @returns {string} The formatted list.
|
258
|
-
*/
|
259
|
-
export function formatList(params) {
|
260
|
-
return _formatList(params);
|
261
|
-
}
|
262
|
-
;
|
263
|
-
/**
|
264
|
-
* Formats a relative time value according to the specified languages and options.
|
265
|
-
* @param {Object} params - The parameters for the relative time formatting.
|
266
|
-
* @param {number} params.value - The relative time value to format.
|
267
|
-
* @param {Intl.RelativeTimeFormatUnit} params.unit - The unit of time (e.g., 'second', 'minute', 'hour', 'day', 'week', 'month', 'year').
|
268
|
-
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
269
|
-
* @param {Intl.RelativeTimeFormatOptions} [params.options={}] - Additional options for relative time formatting.
|
270
|
-
* @returns {string} The formatted relative time string.
|
271
|
-
*/
|
272
|
-
export function formatRelativeTime(params) {
|
273
|
-
return _formatRelativeTime(params);
|
274
|
-
}
|
275
|
-
;
|
276
|
-
/**
|
277
|
-
* Splits a string into an array of text and variable objects.
|
278
|
-
* @param {string} string - The input string to split.
|
279
|
-
* @returns {Content} - An array containing strings and VariableObjects.
|
280
|
-
*/
|
281
|
-
export function splitStringToContent(string) {
|
282
|
-
return _splitStringToContent(string);
|
283
|
-
}
|
284
|
-
;
|
285
|
-
/**
|
286
|
-
* Renders content to a string by replacing variables with their formatted values.
|
287
|
-
* @param {Content} content - The content to render.
|
288
|
-
* @param {string | string[]} [languages='en'] - The language(s) to use for formatting.
|
289
|
-
* @param {Record<string, any>} [variables={}] - An object containing variable values.
|
290
|
-
* @param {Record<string, any>} [variableOptions={}] - An object containing options for formatting variables.
|
291
|
-
* @returns {string} - The rendered string with variables replaced by their formatted values.
|
292
|
-
*/
|
293
|
-
export function renderContentToString(content, languages, variables, variableOptions) {
|
294
|
-
return _renderContentToString(content, languages, variables, variableOptions);
|
295
|
-
}
|
296
|
-
;
|
297
|
-
/**
|
298
|
-
* Determines the best matching language from the approved languages list based on a provided list of preferred languages.
|
299
|
-
* @param {string | string[]} languages - A single language or an array of languages sorted in preference order.
|
300
|
-
* @param {string[]} approvedLanguages - An array of approved languages, also sorted by preference.
|
301
|
-
* @returns {string | undefined} - The best matching language from the approvedLanguages list, or undefined if no match is found.
|
302
|
-
*/
|
303
|
-
export function determineLanguage(languages, approvedLanguages) {
|
304
|
-
return _determineLanguage(languages, approvedLanguages);
|
305
|
-
}
|
306
|
-
;
|
307
|
-
// DEFAULT EXPORT
|
308
|
-
export default GT;
|
package/dist/internal.js
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
// Functions provided to other GT libraries
|
2
|
-
import XXH from 'xxhashjs';
|
3
|
-
/**
|
4
|
-
* Calculates a unique hash for a given string using xxhash.
|
5
|
-
*
|
6
|
-
* @param {string} string - The string to be hashed.
|
7
|
-
* @returns {string} - The resulting hash as a hexadecimal string.
|
8
|
-
*/
|
9
|
-
export function hashString(string) {
|
10
|
-
return XXH.h64().update(string).digest().toString(16);
|
11
|
-
}
|
@@ -1,75 +0,0 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
12
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
14
|
-
function step(op) {
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
17
|
-
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;
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
19
|
-
switch (op[0]) {
|
20
|
-
case 0: case 1: t = op; break;
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24
|
-
default:
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29
|
-
if (t[2]) _.ops.pop();
|
30
|
-
_.trys.pop(); continue;
|
31
|
-
}
|
32
|
-
op = body.call(thisArg, _);
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35
|
-
}
|
36
|
-
};
|
37
|
-
/**
|
38
|
-
* @internal
|
39
|
-
*/
|
40
|
-
export default function _translateBundle(gt, requests) {
|
41
|
-
return __awaiter(this, void 0, void 0, function () {
|
42
|
-
var controller, signal, response, _a, _b, _c, resultArray;
|
43
|
-
var _d, _e, _f;
|
44
|
-
return __generator(this, function (_g) {
|
45
|
-
switch (_g.label) {
|
46
|
-
case 0:
|
47
|
-
controller = new AbortController();
|
48
|
-
signal = controller.signal;
|
49
|
-
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) {
|
50
|
-
setTimeout(function () { return controller.abort(); }, requests[0].data.metadata.timeout);
|
51
|
-
}
|
52
|
-
return [4 /*yield*/, fetch("".concat(gt.baseURL, "/bundle"), {
|
53
|
-
method: 'POST',
|
54
|
-
headers: {
|
55
|
-
'Content-Type': 'application/json',
|
56
|
-
'gtx-api-key': gt.apiKey,
|
57
|
-
},
|
58
|
-
body: JSON.stringify(requests),
|
59
|
-
signal: signal
|
60
|
-
})];
|
61
|
-
case 1:
|
62
|
-
response = _g.sent();
|
63
|
-
if (!!response.ok) return [3 /*break*/, 3];
|
64
|
-
_a = Error.bind;
|
65
|
-
_c = (_b = "".concat(response.status, ": ")).concat;
|
66
|
-
return [4 /*yield*/, response.text()];
|
67
|
-
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_g.sent()])]))();
|
68
|
-
case 3: return [4 /*yield*/, response.json()];
|
69
|
-
case 4:
|
70
|
-
resultArray = _g.sent();
|
71
|
-
return [2 /*return*/, resultArray];
|
72
|
-
}
|
73
|
-
});
|
74
|
-
});
|
75
|
-
}
|
@@ -1,71 +0,0 @@
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
-
});
|
9
|
-
};
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
12
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
14
|
-
function step(op) {
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
16
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
17
|
-
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;
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
19
|
-
switch (op[0]) {
|
20
|
-
case 0: case 1: t = op; break;
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24
|
-
default:
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29
|
-
if (t[2]) _.ops.pop();
|
30
|
-
_.trys.pop(); continue;
|
31
|
-
}
|
32
|
-
op = body.call(thisArg, _);
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35
|
-
}
|
36
|
-
};
|
37
|
-
/**
|
38
|
-
* @internal
|
39
|
-
*/
|
40
|
-
export default function _updateProjectDictionary(gt, updates, languages, replace) {
|
41
|
-
return __awaiter(this, void 0, void 0, function () {
|
42
|
-
var response, _a, _b, _c, result;
|
43
|
-
return __generator(this, function (_d) {
|
44
|
-
switch (_d.label) {
|
45
|
-
case 0: return [4 /*yield*/, fetch("".concat(gt.baseURL, "/update"), {
|
46
|
-
method: 'POST',
|
47
|
-
headers: {
|
48
|
-
'Content-Type': 'application/json',
|
49
|
-
'gtx-api-key': gt.apiKey,
|
50
|
-
},
|
51
|
-
body: JSON.stringify({
|
52
|
-
updates: updates,
|
53
|
-
languages: languages,
|
54
|
-
replace: replace
|
55
|
-
})
|
56
|
-
})];
|
57
|
-
case 1:
|
58
|
-
response = _d.sent();
|
59
|
-
if (!!response.ok) return [3 /*break*/, 3];
|
60
|
-
_a = Error.bind;
|
61
|
-
_c = (_b = "".concat(response.status, ": ")).concat;
|
62
|
-
return [4 /*yield*/, response.text()];
|
63
|
-
case 2: throw new (_a.apply(Error, [void 0, _c.apply(_b, [_d.sent()])]))();
|
64
|
-
case 3: return [4 /*yield*/, response.json()];
|
65
|
-
case 4:
|
66
|
-
result = _d.sent();
|
67
|
-
return [2 /*return*/, result === null || result === void 0 ? void 0 : result.languages];
|
68
|
-
}
|
69
|
-
});
|
70
|
-
});
|
71
|
-
}
|