@lingui/react 4.3.0 → 4.4.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
@@ -21,7 +21,8 @@ const I18nProvider = ({
21
21
  const makeContext = React.useCallback(
22
22
  () => ({
23
23
  i18n,
24
- defaultComponent
24
+ defaultComponent,
25
+ _: i18n.t.bind(i18n)
25
26
  }),
26
27
  [i18n, defaultComponent]
27
28
  );
@@ -76,7 +77,7 @@ function formatElements(value, elements = {}) {
76
77
  if (before)
77
78
  tree.push(before);
78
79
  for (const [index, children, after] of getElements(parts)) {
79
- let element = elements[index];
80
+ let element = typeof index !== "undefined" ? elements[index] : void 0;
80
81
  if (!element || voidElementTags[element.type] && children) {
81
82
  if (!element) {
82
83
  console.error(
@@ -110,21 +111,30 @@ function getElements(parts) {
110
111
  if (!parts.length)
111
112
  return [];
112
113
  const [paired, children, unpaired, after] = parts.slice(0, 4);
113
- return [[paired || unpaired, children || "", after]].concat(
114
- getElements(parts.slice(4, parts.length))
115
- );
114
+ const triple = [paired || unpaired, children || "", after];
115
+ return [triple].concat(getElements(parts.slice(4, parts.length)));
116
116
  }
117
117
  const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
118
118
 
119
119
  function Trans(props) {
120
- const { i18n, defaultComponent } = useLingui();
121
- const { render, component, id, message, formats } = props;
120
+ const lingui = useLingui();
121
+ return React.createElement(TransNoContext, { ...props, lingui });
122
+ }
123
+ function TransNoContext(props) {
124
+ const {
125
+ render,
126
+ component,
127
+ id,
128
+ message,
129
+ formats,
130
+ lingui: { i18n, defaultComponent }
131
+ } = props;
122
132
  const values = { ...props.values };
123
133
  const components = { ...props.components };
124
134
  if (values) {
125
135
  Object.keys(values).forEach((key) => {
126
136
  const value = values[key];
127
- const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every((el) => React.isValidElement(el));
137
+ const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every(React.isValidElement);
128
138
  if (!valueIsReactEl)
129
139
  return;
130
140
  const index = Object.keys(components).length;
@@ -173,4 +183,5 @@ const RenderFragment = ({ children }) => {
173
183
  exports.I18nProvider = I18nProvider;
174
184
  exports.LinguiContext = LinguiContext;
175
185
  exports.Trans = Trans;
186
+ exports.TransNoContext = TransNoContext;
176
187
  exports.useLingui = useLingui;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { ComponentType, FunctionComponent } from 'react';
2
- import { I18n } from '@lingui/core';
2
+ import { MessageOptions, I18n } from '@lingui/core';
3
3
 
4
4
  type TransRenderProps = {
5
5
  id: string;
@@ -10,7 +10,7 @@ type TransRenderProps = {
10
10
  };
11
11
  type TransRenderCallbackOrComponent = {
12
12
  component?: undefined;
13
- render?: (props: TransRenderProps) => React.ReactElement<any, any> | null;
13
+ render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
14
14
  } | {
15
15
  component?: React.ComponentType<TransRenderProps> | null;
16
16
  render?: undefined;
@@ -22,21 +22,34 @@ type TransProps = {
22
22
  components?: {
23
23
  [key: string]: React.ElementType | any;
24
24
  };
25
- formats?: Record<string, unknown>;
25
+ formats?: MessageOptions["formats"];
26
26
  comment?: string;
27
27
  children?: React.ReactNode;
28
28
  } & TransRenderCallbackOrComponent;
29
29
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
30
+ /**
31
+ * Version of `<Trans>` component without using a Provider/Context React feature.
32
+ * Primarily made for support React Server Components (RSC)
33
+ *
34
+ * @experimental the api of this component is not stabilized yet.
35
+ */
36
+ declare function TransNoContext(props: TransProps & {
37
+ lingui: {
38
+ i18n: I18n;
39
+ defaultComponent?: ComponentType<TransRenderProps>;
40
+ };
41
+ }): React.ReactElement<any, any> | null;
30
42
 
31
43
  type I18nContext = {
32
44
  i18n: I18n;
45
+ _: I18n["_"];
33
46
  defaultComponent?: ComponentType<TransRenderProps>;
34
47
  };
35
- type I18nProviderProps = I18nContext & {
48
+ type I18nProviderProps = Omit<I18nContext, "_"> & {
36
49
  children?: React.ReactNode;
37
50
  };
38
- declare const LinguiContext: React.Context<I18nContext>;
51
+ declare const LinguiContext: React.Context<I18nContext | null>;
39
52
  declare function useLingui(): I18nContext;
40
53
  declare const I18nProvider: FunctionComponent<I18nProviderProps>;
41
54
 
42
- export { I18nContext, I18nProvider, I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderCallbackOrComponent, TransRenderProps, useLingui };
55
+ export { I18nContext, I18nProvider, I18nProviderProps, LinguiContext, Trans, TransNoContext, TransProps, TransRenderCallbackOrComponent, TransRenderProps, useLingui };
package/dist/index.mjs CHANGED
@@ -19,7 +19,8 @@ const I18nProvider = ({
19
19
  const makeContext = React.useCallback(
20
20
  () => ({
21
21
  i18n,
22
- defaultComponent
22
+ defaultComponent,
23
+ _: i18n.t.bind(i18n)
23
24
  }),
24
25
  [i18n, defaultComponent]
25
26
  );
@@ -74,7 +75,7 @@ function formatElements(value, elements = {}) {
74
75
  if (before)
75
76
  tree.push(before);
76
77
  for (const [index, children, after] of getElements(parts)) {
77
- let element = elements[index];
78
+ let element = typeof index !== "undefined" ? elements[index] : void 0;
78
79
  if (!element || voidElementTags[element.type] && children) {
79
80
  if (!element) {
80
81
  console.error(
@@ -108,21 +109,30 @@ function getElements(parts) {
108
109
  if (!parts.length)
109
110
  return [];
110
111
  const [paired, children, unpaired, after] = parts.slice(0, 4);
111
- return [[paired || unpaired, children || "", after]].concat(
112
- getElements(parts.slice(4, parts.length))
113
- );
112
+ const triple = [paired || unpaired, children || "", after];
113
+ return [triple].concat(getElements(parts.slice(4, parts.length)));
114
114
  }
115
115
  const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
116
116
 
117
117
  function Trans(props) {
118
- const { i18n, defaultComponent } = useLingui();
119
- const { render, component, id, message, formats } = props;
118
+ const lingui = useLingui();
119
+ return React.createElement(TransNoContext, { ...props, lingui });
120
+ }
121
+ function TransNoContext(props) {
122
+ const {
123
+ render,
124
+ component,
125
+ id,
126
+ message,
127
+ formats,
128
+ lingui: { i18n, defaultComponent }
129
+ } = props;
120
130
  const values = { ...props.values };
121
131
  const components = { ...props.components };
122
132
  if (values) {
123
133
  Object.keys(values).forEach((key) => {
124
134
  const value = values[key];
125
- const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every((el) => React.isValidElement(el));
135
+ const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every(React.isValidElement);
126
136
  if (!valueIsReactEl)
127
137
  return;
128
138
  const index = Object.keys(components).length;
@@ -168,4 +178,4 @@ const RenderFragment = ({ children }) => {
168
178
  return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
169
179
  };
170
180
 
171
- export { I18nProvider, LinguiContext, Trans, useLingui };
181
+ export { I18nProvider, LinguiContext, Trans, TransNoContext, useLingui };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/react",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "sideEffects": false,
5
5
  "description": "React components for translations",
6
6
  "main": "./dist/index.cjs",
@@ -63,7 +63,7 @@
63
63
  },
64
64
  "dependencies": {
65
65
  "@babel/runtime": "^7.20.13",
66
- "@lingui/core": "4.3.0"
66
+ "@lingui/core": "4.4.0"
67
67
  },
68
68
  "devDependencies": {
69
69
  "@lingui/jest-mocks": "*",
@@ -75,5 +75,5 @@
75
75
  "react-dom": "^18.2.0",
76
76
  "unbuild": "^1.1.2"
77
77
  },
78
- "gitHead": "da6c52629e978c24ec83f2a6c22e4dfc910808b4"
78
+ "gitHead": "c36c0340ced1c4b76d5e1c340785932122449ac9"
79
79
  }