generaltranslation 2.0.67 → 2.0.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -369
- package/dist/codes/{determineLanguage.js → _determineLanguage.js} +5 -2
- package/dist/codes/_isSameLanguage.js +22 -0
- package/dist/codes/codes.d.ts +1 -8
- package/dist/codes/codes.js +61 -287
- package/dist/formatting/{_format.js → format.js} +40 -3
- package/dist/formatting/{_string_content.js → string_content.js} +21 -12
- package/dist/index.d.ts +55 -72
- package/dist/index.js +88 -66
- package/dist/settings/libraryDefaultLanguage.d.ts +2 -0
- package/dist/settings/libraryDefaultLanguage.js +4 -0
- package/dist/translation/dictionaries/_translateBundle.js +2 -2
- package/dist/translation/dictionaries/_updateProjectDictionary.js +4 -5
- package/dist/translation/react/_translateReact.js +2 -2
- package/dist/translation/strings/_translate.js +2 -2
- package/package.json +2 -2
- package/dist/codes/15924/CodeToScript.json +0 -215
- package/dist/codes/15924/ScriptToCode.json +0 -215
- package/dist/codes/3166/CodeToRegion.json +0 -296
- package/dist/codes/3166/RegionToCode.json +0 -296
- package/dist/codes/639-1/CodeToLanguage.json +0 -185
- package/dist/codes/639-1/LanguageToCode.json +0 -227
- package/dist/codes/639-3/CodeToLanguageTriletter.json +0 -186
- package/dist/codes/639-3/LanguageToCodeTriletter.json +0 -228
- package/dist/codes/getLanguageDirection.js +0 -71
- package/dist/codes/predefined/Predefined.json +0 -28
- /package/dist/codes/{determineLanguage.d.ts → _determineLanguage.d.ts} +0 -0
- /package/dist/codes/{getLanguageDirection.d.ts → _isSameLanguage.d.ts} +0 -0
- /package/dist/formatting/{_format.d.ts → format.d.ts} +0 -0
- /package/dist/formatting/{_string_content.d.ts → string_content.d.ts} +0 -0
package/dist/codes/codes.js
CHANGED
@@ -3,319 +3,93 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.
|
7
|
-
exports.
|
8
|
-
exports.
|
9
|
-
|
10
|
-
exports._isSameLanguage = _isSameLanguage;
|
11
|
-
// ----- IMPORTS ----- //
|
12
|
-
// Import modules for mapping ISO 639 codes to language names and vice versa
|
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
|
-
// Import modules for mapping ISO 639-3 codes (for languages without two-letter codes)
|
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
|
-
// Import module for mapping ISO 15924 script codes to script names
|
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
|
-
// Import module for mapping ISO 3166 region codes to region names
|
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
|
-
// Import predefined common regions
|
33
|
-
var Predefined_json_1 = __importDefault(require("./predefined/Predefined.json"));
|
34
|
-
var Predefined = Predefined_json_1.default;
|
35
|
-
// ----- VALIDITY CHECKS ----- //
|
6
|
+
exports._standardizeLanguageCode = exports._isValidLanguageCode = void 0;
|
7
|
+
exports._getLanguageName = _getLanguageName;
|
8
|
+
exports._getLanguageDirection = _getLanguageDirection;
|
9
|
+
var libraryDefaultLanguage_1 = __importDefault(require("../settings/libraryDefaultLanguage"));
|
36
10
|
/**
|
37
|
-
*
|
38
|
-
* @param {string} code - The language
|
39
|
-
* @returns {
|
11
|
+
* Checks if a given BCP 47 language code is valid.
|
12
|
+
* @param {string} code - The BCP 47 language code to validate.
|
13
|
+
* @returns {boolean} True if the BCP 47 code is valid, false otherwise.
|
40
14
|
* @internal
|
41
15
|
*/
|
42
|
-
function
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
var result = "".concat(codeParts[0].toLowerCase());
|
47
|
-
if (codeParts[1]) {
|
48
|
-
if (codeParts[1].length === 4) {
|
49
|
-
result += "-".concat(_capitalize(codeParts[1]));
|
50
|
-
if (codeParts[2] && codeParts[2].length === 2) {
|
51
|
-
result += "-".concat(codeParts[2].toUpperCase());
|
52
|
-
}
|
53
|
-
}
|
54
|
-
else if (codeParts[1].length === 2) {
|
55
|
-
result += "-".concat(codeParts[1].toUpperCase());
|
56
|
-
}
|
16
|
+
var _isValidLanguageCode = function (code) {
|
17
|
+
try {
|
18
|
+
var displayNames = new Intl.DisplayNames([libraryDefaultLanguage_1.default], { type: 'language' });
|
19
|
+
return displayNames.of(code) !== code;
|
57
20
|
}
|
58
|
-
|
59
|
-
}
|
60
|
-
/**
|
61
|
-
* Check if a given language-country-script code is valid.
|
62
|
-
* @param {string} code - The language-country-script code to validate.
|
63
|
-
* @returns {boolean} - Returns true if valid, false otherwise.
|
64
|
-
* @internal
|
65
|
-
*/
|
66
|
-
function _isValidLanguageCode(code) {
|
67
|
-
if (!code || typeof code !== 'string')
|
68
|
-
return false;
|
69
|
-
var codeParts = code.split('-');
|
70
|
-
if (!_mapCodeToLanguage(codeParts[0]))
|
21
|
+
catch (_a) {
|
71
22
|
return false;
|
72
|
-
if (codeParts[1]) {
|
73
|
-
if (codeParts[1].length === 4) {
|
74
|
-
if (!_mapCodeToScript(codeParts[1]))
|
75
|
-
return false;
|
76
|
-
if (codeParts[2] && codeParts[2].length === 2) {
|
77
|
-
if (!_mapCodeToRegion(codeParts[2]))
|
78
|
-
return false;
|
79
|
-
}
|
80
|
-
}
|
81
|
-
else if (codeParts[1].length === 2) {
|
82
|
-
if (!_mapCodeToRegion(codeParts[1]))
|
83
|
-
return false;
|
84
|
-
}
|
85
|
-
}
|
86
|
-
return true;
|
87
|
-
}
|
88
|
-
// ----- FORMATTING HELPER FUNCTIONS ----- //
|
89
|
-
/**
|
90
|
-
* Capitalizes the first letter of a code and converts the rest to lowercase.
|
91
|
-
* @param {string} code - The code to capitalize.
|
92
|
-
* @returns {string} The capitalized code.
|
93
|
-
*/
|
94
|
-
var _capitalize = function (code) {
|
95
|
-
if (code.length === 0)
|
96
|
-
return code;
|
97
|
-
return code.charAt(0).toUpperCase() + code.slice(1).toLowerCase();
|
98
|
-
};
|
99
|
-
// ----- MAPPING FUNCTIONS ----- //
|
100
|
-
/**
|
101
|
-
* Returns the name of a language from an ISO 639 code.
|
102
|
-
* @param {string} code - The ISO 639 code.
|
103
|
-
* @returns {string} The language name.
|
104
|
-
*/
|
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
|
-
return CodeToLanguage[code];
|
109
|
-
}
|
110
|
-
if ((code === null || code === void 0 ? void 0 : code.length) === 3) {
|
111
|
-
return CodeToLanguageTriletter[code];
|
112
23
|
}
|
113
|
-
return '';
|
114
|
-
};
|
115
|
-
/**
|
116
|
-
* Returns a BCP 47 language tag from a language name.
|
117
|
-
* Preferentially returns two-letter codes.
|
118
|
-
* @param {string} language - The language name.
|
119
|
-
* @returns {string} BCP 47 language tag.
|
120
|
-
*/
|
121
|
-
var _mapLanguageToCode = function (language) {
|
122
|
-
language = language === null || language === void 0 ? void 0 : language.toLowerCase();
|
123
|
-
return LanguageToCode[language] || LanguageToCodeTriletter[language] || '';
|
124
|
-
};
|
125
|
-
/**
|
126
|
-
* Returns the name of a script from an ISO 15924 code.
|
127
|
-
* @param {string} code - The ISO 15924 code.
|
128
|
-
* @returns {string} The script name.
|
129
|
-
*/
|
130
|
-
var _mapCodeToScript = function (code) {
|
131
|
-
code = _capitalize(code);
|
132
|
-
return CodeToScript[code] || '';
|
133
|
-
};
|
134
|
-
/**
|
135
|
-
* Returns an ISO 15924 code from a script name.
|
136
|
-
* @param {string} script - The script name.
|
137
|
-
* @returns {string} The ISO 15924 code.
|
138
|
-
*/
|
139
|
-
var _mapScriptToCode = function (script) {
|
140
|
-
script = script === null || script === void 0 ? void 0 : script.toLowerCase();
|
141
|
-
return ScriptToCode[script] || '';
|
142
|
-
};
|
143
|
-
/**
|
144
|
-
* Returns the name of a region from an ISO 3166 code.
|
145
|
-
* @param {string} code - The ISO 3166 code.
|
146
|
-
* @returns {string} The region name.
|
147
|
-
*/
|
148
|
-
var _mapCodeToRegion = function (code) {
|
149
|
-
code = code === null || code === void 0 ? void 0 : code.toUpperCase();
|
150
|
-
return CodeToRegion[code] || '';
|
151
|
-
};
|
152
|
-
/**
|
153
|
-
* Returns an ISO 3166 code from a region name.
|
154
|
-
* @param {string} region - The region name.
|
155
|
-
* @returns {string} The ISO 3166 code.
|
156
|
-
*/
|
157
|
-
var _mapRegionToCode = function (region) {
|
158
|
-
region = region === null || region === void 0 ? void 0 : region.toLowerCase();
|
159
|
-
return RegionToCode[region] || '';
|
160
24
|
};
|
25
|
+
exports._isValidLanguageCode = _isValidLanguageCode;
|
161
26
|
/**
|
27
|
+
* Standardizes a BCP 47 language code to ensure correct formatting.
|
28
|
+
* @param {string} code - The BCP 47 language code to standardize.
|
29
|
+
* @returns {string} The standardized BCP 47 language code, or an empty string if invalid.
|
162
30
|
* @internal
|
163
31
|
*/
|
164
|
-
function
|
165
|
-
|
166
|
-
|
167
|
-
/**
|
168
|
-
* Helper function to create a language object from a code.
|
169
|
-
* @param {string} code - The language code.
|
170
|
-
* @returns {LanguageObject|null} The language object.
|
171
|
-
*/
|
172
|
-
var _handleGetLanguageObject = function (code) {
|
173
|
-
if (!_isValidLanguageCode(code))
|
174
|
-
return null;
|
175
|
-
var codeParts = code.split('-');
|
176
|
-
var languageObject = {
|
177
|
-
language: _mapCodeToLanguage(codeParts[0]),
|
178
|
-
};
|
179
|
-
if (codeParts[1]) {
|
180
|
-
if (codeParts[1].length === 4) {
|
181
|
-
languageObject.script = _mapCodeToScript(codeParts[1]);
|
182
|
-
if (codeParts[2] && codeParts[2].length === 2) {
|
183
|
-
languageObject.region = _mapCodeToRegion(codeParts[2]);
|
184
|
-
}
|
185
|
-
}
|
186
|
-
else if (codeParts[1].length === 2) {
|
187
|
-
languageObject.region = _mapCodeToRegion(codeParts[1]);
|
188
|
-
}
|
32
|
+
var _standardizeLanguageCode = function (code) {
|
33
|
+
try {
|
34
|
+
return new Intl.Locale(code).toString();
|
189
35
|
}
|
190
|
-
|
191
|
-
|
192
|
-
// ----- LANGUAGE NAMES FROM CODES ----- //
|
193
|
-
/**
|
194
|
-
* Returns the language name(s) from an array of codes or a single code.
|
195
|
-
* @param {string|string[]} codes - The code or array of codes.
|
196
|
-
* @returns {string|string[]} The language name(s).
|
197
|
-
* @internal
|
198
|
-
*/
|
199
|
-
var _getLanguageName = function (codes) {
|
200
|
-
return Array.isArray(codes) ? codes.map(_handleGetLanguageName) : _handleGetLanguageName(codes);
|
201
|
-
};
|
202
|
-
exports._getLanguageName = _getLanguageName;
|
203
|
-
/**
|
204
|
-
* Helper function to get the language name from a code.
|
205
|
-
* @param {string} code - The language code.
|
206
|
-
* @returns {string} The language name.
|
207
|
-
*/
|
208
|
-
var _handleGetLanguageName = function (code) {
|
209
|
-
if (!_isValidLanguageCode(code))
|
36
|
+
catch (_a) {
|
37
|
+
// Return empty string instead of throwing an error
|
210
38
|
return '';
|
211
|
-
if (Predefined[code])
|
212
|
-
return Predefined[code];
|
213
|
-
var languageObject = _handleGetLanguageObject(code);
|
214
|
-
if (!languageObject)
|
215
|
-
return '';
|
216
|
-
var result = languageObject.language;
|
217
|
-
if (languageObject.script) {
|
218
|
-
result += ", ".concat(languageObject.script);
|
219
|
-
}
|
220
|
-
if (languageObject.region) {
|
221
|
-
result += ", ".concat(languageObject.region);
|
222
39
|
}
|
223
|
-
return result;
|
224
40
|
};
|
225
|
-
|
41
|
+
exports._standardizeLanguageCode = _standardizeLanguageCode;
|
226
42
|
/**
|
227
|
-
*
|
228
|
-
*
|
229
|
-
* @
|
43
|
+
* Retrieves the display name(s) of language code(s) using Intl.DisplayNames.
|
44
|
+
*
|
45
|
+
* @param {string | string[]} code - A language code or an array of codes.
|
46
|
+
* @param {string} [defaultLanguage=libraryDefaultLanguage] - The language for display names.
|
47
|
+
* @returns {string | string[]} The display name(s) corresponding to the code(s), or empty string(s) if invalid.
|
230
48
|
* @internal
|
231
49
|
*/
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
*/
|
241
|
-
var _handleGetLanguageCode = function (language) {
|
242
|
-
if (typeof language === 'string')
|
243
|
-
return _handleGetLanguageCodeFromString(language);
|
244
|
-
return _handleGetLanguageCodeFromObject(language);
|
245
|
-
};
|
246
|
-
/**
|
247
|
-
* Helper function to get the language code from a language name string.
|
248
|
-
* @param {string} language - The language name.
|
249
|
-
* @returns {string} The language code.
|
250
|
-
*/
|
251
|
-
var _handleGetLanguageCodeFromString = function (language) {
|
252
|
-
var subtagStrings = language.split(',').map(function (string) { return string.trim(); });
|
253
|
-
var code = _mapLanguageToCode(subtagStrings[0]);
|
254
|
-
if (code) {
|
255
|
-
if (subtagStrings.length === 3) {
|
256
|
-
code += "-".concat(_mapScriptToCode(subtagStrings[1]));
|
257
|
-
code += "-".concat(_mapRegionToCode(subtagStrings[2]));
|
50
|
+
function _getLanguageName(code, defaultLanguage) {
|
51
|
+
if (defaultLanguage === void 0) { defaultLanguage = libraryDefaultLanguage_1.default; }
|
52
|
+
try {
|
53
|
+
var displayNames_1 = new Intl.DisplayNames([defaultLanguage], { type: 'language' });
|
54
|
+
if (typeof code === 'string') {
|
55
|
+
// Handle the case where it's a single language code
|
56
|
+
var name = displayNames_1.of(code);
|
57
|
+
return name || '';
|
258
58
|
}
|
259
|
-
else if (
|
260
|
-
|
261
|
-
|
262
|
-
tag = _mapRegionToCode(subtagStrings[1]);
|
263
|
-
if (tag)
|
264
|
-
code += "-".concat(tag);
|
59
|
+
else if (Array.isArray(code)) {
|
60
|
+
// Handle the case where it's an array of language codes
|
61
|
+
return code.map(function (c) { return displayNames_1.of(c) || ''; });
|
265
62
|
}
|
63
|
+
// If code is neither string nor array, return empty string
|
64
|
+
return '';
|
266
65
|
}
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
66
|
+
catch (_a) {
|
67
|
+
// In case Intl.DisplayNames construction fails, return empty string(s)
|
68
|
+
if (typeof code === 'string') {
|
69
|
+
return '';
|
70
|
+
}
|
71
|
+
else if (Array.isArray(code)) {
|
72
|
+
return code.map(function () { return ''; });
|
272
73
|
}
|
273
|
-
}
|
274
|
-
return code;
|
275
|
-
};
|
276
|
-
/**
|
277
|
-
* Helper function to get the language code from a language object.
|
278
|
-
* @param {LanguageObject} languageObject - The language object.
|
279
|
-
* @returns {string} The language code.
|
280
|
-
*/
|
281
|
-
var _handleGetLanguageCodeFromObject = function (languageObject) {
|
282
|
-
if (!(languageObject === null || languageObject === void 0 ? void 0 : languageObject.language))
|
283
74
|
return '';
|
284
|
-
var code = languageObject.language.toLowerCase();
|
285
|
-
if (languageObject.script) {
|
286
|
-
code += "-".concat(_capitalize(languageObject.script));
|
287
|
-
}
|
288
|
-
if (languageObject.region) {
|
289
|
-
code += "-".concat(languageObject.region.toUpperCase());
|
290
75
|
}
|
291
|
-
|
292
|
-
};
|
76
|
+
}
|
293
77
|
/**
|
78
|
+
* Get the text direction for a given language code using the Intl.Locale API.
|
79
|
+
*
|
80
|
+
* @param {string} code - The language code to check.
|
81
|
+
* @returns {string} - 'rtl' if the language is right-to-left, otherwise 'ltr'.
|
294
82
|
* @internal
|
295
83
|
*/
|
296
|
-
function
|
297
|
-
var
|
298
|
-
|
299
|
-
|
84
|
+
function _getLanguageDirection(code) {
|
85
|
+
var _a;
|
86
|
+
try {
|
87
|
+
var locale = new Intl.Locale(code);
|
88
|
+
// Return 'rtl' if the text direction of the language is right-to-left, otherwise 'ltr'
|
89
|
+
return ((_a = locale === null || locale === void 0 ? void 0 : locale.textInfo) === null || _a === void 0 ? void 0 : _a.direction) === 'rtl' ? 'rtl' : 'ltr';
|
300
90
|
}
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
}
|
305
|
-
if (codes.length < 2)
|
306
|
-
return false;
|
307
|
-
var language = null;
|
308
|
-
for (var i = 0; i < codes.length; i++) {
|
309
|
-
if (typeof codes[i] !== 'string')
|
310
|
-
return false;
|
311
|
-
var languageCode = codes[i].split('-')[0];
|
312
|
-
var currentLanguage = _mapCodeToLanguage(languageCode);
|
313
|
-
if (language === null) {
|
314
|
-
language = currentLanguage;
|
315
|
-
}
|
316
|
-
else if (language !== currentLanguage) {
|
317
|
-
return false;
|
318
|
-
}
|
91
|
+
catch (_b) {
|
92
|
+
// If the code is invalid or causes an error, fallback to 'ltr'
|
93
|
+
return 'ltr';
|
319
94
|
}
|
320
|
-
return true;
|
321
95
|
}
|
@@ -10,10 +10,16 @@ var __assign = (this && this.__assign) || function () {
|
|
10
10
|
};
|
11
11
|
return __assign.apply(this, arguments);
|
12
12
|
};
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
|
+
};
|
13
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
14
17
|
exports._formatNum = _formatNum;
|
15
18
|
exports._formatDateTime = _formatDateTime;
|
16
19
|
exports._formatCurrency = _formatCurrency;
|
20
|
+
exports._formatList = _formatList;
|
21
|
+
exports._formatRelativeTime = _formatRelativeTime;
|
22
|
+
var libraryDefaultLanguage_1 = __importDefault(require("../settings/libraryDefaultLanguage"));
|
17
23
|
/**
|
18
24
|
* Formats a number according to the specified languages and options.
|
19
25
|
*
|
@@ -26,7 +32,7 @@ exports._formatCurrency = _formatCurrency;
|
|
26
32
|
* @internal
|
27
33
|
*/
|
28
34
|
function _formatNum(_a) {
|
29
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [
|
35
|
+
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage_1.default] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
30
36
|
return new Intl.NumberFormat(languages, __assign({ numberingSystem: 'latn' }, options)).format(value);
|
31
37
|
}
|
32
38
|
/**
|
@@ -41,7 +47,7 @@ function _formatNum(_a) {
|
|
41
47
|
* @internal
|
42
48
|
*/
|
43
49
|
function _formatDateTime(_a) {
|
44
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [
|
50
|
+
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage_1.default] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
45
51
|
return new Intl.DateTimeFormat(languages, __assign({ calendar: "gregory", numberingSystem: "latn" }, options)).format(value);
|
46
52
|
}
|
47
53
|
/**
|
@@ -57,6 +63,37 @@ function _formatDateTime(_a) {
|
|
57
63
|
* @internal
|
58
64
|
*/
|
59
65
|
function _formatCurrency(_a) {
|
60
|
-
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [
|
66
|
+
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage_1.default] : _b, _c = _a.currency, currency = _c === void 0 ? 'USD' : _c, _d = _a.options, options = _d === void 0 ? {} : _d;
|
61
67
|
return new Intl.NumberFormat(languages, __assign({ style: 'currency', currency: currency, numberingSystem: 'latn' }, options)).format(value);
|
62
68
|
}
|
69
|
+
/**
|
70
|
+
* Formats a list of items according to the specified languages and options.
|
71
|
+
*
|
72
|
+
* @param {Object} params - The parameters for the list formatting.
|
73
|
+
* @param {Array<string | number>} params.value - The list of items to format.
|
74
|
+
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
75
|
+
* @param {Intl.ListFormatOptions} [params.options={}] - Additional options for list formatting.
|
76
|
+
*
|
77
|
+
* @returns {string} The formatted list.
|
78
|
+
* @internal
|
79
|
+
*/
|
80
|
+
function _formatList(_a) {
|
81
|
+
var value = _a.value, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage_1.default] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
82
|
+
return new Intl.ListFormat(languages, __assign({ type: 'conjunction', style: 'long' }, options)).format(value);
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Formats a relative time value according to the specified languages and options.
|
86
|
+
*
|
87
|
+
* @param {Object} params - The parameters for the relative time formatting.
|
88
|
+
* @param {number} params.value - The relative time value to format.
|
89
|
+
* @param {Intl.RelativeTimeFormatUnit} params.unit - The unit of time (e.g., 'second', 'minute', 'hour', 'day', 'week', 'month', 'year').
|
90
|
+
* @param {string | string[]} [params.languages=['en']] - The languages to use for formatting.
|
91
|
+
* @param {Intl.RelativeTimeFormatOptions} [params.options={}] - Additional options for relative time formatting.
|
92
|
+
*
|
93
|
+
* @returns {string} The formatted relative time string.
|
94
|
+
* @internal
|
95
|
+
*/
|
96
|
+
function _formatRelativeTime(_a) {
|
97
|
+
var value = _a.value, unit = _a.unit, _b = _a.languages, languages = _b === void 0 ? [libraryDefaultLanguage_1.default] : _b, _c = _a.options, options = _c === void 0 ? {} : _c;
|
98
|
+
return new Intl.RelativeTimeFormat(languages, __assign({ style: "long", numeric: 'auto' }, options)).format(value, unit);
|
99
|
+
}
|
@@ -10,10 +10,14 @@ var __assign = (this && this.__assign) || function () {
|
|
10
10
|
};
|
11
11
|
return __assign.apply(this, arguments);
|
12
12
|
};
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
15
|
+
};
|
13
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
14
17
|
exports._splitStringToContent = _splitStringToContent;
|
15
18
|
exports._renderContentToString = _renderContentToString;
|
16
|
-
var
|
19
|
+
var libraryDefaultLanguage_1 = __importDefault(require("../settings/libraryDefaultLanguage"));
|
20
|
+
var format_1 = require("./format");
|
17
21
|
// Variable types mapping
|
18
22
|
var variableTypeMap = {
|
19
23
|
var: "variable",
|
@@ -28,6 +32,8 @@ var variableTypeMap = {
|
|
28
32
|
* @internal
|
29
33
|
*/
|
30
34
|
function _splitStringToContent(string) {
|
35
|
+
if (typeof string !== 'string')
|
36
|
+
throw new Error("splitStringToContent: ".concat(string, " is not a string!"));
|
31
37
|
var result = [];
|
32
38
|
var regex = /{([^}]+)}/g;
|
33
39
|
var lastIndex = 0;
|
@@ -68,15 +74,15 @@ function _splitStringToContent(string) {
|
|
68
74
|
* @internal
|
69
75
|
*/
|
70
76
|
function _renderContentToString(content, languages, variables, variableOptions) {
|
71
|
-
if (languages === void 0) { languages =
|
77
|
+
if (languages === void 0) { languages = libraryDefaultLanguage_1.default; }
|
72
78
|
if (variables === void 0) { variables = {}; }
|
73
79
|
if (variableOptions === void 0) { variableOptions = {}; }
|
74
|
-
if (typeof content === 'string')
|
80
|
+
if (typeof content === 'string')
|
75
81
|
content = _splitStringToContent(content);
|
76
|
-
|
77
|
-
if (typeof content === 'string') {
|
82
|
+
if (typeof content === 'string')
|
78
83
|
return content;
|
79
|
-
|
84
|
+
if (!Array.isArray(content))
|
85
|
+
throw new Error("renderContentToString: content ".concat(content, " is invalid"));
|
80
86
|
return content.map(function (item) {
|
81
87
|
var _a;
|
82
88
|
if (typeof item === 'string')
|
@@ -85,18 +91,21 @@ function _renderContentToString(content, languages, variables, variableOptions)
|
|
85
91
|
var value = variables[item.key];
|
86
92
|
if (!item.variable)
|
87
93
|
return value;
|
88
|
-
if (item.variable === "number") {
|
89
|
-
return (0,
|
94
|
+
else if (item.variable === "number") {
|
95
|
+
return (0, format_1._formatNum)({
|
90
96
|
value: value,
|
91
97
|
languages: languages,
|
92
98
|
options: variableOptions[item.key]
|
93
99
|
});
|
94
100
|
}
|
95
|
-
if (item.variable === "currency") {
|
96
|
-
return (0,
|
101
|
+
else if (item.variable === "currency") {
|
102
|
+
return (0, format_1._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 })));
|
103
|
+
}
|
104
|
+
else if (item.variable === "datetime") {
|
105
|
+
return (0, format_1._formatDateTime)(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })));
|
97
106
|
}
|
98
|
-
if (item.variable === "
|
99
|
-
return (0,
|
107
|
+
else if (item.variable === "list") {
|
108
|
+
return (0, format_1._formatList)(__assign({ value: value, languages: languages }, (variableOptions[item.key] && { options: variableOptions[item.key] })));
|
100
109
|
}
|
101
110
|
return value;
|
102
111
|
}
|