@lingui/core 4.0.0-next.8 → 4.1.0

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/dist/index.cjs CHANGED
@@ -10,7 +10,7 @@ function normalizeLocales(locales) {
10
10
  const out = Array.isArray(locales) ? locales : [locales];
11
11
  return [...out, "en"];
12
12
  }
13
- function date(locales, value, format = {}) {
13
+ function date(locales, value, format) {
14
14
  const _locales = normalizeLocales(locales);
15
15
  const formatter = getMemoized(
16
16
  () => cacheKey("date", _locales, format),
@@ -18,7 +18,7 @@ function date(locales, value, format = {}) {
18
18
  );
19
19
  return formatter.format(isString(value) ? new Date(value) : value);
20
20
  }
21
- function number(locales, value, format = {}) {
21
+ function number(locales, value, format) {
22
22
  const _locales = normalizeLocales(locales);
23
23
  const formatter = getMemoized(
24
24
  () => cacheKey("number", _locales, format),
@@ -29,10 +29,10 @@ function number(locales, value, format = {}) {
29
29
  function plural(locales, ordinal, value, { offset = 0, ...rules }) {
30
30
  const _locales = normalizeLocales(locales);
31
31
  const plurals = ordinal ? getMemoized(
32
- () => cacheKey("plural-ordinal", _locales, {}),
32
+ () => cacheKey("plural-ordinal", _locales),
33
33
  () => new Intl.PluralRules(_locales, { type: "ordinal" })
34
34
  ) : getMemoized(
35
- () => cacheKey("plural-cardinal", _locales, {}),
35
+ () => cacheKey("plural-cardinal", _locales),
36
36
  () => new Intl.PluralRules(_locales, { type: "cardinal" })
37
37
  );
38
38
  return rules[value] ?? rules[plurals.select(value - offset)] ?? rules.other;
@@ -46,8 +46,8 @@ function getMemoized(getKey, construct) {
46
46
  }
47
47
  return formatter;
48
48
  }
49
- function cacheKey(type, locales, options = {}) {
50
- const localeKey = [...locales].sort().join("-");
49
+ function cacheKey(type, locales, options) {
50
+ const localeKey = locales.join("-");
51
51
  return `${type}-${localeKey}-${JSON.stringify(options)}`;
52
52
  }
53
53
 
@@ -216,25 +216,13 @@ class I18n extends EventEmitter {
216
216
  this.emit("change");
217
217
  }
218
218
  /**
219
- * @param locales one locale or array of locales.
220
- * If array of locales is passed they would be used as fallback
221
- * locales for date and number formatting
222
- * @param messages compiled message catalog
223
- * @param notify Should emit `change` event for all subscribers.
224
- * This is useful for integration with frameworks as NextJS to avoid race-conditions during initialization.
219
+ * @param options {@link LoadAndActivateOptions}
225
220
  */
226
- loadAndActivate(locales, messages, notify = true) {
227
- if (Array.isArray(locales)) {
228
- this._locale = locales[0];
229
- this._locales = locales;
230
- } else {
231
- this._locale = locales;
232
- this._locales = null;
233
- }
221
+ loadAndActivate({ locale, locales, messages }) {
222
+ this._locale = locale;
223
+ this._locales = locales || void 0;
234
224
  this._messages[this._locale] = messages;
235
- if (notify) {
236
- this.emit("change");
237
- }
225
+ this.emit("change");
238
226
  }
239
227
  activate(locale, locales) {
240
228
  if (process.env.NODE_ENV !== "production") {
package/dist/index.d.ts CHANGED
@@ -57,6 +57,14 @@ type Events = {
57
57
  change: () => void;
58
58
  missing: (event: MissingMessageEvent) => void;
59
59
  };
60
+ type LoadAndActivateOptions = {
61
+ /** initial active locale */
62
+ locale: Locale;
63
+ /** list of alternative locales (BCP 47 language tags) which are used for number and date formatting */
64
+ locales?: Locales;
65
+ /** compiled message catalog */
66
+ messages: Messages;
67
+ };
60
68
  declare class I18n extends EventEmitter<Events> {
61
69
  private _locale;
62
70
  private _locales;
@@ -84,14 +92,9 @@ declare class I18n extends EventEmitter<Events> {
84
92
  load(allMessages: AllMessages): void;
85
93
  load(locale: Locale, messages: Messages): void;
86
94
  /**
87
- * @param locales one locale or array of locales.
88
- * If array of locales is passed they would be used as fallback
89
- * locales for date and number formatting
90
- * @param messages compiled message catalog
91
- * @param notify Should emit `change` event for all subscribers.
92
- * This is useful for integration with frameworks as NextJS to avoid race-conditions during initialization.
95
+ * @param options {@link LoadAndActivateOptions}
93
96
  */
94
- loadAndActivate(locales: Locale | Locales, messages: Messages, notify?: boolean): void;
97
+ loadAndActivate({ locale, locales, messages }: LoadAndActivateOptions): void;
95
98
  activate(locale: Locale, locales?: Locales): void;
96
99
  _(descriptor: MessageDescriptor): string;
97
100
  _(id: string, values?: Values, options?: MessageOptions): string;
package/dist/index.mjs CHANGED
@@ -8,7 +8,7 @@ function normalizeLocales(locales) {
8
8
  const out = Array.isArray(locales) ? locales : [locales];
9
9
  return [...out, "en"];
10
10
  }
11
- function date(locales, value, format = {}) {
11
+ function date(locales, value, format) {
12
12
  const _locales = normalizeLocales(locales);
13
13
  const formatter = getMemoized(
14
14
  () => cacheKey("date", _locales, format),
@@ -16,7 +16,7 @@ function date(locales, value, format = {}) {
16
16
  );
17
17
  return formatter.format(isString(value) ? new Date(value) : value);
18
18
  }
19
- function number(locales, value, format = {}) {
19
+ function number(locales, value, format) {
20
20
  const _locales = normalizeLocales(locales);
21
21
  const formatter = getMemoized(
22
22
  () => cacheKey("number", _locales, format),
@@ -27,10 +27,10 @@ function number(locales, value, format = {}) {
27
27
  function plural(locales, ordinal, value, { offset = 0, ...rules }) {
28
28
  const _locales = normalizeLocales(locales);
29
29
  const plurals = ordinal ? getMemoized(
30
- () => cacheKey("plural-ordinal", _locales, {}),
30
+ () => cacheKey("plural-ordinal", _locales),
31
31
  () => new Intl.PluralRules(_locales, { type: "ordinal" })
32
32
  ) : getMemoized(
33
- () => cacheKey("plural-cardinal", _locales, {}),
33
+ () => cacheKey("plural-cardinal", _locales),
34
34
  () => new Intl.PluralRules(_locales, { type: "cardinal" })
35
35
  );
36
36
  return rules[value] ?? rules[plurals.select(value - offset)] ?? rules.other;
@@ -44,8 +44,8 @@ function getMemoized(getKey, construct) {
44
44
  }
45
45
  return formatter;
46
46
  }
47
- function cacheKey(type, locales, options = {}) {
48
- const localeKey = [...locales].sort().join("-");
47
+ function cacheKey(type, locales, options) {
48
+ const localeKey = locales.join("-");
49
49
  return `${type}-${localeKey}-${JSON.stringify(options)}`;
50
50
  }
51
51
 
@@ -214,25 +214,13 @@ class I18n extends EventEmitter {
214
214
  this.emit("change");
215
215
  }
216
216
  /**
217
- * @param locales one locale or array of locales.
218
- * If array of locales is passed they would be used as fallback
219
- * locales for date and number formatting
220
- * @param messages compiled message catalog
221
- * @param notify Should emit `change` event for all subscribers.
222
- * This is useful for integration with frameworks as NextJS to avoid race-conditions during initialization.
217
+ * @param options {@link LoadAndActivateOptions}
223
218
  */
224
- loadAndActivate(locales, messages, notify = true) {
225
- if (Array.isArray(locales)) {
226
- this._locale = locales[0];
227
- this._locales = locales;
228
- } else {
229
- this._locale = locales;
230
- this._locales = null;
231
- }
219
+ loadAndActivate({ locale, locales, messages }) {
220
+ this._locale = locale;
221
+ this._locales = locales || void 0;
232
222
  this._messages[this._locale] = messages;
233
- if (notify) {
234
- this.emit("change");
235
- }
223
+ this.emit("change");
236
224
  }
237
225
  activate(locale, locales) {
238
226
  if (process.env.NODE_ENV !== "production") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/core",
3
- "version": "4.0.0-next.8",
3
+ "version": "4.1.0",
4
4
  "sideEffects": false,
5
5
  "description": "I18n tools for javascript",
6
6
  "main": "./dist/index.cjs",
@@ -53,11 +53,11 @@
53
53
  ],
54
54
  "dependencies": {
55
55
  "@babel/runtime": "^7.20.13",
56
- "@lingui/message-utils": "^4.0.0-next.8"
56
+ "@lingui/message-utils": "4.1.0"
57
57
  },
58
58
  "devDependencies": {
59
- "@lingui/jest-mocks": "^3.0.3",
59
+ "@lingui/jest-mocks": "*",
60
60
  "unbuild": "^1.1.2"
61
61
  },
62
- "gitHead": "c9104b3227327030db3f1c684acba65ea2c0d03f"
62
+ "gitHead": "471813c5de9ade3acccf647e18922b570a804577"
63
63
  }