@webiny/i18n 0.0.0-unstable.79032b23a5 β†’ 0.0.0-unstable.7be00a75a9

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.
Files changed (48) hide show
  1. package/I18n.d.ts +2 -2
  2. package/I18n.js +166 -370
  3. package/I18n.js.map +1 -1
  4. package/README.md +7 -13
  5. package/extractor/extract.js +25 -47
  6. package/extractor/extract.js.map +1 -1
  7. package/extractor/index.js +33 -44
  8. package/extractor/index.js.map +1 -1
  9. package/index.d.ts +3 -3
  10. package/index.js +11 -28
  11. package/index.js.map +1 -1
  12. package/modifiers/countModifier.d.ts +1 -1
  13. package/modifiers/countModifier.js +17 -34
  14. package/modifiers/countModifier.js.map +1 -1
  15. package/modifiers/dateModifier.d.ts +1 -1
  16. package/modifiers/dateModifier.js +8 -14
  17. package/modifiers/dateModifier.js.map +1 -1
  18. package/modifiers/dateTimeModifier.d.ts +1 -1
  19. package/modifiers/dateTimeModifier.js +8 -14
  20. package/modifiers/dateTimeModifier.js.map +1 -1
  21. package/modifiers/genderModifier.d.ts +1 -1
  22. package/modifiers/genderModifier.js +8 -14
  23. package/modifiers/genderModifier.js.map +1 -1
  24. package/modifiers/ifModifier.d.ts +1 -1
  25. package/modifiers/ifModifier.js +8 -14
  26. package/modifiers/ifModifier.js.map +1 -1
  27. package/modifiers/index.d.ts +1 -1
  28. package/modifiers/index.js +22 -18
  29. package/modifiers/index.js.map +1 -1
  30. package/modifiers/numberModifier.d.ts +1 -1
  31. package/modifiers/numberModifier.js +8 -14
  32. package/modifiers/numberModifier.js.map +1 -1
  33. package/modifiers/pluralModifier.d.ts +1 -1
  34. package/modifiers/pluralModifier.js +17 -32
  35. package/modifiers/pluralModifier.js.map +1 -1
  36. package/modifiers/priceModifier.d.ts +1 -1
  37. package/modifiers/priceModifier.js +8 -14
  38. package/modifiers/priceModifier.js.map +1 -1
  39. package/modifiers/timeModifier.d.ts +1 -1
  40. package/modifiers/timeModifier.js +8 -14
  41. package/modifiers/timeModifier.js.map +1 -1
  42. package/package.json +23 -29
  43. package/processors/default.d.ts +1 -1
  44. package/processors/default.js +34 -47
  45. package/processors/default.js.map +1 -1
  46. package/types.d.ts +3 -3
  47. package/types.js +0 -5
  48. package/types.js.map +0 -1
package/I18n.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { Formats, I18NDataValues, Modifier, NumberFormat, PriceFormat, Processor, ProcessorResult, Translations, Translator } from "./types";
2
- export declare type Translated = ((values: I18NDataValues) => ProcessorResult | null) | ProcessorResult | null;
1
+ import type { Formats, I18NDataValues, Modifier, NumberFormat, PriceFormat, Processor, ProcessorResult, Translations, Translator } from "./types.js";
2
+ export type Translated = ((values: I18NDataValues) => ProcessorResult | null) | ProcessorResult | null;
3
3
  /**
4
4
  * Main class used for all I18n needs.
5
5
  */
package/I18n.js CHANGED
@@ -1,382 +1,178 @@
1
- "use strict";
2
-
3
- var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = void 0;
9
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
- var _accounting = _interopRequireDefault(require("accounting"));
11
- var fecha = _interopRequireWildcard(require("fecha"));
12
- var _shortHash = _interopRequireDefault(require("short-hash"));
13
- var _assign = _interopRequireDefault(require("lodash/assign"));
14
- var _get = _interopRequireDefault(require("lodash/get"));
15
- /**
16
- * Package short-hash has no types.
17
- */
18
- // @ts-ignore
19
-
20
- /**
21
- * Main class used for all I18n needs.
22
- */
1
+ import accounting from "accounting";
2
+ import lodash_assign from "lodash/assign.js";
3
+ import get from "lodash/get.js";
4
+ import { hash } from "ohash";
5
+ import * as __rspack_external_fecha from "fecha";
23
6
  class I18N {
24
- constructor() {
25
- (0, _defineProperty2.default)(this, "locale", null);
26
- (0, _defineProperty2.default)(this, "defaultFormats", void 0);
27
- (0, _defineProperty2.default)(this, "translations", void 0);
28
- (0, _defineProperty2.default)(this, "modifiers", void 0);
29
- (0, _defineProperty2.default)(this, "processors", void 0);
30
- /**
31
- * If we fail to fetch formats for currently selected locale, these default formats will be used.
32
- * @type {{date: string, time: string, datetime: string, number: string}}
33
- */
34
- this.defaultFormats = this.getDefaultFormats();
35
-
36
- /**
37
- * All currently-loaded translations, for easier (synchronous) access.
38
- * @type {{}}
39
- */
40
- this.translations = {};
41
-
42
- /**
43
- * All registered modifiers.
44
- * @type {{}}
45
- */
46
- this.modifiers = {};
47
-
48
- /**
49
- * All registered processors.
50
- * Default built-in processors are registered immediately below.
51
- * @type {{}}
52
- */
53
- this.processors = {};
54
- }
55
- translate(base, namespace) {
56
- // Returns full translation for given base text in given namespace (optional).
57
- // If translation isn't found, base text will be returned.
58
- // We create a key out of given namespace and base text.
59
-
60
- if (!namespace) {
61
- throw Error("I18N text namespace not defined.");
62
- }
63
- base = (0, _get.default)(base, "raw.0", base);
64
- let translation = this.getTranslation(namespace + "." + (0, _shortHash.default)(base));
65
- if (!translation) {
66
- translation = base;
67
- }
68
- const hasVariables = base.includes("{") && base.includes("}");
69
- if (hasVariables) {
70
- // eslint-disable-next-line @typescript-eslint/no-this-alias
71
- const $this = this;
72
- return function i18n(values) {
7
+ constructor(){
8
+ this.locale = null;
9
+ this.defaultFormats = this.getDefaultFormats();
10
+ this.translations = {};
11
+ this.modifiers = {};
12
+ this.processors = {};
13
+ }
14
+ translate(base, namespace) {
15
+ if (!namespace) throw Error("I18N text namespace not defined.");
16
+ base = get(base, "raw.0", base);
17
+ let translation = this.getTranslation(namespace + "." + hash(base));
18
+ if (!translation) translation = base;
19
+ const hasVariables = base.includes("{") && base.includes("}");
20
+ if (hasVariables) {
21
+ const $this = this;
22
+ return function(values) {
23
+ const data = {
24
+ translation: translation,
25
+ base,
26
+ namespace,
27
+ values,
28
+ i18n: $this
29
+ };
30
+ for(const key in $this.processors){
31
+ const processor = $this.processors[key];
32
+ if (processor.canExecute(data)) return processor.execute(data);
33
+ }
34
+ return null;
35
+ };
36
+ }
73
37
  const data = {
74
- translation: translation,
75
- base,
76
- namespace,
77
- values,
78
- i18n: $this
38
+ translation,
39
+ base,
40
+ namespace,
41
+ values: {},
42
+ i18n: this
79
43
  };
80
- for (const key in $this.processors) {
81
- const processor = $this.processors[key];
82
- if (processor.canExecute(data)) {
83
- return processor.execute(data);
84
- }
85
- }
44
+ for(const key in this.processors)if (this.processors[key].canExecute(data)) return this.processors[key].execute(data);
86
45
  return null;
87
- };
88
- }
89
- const data = {
90
- translation,
91
- base,
92
- namespace,
93
- values: {},
94
- i18n: this
95
- };
96
- for (const key in this.processors) {
97
- if (this.processors[key].canExecute(data)) {
98
- return this.processors[key].execute(data);
99
- }
100
- }
101
- return null;
102
- }
103
- namespace(namespace) {
104
- return base => {
105
- return this.translate(base, namespace);
106
- };
107
- }
108
- ns(namespace) {
109
- return this.namespace(namespace);
110
- }
111
-
112
- /**
113
- * Formats and outputs date.
114
- * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
115
- */
116
- date(value, outputFormat, inputFormat) {
117
- if (!outputFormat) {
118
- outputFormat = this.getDateFormat();
119
- }
120
- if (!inputFormat) {
121
- inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
122
- }
123
- let parsedValue;
124
- if (typeof value === "string") {
125
- parsedValue = fecha.parse(value, inputFormat);
126
- } else {
127
- parsedValue = value;
128
- }
129
- return fecha.format(parsedValue, outputFormat);
130
- }
131
-
132
- /**
133
- * Formats and outputs time.
134
- * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
135
- */
136
- time(value, outputFormat, inputFormat) {
137
- if (!outputFormat) {
138
- outputFormat = this.getTimeFormat();
139
- }
140
- if (!inputFormat) {
141
- inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
142
46
  }
143
- let parsedValue;
144
- if (typeof value === "string") {
145
- parsedValue = fecha.parse(value, inputFormat);
146
- } else {
147
- parsedValue = value;
47
+ namespace(namespace) {
48
+ return (base)=>this.translate(base, namespace);
49
+ }
50
+ ns(namespace) {
51
+ return this.namespace(namespace);
52
+ }
53
+ date(value, outputFormat, inputFormat) {
54
+ if (!outputFormat) outputFormat = this.getDateFormat();
55
+ if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
56
+ let parsedValue;
57
+ parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
58
+ return __rspack_external_fecha.format(parsedValue, outputFormat);
59
+ }
60
+ time(value, outputFormat, inputFormat) {
61
+ if (!outputFormat) outputFormat = this.getTimeFormat();
62
+ if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
63
+ let parsedValue;
64
+ parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
65
+ return __rspack_external_fecha.format(parsedValue, outputFormat);
66
+ }
67
+ dateTime(value, outputFormat, inputFormat) {
68
+ if (!outputFormat) outputFormat = this.getDateTimeFormat();
69
+ if (!inputFormat) inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
70
+ let parsedValue;
71
+ parsedValue = "string" == typeof value ? __rspack_external_fecha.parse(value, inputFormat) : value instanceof Date ? value : new Date(value);
72
+ return __rspack_external_fecha.format(parsedValue, outputFormat);
73
+ }
74
+ price(value, outputFormat) {
75
+ outputFormat = outputFormat ? lodash_assign({}, this.defaultFormats.price, outputFormat) : this.getPriceFormat();
76
+ let format = outputFormat.format;
77
+ format = format.replace("{symbol}", "%s");
78
+ format = format.replace("{amount}", "%v");
79
+ return accounting.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
80
+ }
81
+ number(value, outputFormat) {
82
+ outputFormat = outputFormat ? lodash_assign({}, this.defaultFormats.number, outputFormat) : this.getNumberFormat();
83
+ return accounting.formatNumber(value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
84
+ }
85
+ getTranslation(key) {
86
+ if (!key) return null;
87
+ return this.translations[key];
88
+ }
89
+ getTranslations() {
90
+ return this.translations;
91
+ }
92
+ hasTranslation(key) {
93
+ return key in this.translations;
94
+ }
95
+ setTranslation(key, translation) {
96
+ this.translations[key] = translation;
97
+ return this;
98
+ }
99
+ setTranslations(translations) {
100
+ this.translations = translations;
101
+ return this;
102
+ }
103
+ clearTranslations() {
104
+ this.setTranslations({});
105
+ return this;
106
+ }
107
+ mergeTranslations(translations) {
108
+ return lodash_assign(this.translations, translations);
109
+ }
110
+ getLocale() {
111
+ return this.locale;
112
+ }
113
+ setLocale(locale) {
114
+ this.locale = locale;
115
+ return this;
116
+ }
117
+ registerModifier(modifier) {
118
+ this.modifiers[modifier.name] = modifier;
119
+ return this;
120
+ }
121
+ registerModifiers(modifiers) {
122
+ modifiers.forEach((modifier)=>this.registerModifier(modifier));
123
+ return this;
124
+ }
125
+ unregisterModifier(name) {
126
+ delete this.modifiers[name];
127
+ return this;
128
+ }
129
+ registerProcessor(processor) {
130
+ this.processors[processor.name] = processor;
131
+ return this;
132
+ }
133
+ registerProcessors(processors) {
134
+ processors.forEach((processor)=>this.registerProcessor(processor));
135
+ return this;
136
+ }
137
+ unregisterProcessor(name) {
138
+ delete this.processors[name];
139
+ return this;
140
+ }
141
+ getDefaultFormats() {
142
+ return {
143
+ date: "DD/MM/YYYY",
144
+ time: "HH:mm",
145
+ datetime: "DD/MM/YYYY HH:mm",
146
+ price: {
147
+ symbol: "",
148
+ format: "{symbol}{amount}",
149
+ decimal: ".",
150
+ thousand: ",",
151
+ precision: 2
152
+ },
153
+ number: {
154
+ decimal: ".",
155
+ thousand: ",",
156
+ precision: 2
157
+ }
158
+ };
148
159
  }
149
- return fecha.format(parsedValue, outputFormat);
150
- }
151
-
152
- /**
153
- * Formats and outputs date/time.
154
- * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.
155
- */
156
- dateTime(value, outputFormat, inputFormat) {
157
- if (!outputFormat) {
158
- outputFormat = this.getDateTimeFormat();
160
+ getDateFormat() {
161
+ return get(this.locale, "formats.date", this.defaultFormats.date);
159
162
  }
160
- if (!inputFormat) {
161
- inputFormat = "YYYY-MM-DDTHH:mm:ss.SSSZ";
163
+ getTimeFormat() {
164
+ return get(this.locale, "formats.time", this.defaultFormats.time);
162
165
  }
163
- let parsedValue;
164
- if (typeof value === "string") {
165
- parsedValue = fecha.parse(value, inputFormat);
166
- } else {
167
- parsedValue = value;
166
+ getDateTimeFormat() {
167
+ return get(this.locale, "formats.datetime", this.defaultFormats.datetime);
168
168
  }
169
- return fecha.format(parsedValue, outputFormat);
170
- }
171
-
172
- /**
173
- * Outputs formatted number as amount of price.
174
- */
175
- price(value, outputFormat) {
176
- if (!outputFormat) {
177
- outputFormat = this.getPriceFormat();
178
- } else {
179
- outputFormat = (0, _assign.default)({}, this.defaultFormats.price, outputFormat);
169
+ getPriceFormat() {
170
+ return lodash_assign({}, this.defaultFormats.price, get(this.locale, "formats.price", {}));
180
171
  }
181
-
182
- // Convert placeholders to accounting's placeholders.
183
- let format = outputFormat.format;
184
- format = format.replace("{symbol}", "%s");
185
- format = format.replace("{amount}", "%v");
186
- return _accounting.default.formatMoney(value, outputFormat.symbol, outputFormat.precision, outputFormat.thousand, outputFormat.decimal, format);
187
- }
188
-
189
- /**
190
- * Outputs formatted number.
191
- */
192
- number(value, outputFormat) {
193
- if (!outputFormat) {
194
- outputFormat = this.getNumberFormat();
195
- } else {
196
- outputFormat = (0, _assign.default)({}, this.defaultFormats.number, outputFormat);
172
+ getNumberFormat() {
173
+ return lodash_assign({}, this.defaultFormats.number, get(this.locale, "formats.number", {}));
197
174
  }
198
- return _accounting.default.formatNumber(
199
- /**
200
- * Cast as number because method transforms it internally.
201
- */
202
- value, outputFormat.precision, outputFormat.thousand, outputFormat.decimal);
203
- }
204
-
205
- /**
206
- * Returns translation for given text key.
207
- */
208
- getTranslation(key) {
209
- if (!key) {
210
- return null;
211
- }
212
- return this.translations[key];
213
- }
214
-
215
- /**
216
- * Returns all translations for current locale.
217
- */
218
- getTranslations() {
219
- return this.translations;
220
- }
221
-
222
- /**
223
- * Returns true if given key has a translation for currently set locale.
224
- */
225
- hasTranslation(key) {
226
- return key in this.translations;
227
- }
228
-
229
- /**
230
- * Sets translation for given text key.
231
- */
232
- setTranslation(key, translation) {
233
- this.translations[key] = translation;
234
- return this;
235
- }
236
-
237
- /**
238
- * Sets translations that will be used.
239
- */
240
- setTranslations(translations) {
241
- this.translations = translations;
242
- return this;
243
- }
244
-
245
- /**
246
- * Clears all translations.
247
- */
248
- clearTranslations() {
249
- this.setTranslations({});
250
- return this;
251
- }
252
-
253
- /**
254
- * Merges given translations object with already existing.
255
- */
256
-
257
- mergeTranslations(translations) {
258
- return (0, _assign.default)(this.translations, translations);
259
- }
260
-
261
- /**
262
- * Returns currently selected locale (locale's key).
263
- */
264
- getLocale() {
265
- return this.locale;
266
- }
267
-
268
- /**
269
- * Sets current locale.
270
- */
271
- setLocale(locale) {
272
- this.locale = locale;
273
- return this;
274
- }
275
-
276
- /**
277
- * Registers single modifier.
278
- */
279
- registerModifier(modifier) {
280
- this.modifiers[modifier.name] = modifier;
281
- return this;
282
- }
283
-
284
- /**
285
- * Registers all modifiers in given array.
286
- */
287
- registerModifiers(modifiers) {
288
- modifiers.forEach(modifier => this.registerModifier(modifier));
289
- return this;
290
- }
291
-
292
- /**
293
- * Unregisters given modifier.
294
- */
295
- unregisterModifier(name) {
296
- delete this.modifiers[name];
297
- return this;
298
- }
299
-
300
- /**
301
- * Registers single processor.
302
- */
303
- registerProcessor(processor) {
304
- this.processors[processor.name] = processor;
305
- return this;
306
- }
307
-
308
- /**
309
- * Registers all processors in given array.
310
- */
311
- registerProcessors(processors) {
312
- processors.forEach(processor => this.registerProcessor(processor));
313
- return this;
314
- }
315
-
316
- /**
317
- * Unregisters given processor.
318
- */
319
- unregisterProcessor(name) {
320
- delete this.processors[name];
321
- return this;
322
- }
323
-
324
- /**
325
- * Returns default formats
326
- */
327
- getDefaultFormats() {
328
- return {
329
- date: "DD/MM/YYYY",
330
- time: "HH:mm",
331
- datetime: "DD/MM/YYYY HH:mm",
332
- price: {
333
- symbol: "",
334
- format: "{symbol}{amount}",
335
- decimal: ".",
336
- thousand: ",",
337
- precision: 2
338
- },
339
- number: {
340
- decimal: ".",
341
- thousand: ",",
342
- precision: 2
343
- }
344
- };
345
- }
346
-
347
- /**
348
- * Returns current format to be used when outputting dates.
349
- */
350
- getDateFormat() {
351
- return (0, _get.default)(this.locale, "formats.date", this.defaultFormats.date);
352
- }
353
-
354
- /**
355
- * Returns current format to be used when outputting time.
356
- */
357
- getTimeFormat() {
358
- return (0, _get.default)(this.locale, "formats.time", this.defaultFormats.time);
359
- }
360
-
361
- /**
362
- * Returns current format to be used when outputting date/time.
363
- */
364
- getDateTimeFormat() {
365
- return (0, _get.default)(this.locale, "formats.datetime", this.defaultFormats.datetime);
366
- }
367
-
368
- /**
369
- * Returns current format to be used when outputting prices.
370
- */
371
- getPriceFormat() {
372
- return (0, _assign.default)({}, this.defaultFormats.price, (0, _get.default)(this.locale, "formats.price", {}));
373
- }
374
-
375
- /**
376
- * Returns current format to be used when outputting numbers.
377
- */
378
- getNumberFormat() {
379
- return (0, _assign.default)({}, this.defaultFormats.number, (0, _get.default)(this.locale, "formats.number", {}));
380
- }
381
175
  }
382
- exports.default = I18N;
176
+ export default I18N;
177
+
178
+ //# sourceMappingURL=I18n.js.map
package/I18n.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["I18N","constructor","defaultFormats","getDefaultFormats","translations","modifiers","processors","translate","base","namespace","Error","lodashGet","translation","getTranslation","hash","hasVariables","includes","$this","i18n","values","data","key","processor","canExecute","execute","ns","date","value","outputFormat","inputFormat","getDateFormat","parsedValue","fecha","parse","format","time","getTimeFormat","dateTime","getDateTimeFormat","price","getPriceFormat","lodashAssign","replace","accounting","formatMoney","symbol","precision","thousand","decimal","number","getNumberFormat","formatNumber","getTranslations","hasTranslation","setTranslation","setTranslations","clearTranslations","mergeTranslations","getLocale","locale","setLocale","registerModifier","modifier","name","registerModifiers","forEach","unregisterModifier","registerProcessor","registerProcessors","unregisterProcessor","datetime"],"sources":["I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\n/**\n * Package short-hash has no types.\n */\n// @ts-ignore\nimport hash from \"short-hash\";\nimport lodashAssign from \"lodash/assign\";\nimport lodashGet from \"lodash/get\";\n\nimport {\n Formats,\n I18NData,\n I18NDataValues,\n Modifier,\n NumberFormat,\n PriceFormat,\n Processor,\n ProcessorResult,\n Translations,\n Translator\n} from \"./types\";\n\nexport type Translated =\n | ((values: I18NDataValues) => ProcessorResult | null)\n | ProcessorResult\n | null;\n/**\n * Main class used for all I18n needs.\n */\nexport default class I18N {\n public locale: string | null = null;\n public defaultFormats: Formats;\n public translations: Translations;\n public modifiers: {\n [name: string]: Modifier;\n };\n public processors: {\n [name: string]: Processor;\n };\n\n public constructor() {\n /**\n * If we fail to fetch formats for currently selected locale, these default formats will be used.\n * @type {{date: string, time: string, datetime: string, number: string}}\n */\n this.defaultFormats = this.getDefaultFormats();\n\n /**\n * All currently-loaded translations, for easier (synchronous) access.\n * @type {{}}\n */\n this.translations = {};\n\n /**\n * All registered modifiers.\n * @type {{}}\n */\n this.modifiers = {};\n\n /**\n * All registered processors.\n * Default built-in processors are registered immediately below.\n * @type {{}}\n */\n this.processors = {};\n }\n\n public translate(base: string, namespace?: string): Translated {\n // Returns full translation for given base text in given namespace (optional).\n // If translation isn't found, base text will be returned.\n // We create a key out of given namespace and base text.\n\n if (!namespace) {\n throw Error(\"I18N text namespace not defined.\");\n }\n\n base = lodashGet(base, \"raw.0\", base);\n\n let translation: string | null = this.getTranslation(namespace + \".\" + hash(base));\n\n if (!translation) {\n translation = base;\n }\n\n const hasVariables = base.includes(\"{\") && base.includes(\"}\");\n if (hasVariables) {\n // eslint-disable-next-line @typescript-eslint/no-this-alias\n const $this = this;\n return function i18n(values: I18NDataValues) {\n const data: I18NData = {\n translation: translation as string,\n base,\n namespace,\n values,\n i18n: $this\n };\n for (const key in $this.processors) {\n const processor = $this.processors[key];\n if (processor.canExecute(data)) {\n return processor.execute(data);\n }\n }\n return null;\n };\n }\n\n const data: I18NData = { translation, base, namespace, values: {}, i18n: this };\n for (const key in this.processors) {\n if (this.processors[key].canExecute(data)) {\n return this.processors[key].execute(data);\n }\n }\n return null;\n }\n\n public namespace(namespace: string): Translator {\n return base => {\n return this.translate(base as string, namespace);\n };\n }\n\n public ns(namespace: string): Translator {\n return this.namespace(namespace);\n }\n\n /**\n * Formats and outputs date.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public date(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getDateFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value;\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public time(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value;\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs date/time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string {\n if (!outputFormat) {\n outputFormat = this.getDateTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: number | Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value;\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Outputs formatted number as amount of price.\n */\n public price(value: string | number, outputFormat?: any): string {\n if (!outputFormat) {\n outputFormat = this.getPriceFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.price, outputFormat);\n }\n\n // Convert placeholders to accounting's placeholders.\n let format = outputFormat.format;\n format = format.replace(\"{symbol}\", \"%s\");\n format = format.replace(\"{amount}\", \"%v\");\n\n return accounting.formatMoney(\n value,\n outputFormat.symbol,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal,\n format\n );\n }\n\n /**\n * Outputs formatted number.\n */\n public number(value: string | number, outputFormat?: NumberFormat): string {\n if (!outputFormat) {\n outputFormat = this.getNumberFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.number, outputFormat);\n }\n return accounting.formatNumber(\n /**\n * Cast as number because method transforms it internally.\n */\n value as number,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal\n );\n }\n\n /**\n * Returns translation for given text key.\n */\n public getTranslation(key?: string): string | null {\n if (!key) {\n return null;\n }\n return this.translations[key];\n }\n\n /**\n * Returns all translations for current locale.\n */\n public getTranslations(): Translations {\n return this.translations;\n }\n\n /**\n * Returns true if given key has a translation for currently set locale.\n */\n public hasTranslation(key: string): boolean {\n return key in this.translations;\n }\n\n /**\n * Sets translation for given text key.\n */\n public setTranslation(key: string, translation: string): I18N {\n this.translations[key] = translation;\n return this;\n }\n\n /**\n * Sets translations that will be used.\n */\n public setTranslations(translations: Translations): I18N {\n this.translations = translations;\n return this;\n }\n\n /**\n * Clears all translations.\n */\n public clearTranslations(): I18N {\n this.setTranslations({});\n return this;\n }\n\n /**\n * Merges given translations object with already existing.\n */\n\n public mergeTranslations(translations: Translations): Translations {\n return lodashAssign(this.translations, translations);\n }\n\n /**\n * Returns currently selected locale (locale's key).\n */\n public getLocale(): null | string {\n return this.locale;\n }\n\n /**\n * Sets current locale.\n */\n public setLocale(locale: string): I18N {\n this.locale = locale;\n return this;\n }\n\n /**\n * Registers single modifier.\n */\n public registerModifier(modifier: Modifier): I18N {\n this.modifiers[modifier.name] = modifier;\n return this;\n }\n\n /**\n * Registers all modifiers in given array.\n */\n public registerModifiers(modifiers: Array<Modifier>): I18N {\n modifiers.forEach(modifier => this.registerModifier(modifier));\n return this;\n }\n\n /**\n * Unregisters given modifier.\n */\n public unregisterModifier(name: string): I18N {\n delete this.modifiers[name];\n return this;\n }\n\n /**\n * Registers single processor.\n */\n public registerProcessor(processor: Processor): I18N {\n this.processors[processor.name] = processor;\n return this;\n }\n\n /**\n * Registers all processors in given array.\n */\n public registerProcessors(processors: Array<Processor>): I18N {\n processors.forEach(processor => this.registerProcessor(processor));\n return this;\n }\n\n /**\n * Unregisters given processor.\n */\n public unregisterProcessor(name: string): I18N {\n delete this.processors[name];\n return this;\n }\n\n /**\n * Returns default formats\n */\n public getDefaultFormats(): Formats {\n return {\n date: \"DD/MM/YYYY\",\n time: \"HH:mm\",\n datetime: \"DD/MM/YYYY HH:mm\",\n price: {\n symbol: \"\",\n format: \"{symbol}{amount}\",\n decimal: \".\",\n thousand: \",\",\n precision: 2\n },\n number: {\n decimal: \".\",\n thousand: \",\",\n precision: 2\n }\n };\n }\n\n /**\n * Returns current format to be used when outputting dates.\n */\n public getDateFormat(): string {\n return lodashGet(this.locale, \"formats.date\", this.defaultFormats.date);\n }\n\n /**\n * Returns current format to be used when outputting time.\n */\n public getTimeFormat(): string {\n return lodashGet(this.locale, \"formats.time\", this.defaultFormats.time);\n }\n\n /**\n * Returns current format to be used when outputting date/time.\n */\n public getDateTimeFormat(): string {\n return lodashGet(this.locale, \"formats.datetime\", this.defaultFormats.datetime);\n }\n\n /**\n * Returns current format to be used when outputting prices.\n */\n public getPriceFormat(): PriceFormat {\n return lodashAssign(\n {},\n this.defaultFormats.price,\n lodashGet(this.locale, \"formats.price\", {})\n );\n }\n\n /**\n * Returns current format to be used when outputting numbers.\n */\n public getNumberFormat(): NumberFormat {\n return lodashAssign(\n {},\n this.defaultFormats.number,\n lodashGet(this.locale, \"formats.number\", {})\n );\n }\n}\n"],"mappings":";;;;;;;;;AAAA;AACA;AAKA;AACA;AACA;AANA;AACA;AACA;AACA;;AAsBA;AACA;AACA;AACe,MAAMA,IAAI,CAAC;EAWfC,WAAW,GAAG;IAAA,8CAVU,IAAI;IAAA;IAAA;IAAA;IAAA;IAW/B;AACR;AACA;AACA;IACQ,IAAI,CAACC,cAAc,GAAG,IAAI,CAACC,iBAAiB,EAAE;;IAE9C;AACR;AACA;AACA;IACQ,IAAI,CAACC,YAAY,GAAG,CAAC,CAAC;;IAEtB;AACR;AACA;AACA;IACQ,IAAI,CAACC,SAAS,GAAG,CAAC,CAAC;;IAEnB;AACR;AACA;AACA;AACA;IACQ,IAAI,CAACC,UAAU,GAAG,CAAC,CAAC;EACxB;EAEOC,SAAS,CAACC,IAAY,EAAEC,SAAkB,EAAc;IAC3D;IACA;IACA;;IAEA,IAAI,CAACA,SAAS,EAAE;MACZ,MAAMC,KAAK,CAAC,kCAAkC,CAAC;IACnD;IAEAF,IAAI,GAAG,IAAAG,YAAS,EAACH,IAAI,EAAE,OAAO,EAAEA,IAAI,CAAC;IAErC,IAAII,WAA0B,GAAG,IAAI,CAACC,cAAc,CAACJ,SAAS,GAAG,GAAG,GAAG,IAAAK,kBAAI,EAACN,IAAI,CAAC,CAAC;IAElF,IAAI,CAACI,WAAW,EAAE;MACdA,WAAW,GAAGJ,IAAI;IACtB;IAEA,MAAMO,YAAY,GAAGP,IAAI,CAACQ,QAAQ,CAAC,GAAG,CAAC,IAAIR,IAAI,CAACQ,QAAQ,CAAC,GAAG,CAAC;IAC7D,IAAID,YAAY,EAAE;MACd;MACA,MAAME,KAAK,GAAG,IAAI;MAClB,OAAO,SAASC,IAAI,CA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CAAC;EAC3E;;EAEA;AACJ;AACA;EACWG,iBAAiB,GAAW;IAC/B,OAAO,IAAA3B,YAAS,EAAC,IAAI,CAACgD,MAAM,EAAE,kBAAkB,EAAE,IAAI,CAACzD,cAAc,CAACoE,QAAQ,CAAC;EACnF;;EAEA;AACJ;AACA;EACW9B,cAAc,GAAgB;IACjC,OAAO,IAAAC,eAAY,EACf,CAAC,CAAC,EACF,IAAI,CAACvC,cAAc,CAACqC,KAAK,EACzB,IAAA5B,YAAS,EAAC,IAAI,CAACgD,MAAM,EAAE,eAAe,EAAE,CAAC,CAAC,CAAC,CAC9C;EACL;;EAEA;AACJ;AACA;EACWT,eAAe,GAAiB;IACnC,OAAO,IAAAT,eAAY,EACf,CAAC,CAAC,EACF,IAAI,CAACvC,cAAc,CAAC+C,MAAM,EAC1B,IAAAtC,YAAS,EAAC,IAAI,CAACgD,MAAM,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,CAC/C;EACL;AACJ;AAAC"}
1
+ {"version":3,"file":"I18n.js","sources":["../src/I18n.ts"],"sourcesContent":["import accounting from \"accounting\";\nimport * as fecha from \"fecha\";\nimport lodashAssign from \"lodash/assign.js\";\nimport lodashGet from \"lodash/get.js\";\n\nimport type {\n Formats,\n I18NData,\n I18NDataValues,\n Modifier,\n NumberFormat,\n PriceFormat,\n Processor,\n ProcessorResult,\n Translations,\n Translator\n} from \"./types.js\";\nimport { hash } from \"ohash\";\n\nexport type Translated =\n | ((values: I18NDataValues) => ProcessorResult | null)\n | ProcessorResult\n | null;\n/**\n * Main class used for all I18n needs.\n */\nexport default class I18N {\n public locale: string | null = null;\n public defaultFormats: Formats;\n public translations: Translations;\n public modifiers: {\n [name: string]: Modifier;\n };\n public processors: {\n [name: string]: Processor;\n };\n\n public constructor() {\n /**\n * If we fail to fetch formats for currently selected locale, these default formats will be used.\n * @type {{date: string, time: string, datetime: string, number: string}}\n */\n this.defaultFormats = this.getDefaultFormats();\n\n /**\n * All currently-loaded translations, for easier (synchronous) access.\n * @type {{}}\n */\n this.translations = {};\n\n /**\n * All registered modifiers.\n * @type {{}}\n */\n this.modifiers = {};\n\n /**\n * All registered processors.\n * Default built-in processors are registered immediately below.\n * @type {{}}\n */\n this.processors = {};\n }\n\n public translate(base: string, namespace?: string): Translated {\n // Returns full translation for given base text in given namespace (optional).\n // If translation isn't found, base text will be returned.\n // We create a key out of given namespace and base text.\n\n if (!namespace) {\n throw Error(\"I18N text namespace not defined.\");\n }\n\n base = lodashGet(base, \"raw.0\", base);\n\n let translation: string | null = this.getTranslation(namespace + \".\" + hash(base));\n\n if (!translation) {\n translation = base;\n }\n\n const hasVariables = base.includes(\"{\") && base.includes(\"}\");\n if (hasVariables) {\n // oxlint-disable-next-line typescript/no-this-alias\n const $this = this;\n return function i18n(values: I18NDataValues) {\n const data: I18NData = {\n translation: translation as string,\n base,\n namespace,\n values,\n i18n: $this\n };\n for (const key in $this.processors) {\n const processor = $this.processors[key];\n if (processor.canExecute(data)) {\n return processor.execute(data);\n }\n }\n return null;\n };\n }\n\n const data: I18NData = { translation, base, namespace, values: {}, i18n: this };\n for (const key in this.processors) {\n if (this.processors[key].canExecute(data)) {\n return this.processors[key].execute(data);\n }\n }\n return null;\n }\n\n public namespace(namespace: string): Translator {\n return base => {\n return this.translate(base as string, namespace);\n };\n }\n\n public ns(namespace: string): Translator {\n return this.namespace(namespace);\n }\n\n /**\n * Formats and outputs date.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public date(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getDateFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n public time(\n value: Date | string | number,\n outputFormat?: string,\n inputFormat?: string\n ): string {\n if (!outputFormat) {\n outputFormat = this.getTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Formats and outputs date/time.\n * It will try to load format from currently selected locale's settings. If not defined, default formats will be used.\n */\n dateTime(value: Date | string | number, outputFormat?: string, inputFormat?: string): string {\n if (!outputFormat) {\n outputFormat = this.getDateTimeFormat();\n }\n if (!inputFormat) {\n inputFormat = \"YYYY-MM-DDTHH:mm:ss.SSSZ\";\n }\n\n let parsedValue: Date;\n\n if (typeof value === \"string\") {\n parsedValue = fecha.parse(value, inputFormat) as Date;\n } else {\n parsedValue = value instanceof Date ? value : new Date(value);\n }\n\n return fecha.format(parsedValue, outputFormat);\n }\n\n /**\n * Outputs formatted number as amount of price.\n */\n public price(value: string | number, outputFormat?: any): string {\n if (!outputFormat) {\n outputFormat = this.getPriceFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.price, outputFormat);\n }\n\n // Convert placeholders to accounting's placeholders.\n let format = outputFormat.format;\n format = format.replace(\"{symbol}\", \"%s\");\n format = format.replace(\"{amount}\", \"%v\");\n\n return accounting.formatMoney(\n value,\n outputFormat.symbol,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal,\n format\n );\n }\n\n /**\n * Outputs formatted number.\n */\n public number(value: string | number, outputFormat?: NumberFormat): string {\n if (!outputFormat) {\n outputFormat = this.getNumberFormat();\n } else {\n outputFormat = lodashAssign({}, this.defaultFormats.number, outputFormat);\n }\n return accounting.formatNumber(\n /**\n * Cast as number because method transforms it internally.\n */\n value as number,\n outputFormat.precision,\n outputFormat.thousand,\n outputFormat.decimal\n );\n }\n\n /**\n * Returns translation for given text key.\n */\n public getTranslation(key?: string): string | null {\n if (!key) {\n return null;\n }\n return this.translations[key];\n }\n\n /**\n * Returns all translations for current locale.\n */\n public getTranslations(): Translations {\n return this.translations;\n }\n\n /**\n * Returns true if given key has a translation for currently set locale.\n */\n public hasTranslation(key: string): boolean {\n return key in this.translations;\n }\n\n /**\n * Sets translation for given text key.\n */\n public setTranslation(key: string, translation: string): I18N {\n this.translations[key] = translation;\n return this;\n }\n\n /**\n * Sets translations that will be used.\n */\n public setTranslations(translations: Translations): I18N {\n this.translations = translations;\n return this;\n }\n\n /**\n * Clears all translations.\n */\n public clearTranslations(): I18N {\n this.setTranslations({});\n return this;\n }\n\n /**\n * Merges given translations object with already existing.\n */\n\n public mergeTranslations(translations: Translations): Translations {\n return lodashAssign(this.translations, translations);\n }\n\n /**\n * Returns currently selected locale (locale's key).\n */\n public getLocale(): null | string {\n return this.locale;\n }\n\n /**\n * Sets current locale.\n */\n public setLocale(locale: string): I18N {\n this.locale = locale;\n return this;\n }\n\n /**\n * Registers single modifier.\n */\n public registerModifier(modifier: Modifier): I18N {\n this.modifiers[modifier.name] = modifier;\n return this;\n }\n\n /**\n * Registers all modifiers in given array.\n */\n public registerModifiers(modifiers: Array<Modifier>): I18N {\n modifiers.forEach(modifier => this.registerModifier(modifier));\n return this;\n }\n\n /**\n * Unregisters given modifier.\n */\n public unregisterModifier(name: string): I18N {\n delete this.modifiers[name];\n return this;\n }\n\n /**\n * Registers single processor.\n */\n public registerProcessor(processor: Processor): I18N {\n this.processors[processor.name] = processor;\n return this;\n }\n\n /**\n * Registers all processors in given array.\n */\n public registerProcessors(processors: Array<Processor>): I18N {\n processors.forEach(processor => this.registerProcessor(processor));\n return this;\n }\n\n /**\n * Unregisters given processor.\n */\n public unregisterProcessor(name: string): I18N {\n delete this.processors[name];\n return this;\n }\n\n /**\n * Returns default formats\n */\n public getDefaultFormats(): Formats {\n return {\n date: \"DD/MM/YYYY\",\n time: \"HH:mm\",\n datetime: \"DD/MM/YYYY HH:mm\",\n price: {\n symbol: \"\",\n format: \"{symbol}{amount}\",\n decimal: \".\",\n thousand: \",\",\n precision: 2\n },\n number: {\n decimal: \".\",\n thousand: \",\",\n precision: 2\n }\n };\n }\n\n /**\n * Returns current format to be used when outputting dates.\n */\n public getDateFormat(): string {\n return lodashGet(this.locale, \"formats.date\", this.defaultFormats.date);\n }\n\n /**\n * Returns current format to be used when outputting time.\n */\n public getTimeFormat(): string {\n return lodashGet(this.locale, \"formats.time\", this.defaultFormats.time);\n }\n\n /**\n * Returns current format to be used when outputting date/time.\n */\n public getDateTimeFormat(): string {\n return lodashGet(this.locale, \"formats.datetime\", this.defaultFormats.datetime);\n }\n\n /**\n * Returns current format to be used when outputting prices.\n */\n public getPriceFormat(): PriceFormat {\n return lodashAssign(\n {},\n this.defaultFormats.price,\n lodashGet(this.locale, \"formats.price\", {})\n );\n }\n\n /**\n * Returns current format to be used when outputting numbers.\n */\n public getNumberFormat(): NumberFormat {\n return lodashAssign(\n {},\n this.defaultFormats.number,\n lodashGet(this.locale, \"formats.number\", {})\n );\n }\n}\n"],"names":["I18N","base","namespace","Error","lodashGet","translation","hash","hasVariables","$this","values","data","key","processor","value","outputFormat","inputFormat","parsedValue","fecha","Date","lodashAssign","format","accounting","translations","locale","modifier","modifiers","name","processors"],"mappings":";;;;;AA0Be,MAAMA;IAWjB,aAAqB;aAVd,MAAM,GAAkB;QAe3B,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,iBAAiB;QAM5C,IAAI,CAAC,YAAY,GAAG,CAAC;QAMrB,IAAI,CAAC,SAAS,GAAG,CAAC;QAOlB,IAAI,CAAC,UAAU,GAAG,CAAC;IACvB;IAEO,UAAUC,IAAY,EAAEC,SAAkB,EAAc;QAK3D,IAAI,CAACA,WACD,MAAMC,MAAM;QAGhBF,OAAOG,IAAUH,MAAM,SAASA;QAEhC,IAAII,cAA6B,IAAI,CAAC,cAA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O,IAAI;IACf;IAKO,oBAAoBc,IAAY,EAAQ;QAC3C,OAAO,IAAI,CAAC,UAAU,CAACA,KAAK;QAC5B,OAAO,IAAI;IACf;IAKO,oBAA6B;QAChC,OAAO;YACH,MAAM;YACN,MAAM;YACN,UAAU;YACV,OAAO;gBACH,QAAQ;gBACR,QAAQ;gBACR,SAAS;gBACT,UAAU;gBACV,WAAW;YACf;YACA,QAAQ;gBACJ,SAAS;gBACT,UAAU;gBACV,WAAW;YACf;QACJ;IACJ;IAKO,gBAAwB;QAC3B,OAAOtB,IAAU,IAAI,CAAC,MAAM,EAAE,gBAAgB,IAAI,CAAC,cAAc,CAAC,IAAI;IAC1E;IAKO,gBAAwB;QAC3B,OAAOA,IAAU,IAAI,CAAC,MAAM,EAAE,gBAAgB,IAAI,CAAC,cAAc,CAAC,IAAI;IAC1E;IAKO,oBAA4B;QAC/B,OAAOA,IAAU,IAAI,CAAC,MAAM,EAAE,oBAAoB,IAAI,CAAC,cAAc,CAAC,QAAQ;IAClF;IAKO,iBAA8B;QACjC,OAAOe,cACH,CAAC,GACD,IAAI,CAAC,cAAc,CAAC,KAAK,EACzBf,IAAU,IAAI,CAAC,MAAM,EAAE,iBAAiB,CAAC;IAEjD;IAKO,kBAAgC;QACnC,OAAOe,cACH,CAAC,GACD,IAAI,CAAC,cAAc,CAAC,MAAM,EAC1Bf,IAAU,IAAI,CAAC,MAAM,EAAE,kBAAkB,CAAC;IAElD;AACJ"}
package/README.md CHANGED
@@ -1,17 +1,11 @@
1
1
  # @webiny/i18n
2
- [![](https://img.shields.io/npm/dw/@webiny/i18n.svg)](https://www.npmjs.com/package/@webiny/i18n)
3
- [![](https://img.shields.io/npm/v/@webiny/i18n.svg)](https://www.npmjs.com/package/@webiny/i18n)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
2
 
7
- A simple I18N library, for frontend and backend apps.
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
8
6
 
9
- ## Install
10
- ```
11
- npm install --save @webiny/i18n
12
- ```
7
+ πŸ“˜ **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
13
8
 
14
- Or if you prefer yarn:
15
- ```
16
- yarn add @webiny/i18n
17
- ```
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._