@lingui/react 4.4.1 → 4.5.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
@@ -1,11 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  const React = require('react');
4
- const shim = require('use-sync-external-store/shim');
4
+ const server = require('./server.cjs');
5
5
 
6
- const LinguiContext = React.createContext(null);
6
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
+
8
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
9
+
10
+ const LinguiContext = React__default.createContext(null);
7
11
  function useLingui() {
8
- const context = React.useContext(LinguiContext);
12
+ const context = React__default.useContext(LinguiContext);
9
13
  if (process.env.NODE_ENV !== "production") {
10
14
  if (context == null) {
11
15
  throw new Error("useLingui hook was used without I18nProvider.");
@@ -18,7 +22,8 @@ const I18nProvider = ({
18
22
  defaultComponent,
19
23
  children
20
24
  }) => {
21
- const makeContext = React.useCallback(
25
+ const latestKnownLocale = React__default.useRef(i18n.locale);
26
+ const makeContext = React__default.useCallback(
22
27
  () => ({
23
28
  i18n,
24
29
  defaultComponent,
@@ -26,170 +31,30 @@ const I18nProvider = ({
26
31
  }),
27
32
  [i18n, defaultComponent]
28
33
  );
29
- const context = React.useRef(makeContext());
30
- const subscribe = React.useCallback(
31
- (onStoreChange) => {
32
- const renderWithFreshContext = () => {
33
- context.current = makeContext();
34
- onStoreChange();
35
- };
36
- const propsChanged = context.current.i18n !== i18n || context.current.defaultComponent !== defaultComponent;
37
- if (propsChanged) {
38
- renderWithFreshContext();
39
- }
40
- return i18n.on("change", renderWithFreshContext);
41
- },
42
- [makeContext, i18n, defaultComponent]
43
- );
44
- const getSnapshot = React.useCallback(() => {
45
- return context.current;
46
- }, []);
47
- const contextObject = shim.useSyncExternalStore(
48
- subscribe,
49
- getSnapshot,
50
- getSnapshot
51
- );
52
- if (!contextObject.i18n.locale) {
34
+ const [context, setContext] = React__default.useState(makeContext());
35
+ React__default.useEffect(() => {
36
+ const updateContext = () => {
37
+ latestKnownLocale.current = i18n.locale;
38
+ setContext(makeContext());
39
+ };
40
+ const unsubscribe = i18n.on("change", updateContext);
41
+ if (latestKnownLocale.current !== i18n.locale) {
42
+ updateContext();
43
+ }
44
+ return unsubscribe;
45
+ }, [i18n, makeContext]);
46
+ if (!latestKnownLocale.current) {
53
47
  process.env.NODE_ENV === "development" && console.log(
54
48
  "I18nProvider rendered `null`. A call to `i18n.activate` needs to happen in order for translations to be activated and for the I18nProvider to render.This is not an error but an informational message logged only in development."
55
49
  );
56
50
  return null;
57
51
  }
58
- return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: contextObject }, children);
59
- };
60
-
61
- const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
62
- const nlRe = /(?:\r\n|\r|\n)/g;
63
- const voidElementTags = {
64
- area: true,
65
- base: true,
66
- br: true,
67
- col: true,
68
- embed: true,
69
- hr: true,
70
- img: true,
71
- input: true,
72
- keygen: true,
73
- link: true,
74
- meta: true,
75
- param: true,
76
- source: true,
77
- track: true,
78
- wbr: true,
79
- menuitem: true
80
- };
81
- function formatElements(value, elements = {}) {
82
- const uniqueId = makeCounter(0, "$lingui$");
83
- const parts = value.replace(nlRe, "").split(tagRe);
84
- if (parts.length === 1)
85
- return value;
86
- const tree = [];
87
- const before = parts.shift();
88
- if (before)
89
- tree.push(before);
90
- for (const [index, children, after] of getElements(parts)) {
91
- let element = typeof index !== "undefined" ? elements[index] : void 0;
92
- if (!element || voidElementTags[element.type] && children) {
93
- if (!element) {
94
- console.error(
95
- `Can't use element at index '${index}' as it is not declared in the original translation`
96
- );
97
- } else {
98
- console.error(
99
- `${element.type} is a void element tag therefore it must have no children`
100
- );
101
- }
102
- element = React.createElement(React.Fragment);
103
- }
104
- if (Array.isArray(element)) {
105
- element = React.createElement(React.Fragment, {}, element);
106
- }
107
- tree.push(
108
- React.cloneElement(
109
- element,
110
- { key: uniqueId() },
111
- // format children for pair tags
112
- // unpaired tags might have children if it's a component passed as a variable
113
- children ? formatElements(children, elements) : element.props.children
114
- )
115
- );
116
- if (after)
117
- tree.push(after);
118
- }
119
- return tree;
120
- }
121
- function getElements(parts) {
122
- if (!parts.length)
123
- return [];
124
- const [paired, children, unpaired, after] = parts.slice(0, 4);
125
- const triple = [paired || unpaired, children || "", after];
126
- return [triple].concat(getElements(parts.slice(4, parts.length)));
127
- }
128
- const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
129
-
130
- function TransNoContext(props) {
131
- const {
132
- render,
133
- component,
134
- id,
135
- message,
136
- formats,
137
- lingui: { i18n, defaultComponent }
138
- } = props;
139
- const values = { ...props.values };
140
- const components = { ...props.components };
141
- if (values) {
142
- Object.keys(values).forEach((key) => {
143
- const value = values[key];
144
- const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every(React.isValidElement);
145
- if (!valueIsReactEl)
146
- return;
147
- const index = Object.keys(components).length;
148
- components[index] = value;
149
- values[key] = `<${index}/>`;
150
- });
151
- }
152
- const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, { message, formats }) : id;
153
- const translation = _translation ? formatElements(_translation, components) : null;
154
- if (render === null || component === null) {
155
- return translation;
156
- }
157
- const FallbackComponent = defaultComponent || RenderFragment;
158
- const i18nProps = {
159
- id,
160
- message,
161
- translation,
162
- isTranslated: id !== translation && message !== translation,
163
- children: translation
164
- // for type-compatibility with `component` prop
165
- };
166
- if (render && component) {
167
- console.error(
168
- "You can't use both `component` and `render` prop at the same time. `component` is ignored."
169
- );
170
- } else if (render && typeof render !== "function") {
171
- console.error(
172
- `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`
173
- );
174
- } else if (component && typeof component !== "function") {
175
- console.error(
176
- `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
177
- );
178
- return React.createElement(FallbackComponent, i18nProps, translation);
179
- }
180
- if (typeof render === "function") {
181
- return render(i18nProps);
182
- }
183
- const Component = component || FallbackComponent;
184
- return React.createElement(Component, i18nProps, translation);
185
- }
186
- const RenderFragment = ({ children }) => {
187
- return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
52
+ return /* @__PURE__ */ React__default.createElement(LinguiContext.Provider, { value: context }, children);
188
53
  };
189
54
 
190
55
  function Trans(props) {
191
56
  const lingui = useLingui();
192
- return React.createElement(TransNoContext, { ...props, lingui });
57
+ return React__default.createElement(server.TransNoContext, { ...props, lingui });
193
58
  }
194
59
 
195
60
  exports.I18nProvider = I18nProvider;
@@ -0,0 +1,20 @@
1
+ import React, { ComponentType, FunctionComponent } from 'react';
2
+ import { I18n } from '@lingui/core';
3
+ import { TransRenderProps, TransProps } from './server.cjs';
4
+ export { TransRenderCallbackOrComponent } from './server.cjs';
5
+
6
+ type I18nContext = {
7
+ i18n: I18n;
8
+ _: I18n["_"];
9
+ defaultComponent?: ComponentType<TransRenderProps>;
10
+ };
11
+ type I18nProviderProps = Omit<I18nContext, "_"> & {
12
+ children?: React.ReactNode;
13
+ };
14
+ declare const LinguiContext: React.Context<I18nContext | null>;
15
+ declare function useLingui(): I18nContext;
16
+ declare const I18nProvider: FunctionComponent<I18nProviderProps>;
17
+
18
+ declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
19
+
20
+ export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
@@ -0,0 +1,20 @@
1
+ import React, { ComponentType, FunctionComponent } from 'react';
2
+ import { I18n } from '@lingui/core';
3
+ import { TransRenderProps, TransProps } from './server.mjs';
4
+ export { TransRenderCallbackOrComponent } from './server.mjs';
5
+
6
+ type I18nContext = {
7
+ i18n: I18n;
8
+ _: I18n["_"];
9
+ defaultComponent?: ComponentType<TransRenderProps>;
10
+ };
11
+ type I18nProviderProps = Omit<I18nContext, "_"> & {
12
+ children?: React.ReactNode;
13
+ };
14
+ declare const LinguiContext: React.Context<I18nContext | null>;
15
+ declare function useLingui(): I18nContext;
16
+ declare const I18nProvider: FunctionComponent<I18nProviderProps>;
17
+
18
+ declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
19
+
20
+ export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
package/dist/index.d.ts CHANGED
@@ -1,31 +1,7 @@
1
1
  import React, { ComponentType, FunctionComponent } from 'react';
2
- import { MessageOptions, I18n } from '@lingui/core';
3
-
4
- type TransRenderProps = {
5
- id: string;
6
- translation: React.ReactNode;
7
- children: React.ReactNode;
8
- message?: string | null;
9
- isTranslated: boolean;
10
- };
11
- type TransRenderCallbackOrComponent = {
12
- component?: undefined;
13
- render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
14
- } | {
15
- component?: React.ComponentType<TransRenderProps> | null;
16
- render?: undefined;
17
- };
18
- type TransProps = {
19
- id: string;
20
- message?: string;
21
- values?: Record<string, unknown>;
22
- components?: {
23
- [key: string]: React.ElementType | any;
24
- };
25
- formats?: MessageOptions["formats"];
26
- comment?: string;
27
- children?: React.ReactNode;
28
- } & TransRenderCallbackOrComponent;
2
+ import { I18n } from '@lingui/core';
3
+ import { TransRenderProps, TransProps } from './server.js';
4
+ export { TransRenderCallbackOrComponent } from './server.js';
29
5
 
30
6
  type I18nContext = {
31
7
  i18n: I18n;
@@ -41,4 +17,4 @@ declare const I18nProvider: FunctionComponent<I18nProviderProps>;
41
17
 
42
18
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
43
19
 
44
- export { I18nContext, I18nProvider, I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderCallbackOrComponent, TransRenderProps, useLingui };
20
+ export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import React, { useCallback, useRef } from 'react';
2
- import { useSyncExternalStore } from 'use-sync-external-store/shim';
1
+ import React from 'react';
2
+ import { TransNoContext } from './server.mjs';
3
3
 
4
4
  const LinguiContext = React.createContext(null);
5
5
  function useLingui() {
@@ -16,7 +16,8 @@ const I18nProvider = ({
16
16
  defaultComponent,
17
17
  children
18
18
  }) => {
19
- const makeContext = useCallback(
19
+ const latestKnownLocale = React.useRef(i18n.locale);
20
+ const makeContext = React.useCallback(
20
21
  () => ({
21
22
  i18n,
22
23
  defaultComponent,
@@ -24,165 +25,25 @@ const I18nProvider = ({
24
25
  }),
25
26
  [i18n, defaultComponent]
26
27
  );
27
- const context = useRef(makeContext());
28
- const subscribe = useCallback(
29
- (onStoreChange) => {
30
- const renderWithFreshContext = () => {
31
- context.current = makeContext();
32
- onStoreChange();
33
- };
34
- const propsChanged = context.current.i18n !== i18n || context.current.defaultComponent !== defaultComponent;
35
- if (propsChanged) {
36
- renderWithFreshContext();
37
- }
38
- return i18n.on("change", renderWithFreshContext);
39
- },
40
- [makeContext, i18n, defaultComponent]
41
- );
42
- const getSnapshot = useCallback(() => {
43
- return context.current;
44
- }, []);
45
- const contextObject = useSyncExternalStore(
46
- subscribe,
47
- getSnapshot,
48
- getSnapshot
49
- );
50
- if (!contextObject.i18n.locale) {
28
+ const [context, setContext] = React.useState(makeContext());
29
+ React.useEffect(() => {
30
+ const updateContext = () => {
31
+ latestKnownLocale.current = i18n.locale;
32
+ setContext(makeContext());
33
+ };
34
+ const unsubscribe = i18n.on("change", updateContext);
35
+ if (latestKnownLocale.current !== i18n.locale) {
36
+ updateContext();
37
+ }
38
+ return unsubscribe;
39
+ }, [i18n, makeContext]);
40
+ if (!latestKnownLocale.current) {
51
41
  process.env.NODE_ENV === "development" && console.log(
52
42
  "I18nProvider rendered `null`. A call to `i18n.activate` needs to happen in order for translations to be activated and for the I18nProvider to render.This is not an error but an informational message logged only in development."
53
43
  );
54
44
  return null;
55
45
  }
56
- return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: contextObject }, children);
57
- };
58
-
59
- const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
60
- const nlRe = /(?:\r\n|\r|\n)/g;
61
- const voidElementTags = {
62
- area: true,
63
- base: true,
64
- br: true,
65
- col: true,
66
- embed: true,
67
- hr: true,
68
- img: true,
69
- input: true,
70
- keygen: true,
71
- link: true,
72
- meta: true,
73
- param: true,
74
- source: true,
75
- track: true,
76
- wbr: true,
77
- menuitem: true
78
- };
79
- function formatElements(value, elements = {}) {
80
- const uniqueId = makeCounter(0, "$lingui$");
81
- const parts = value.replace(nlRe, "").split(tagRe);
82
- if (parts.length === 1)
83
- return value;
84
- const tree = [];
85
- const before = parts.shift();
86
- if (before)
87
- tree.push(before);
88
- for (const [index, children, after] of getElements(parts)) {
89
- let element = typeof index !== "undefined" ? elements[index] : void 0;
90
- if (!element || voidElementTags[element.type] && children) {
91
- if (!element) {
92
- console.error(
93
- `Can't use element at index '${index}' as it is not declared in the original translation`
94
- );
95
- } else {
96
- console.error(
97
- `${element.type} is a void element tag therefore it must have no children`
98
- );
99
- }
100
- element = React.createElement(React.Fragment);
101
- }
102
- if (Array.isArray(element)) {
103
- element = React.createElement(React.Fragment, {}, element);
104
- }
105
- tree.push(
106
- React.cloneElement(
107
- element,
108
- { key: uniqueId() },
109
- // format children for pair tags
110
- // unpaired tags might have children if it's a component passed as a variable
111
- children ? formatElements(children, elements) : element.props.children
112
- )
113
- );
114
- if (after)
115
- tree.push(after);
116
- }
117
- return tree;
118
- }
119
- function getElements(parts) {
120
- if (!parts.length)
121
- return [];
122
- const [paired, children, unpaired, after] = parts.slice(0, 4);
123
- const triple = [paired || unpaired, children || "", after];
124
- return [triple].concat(getElements(parts.slice(4, parts.length)));
125
- }
126
- const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
127
-
128
- function TransNoContext(props) {
129
- const {
130
- render,
131
- component,
132
- id,
133
- message,
134
- formats,
135
- lingui: { i18n, defaultComponent }
136
- } = props;
137
- const values = { ...props.values };
138
- const components = { ...props.components };
139
- if (values) {
140
- Object.keys(values).forEach((key) => {
141
- const value = values[key];
142
- const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every(React.isValidElement);
143
- if (!valueIsReactEl)
144
- return;
145
- const index = Object.keys(components).length;
146
- components[index] = value;
147
- values[key] = `<${index}/>`;
148
- });
149
- }
150
- const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, { message, formats }) : id;
151
- const translation = _translation ? formatElements(_translation, components) : null;
152
- if (render === null || component === null) {
153
- return translation;
154
- }
155
- const FallbackComponent = defaultComponent || RenderFragment;
156
- const i18nProps = {
157
- id,
158
- message,
159
- translation,
160
- isTranslated: id !== translation && message !== translation,
161
- children: translation
162
- // for type-compatibility with `component` prop
163
- };
164
- if (render && component) {
165
- console.error(
166
- "You can't use both `component` and `render` prop at the same time. `component` is ignored."
167
- );
168
- } else if (render && typeof render !== "function") {
169
- console.error(
170
- `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`
171
- );
172
- } else if (component && typeof component !== "function") {
173
- console.error(
174
- `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
175
- );
176
- return React.createElement(FallbackComponent, i18nProps, translation);
177
- }
178
- if (typeof render === "function") {
179
- return render(i18nProps);
180
- }
181
- const Component = component || FallbackComponent;
182
- return React.createElement(Component, i18nProps, translation);
183
- }
184
- const RenderFragment = ({ children }) => {
185
- return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
46
+ return /* @__PURE__ */ React.createElement(LinguiContext.Provider, { value: context }, children);
186
47
  };
187
48
 
188
49
  function Trans(props) {
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ const React = require('react');
4
+
5
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
+
7
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
8
+
9
+ const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
10
+ const nlRe = /(?:\r\n|\r|\n)/g;
11
+ const voidElementTags = {
12
+ area: true,
13
+ base: true,
14
+ br: true,
15
+ col: true,
16
+ embed: true,
17
+ hr: true,
18
+ img: true,
19
+ input: true,
20
+ keygen: true,
21
+ link: true,
22
+ meta: true,
23
+ param: true,
24
+ source: true,
25
+ track: true,
26
+ wbr: true,
27
+ menuitem: true
28
+ };
29
+ function formatElements(value, elements = {}) {
30
+ const uniqueId = makeCounter(0, "$lingui$");
31
+ const parts = value.replace(nlRe, "").split(tagRe);
32
+ if (parts.length === 1)
33
+ return value;
34
+ const tree = [];
35
+ const before = parts.shift();
36
+ if (before)
37
+ tree.push(before);
38
+ for (const [index, children, after] of getElements(parts)) {
39
+ let element = typeof index !== "undefined" ? elements[index] : void 0;
40
+ if (!element || voidElementTags[element.type] && children) {
41
+ if (!element) {
42
+ console.error(
43
+ `Can't use element at index '${index}' as it is not declared in the original translation`
44
+ );
45
+ } else {
46
+ console.error(
47
+ `${element.type} is a void element tag therefore it must have no children`
48
+ );
49
+ }
50
+ element = React__default.createElement(React__default.Fragment);
51
+ }
52
+ if (Array.isArray(element)) {
53
+ element = React__default.createElement(React__default.Fragment, {}, element);
54
+ }
55
+ tree.push(
56
+ React__default.cloneElement(
57
+ element,
58
+ { key: uniqueId() },
59
+ // format children for pair tags
60
+ // unpaired tags might have children if it's a component passed as a variable
61
+ children ? formatElements(children, elements) : element.props.children
62
+ )
63
+ );
64
+ if (after)
65
+ tree.push(after);
66
+ }
67
+ return tree;
68
+ }
69
+ function getElements(parts) {
70
+ if (!parts.length)
71
+ return [];
72
+ const [paired, children, unpaired, after] = parts.slice(0, 4);
73
+ const triple = [paired || unpaired, children || "", after];
74
+ return [triple].concat(getElements(parts.slice(4, parts.length)));
75
+ }
76
+ const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
77
+
78
+ function TransNoContext(props) {
79
+ const {
80
+ render,
81
+ component,
82
+ id,
83
+ message,
84
+ formats,
85
+ lingui: { i18n, defaultComponent }
86
+ } = props;
87
+ const values = { ...props.values };
88
+ const components = { ...props.components };
89
+ if (values) {
90
+ Object.keys(values).forEach((key) => {
91
+ const value = values[key];
92
+ const valueIsReactEl = React__default.isValidElement(value) || Array.isArray(value) && value.every(React__default.isValidElement);
93
+ if (!valueIsReactEl)
94
+ return;
95
+ const index = Object.keys(components).length;
96
+ components[index] = value;
97
+ values[key] = `<${index}/>`;
98
+ });
99
+ }
100
+ const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, { message, formats }) : id;
101
+ const translation = _translation ? formatElements(_translation, components) : null;
102
+ if (render === null || component === null) {
103
+ return translation;
104
+ }
105
+ const FallbackComponent = defaultComponent || RenderFragment;
106
+ const i18nProps = {
107
+ id,
108
+ message,
109
+ translation,
110
+ isTranslated: id !== translation && message !== translation,
111
+ children: translation
112
+ // for type-compatibility with `component` prop
113
+ };
114
+ if (render && component) {
115
+ console.error(
116
+ "You can't use both `component` and `render` prop at the same time. `component` is ignored."
117
+ );
118
+ } else if (render && typeof render !== "function") {
119
+ console.error(
120
+ `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`
121
+ );
122
+ } else if (component && typeof component !== "function") {
123
+ console.error(
124
+ `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
125
+ );
126
+ return React__default.createElement(FallbackComponent, i18nProps, translation);
127
+ }
128
+ if (typeof render === "function") {
129
+ return render(i18nProps);
130
+ }
131
+ const Component = component || FallbackComponent;
132
+ return React__default.createElement(Component, i18nProps, translation);
133
+ }
134
+ const RenderFragment = ({ children }) => {
135
+ return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, children);
136
+ };
137
+
138
+ exports.TransNoContext = TransNoContext;
@@ -0,0 +1,42 @@
1
+ import React, { ComponentType } from 'react';
2
+ import { MessageOptions, I18n } from '@lingui/core';
3
+
4
+ type TransRenderProps = {
5
+ id: string;
6
+ translation: React.ReactNode;
7
+ children: React.ReactNode;
8
+ message?: string | null;
9
+ isTranslated: boolean;
10
+ };
11
+ type TransRenderCallbackOrComponent = {
12
+ component?: undefined;
13
+ render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
14
+ } | {
15
+ component?: React.ComponentType<TransRenderProps> | null;
16
+ render?: undefined;
17
+ };
18
+ type TransProps = {
19
+ id: string;
20
+ message?: string;
21
+ values?: Record<string, unknown>;
22
+ components?: {
23
+ [key: string]: React.ElementType | any;
24
+ };
25
+ formats?: MessageOptions["formats"];
26
+ comment?: string;
27
+ children?: React.ReactNode;
28
+ } & TransRenderCallbackOrComponent;
29
+ /**
30
+ * Version of `<Trans>` component without using a Provider/Context React feature.
31
+ * Primarily made for support React Server Components (RSC)
32
+ *
33
+ * @experimental the api of this component is not stabilized yet.
34
+ */
35
+ declare function TransNoContext(props: TransProps & {
36
+ lingui: {
37
+ i18n: I18n;
38
+ defaultComponent?: ComponentType<TransRenderProps>;
39
+ };
40
+ }): React.ReactElement<any, any> | null;
41
+
42
+ export { TransNoContext, type TransProps, type TransRenderCallbackOrComponent, type TransRenderProps };
@@ -0,0 +1,42 @@
1
+ import React, { ComponentType } from 'react';
2
+ import { MessageOptions, I18n } from '@lingui/core';
3
+
4
+ type TransRenderProps = {
5
+ id: string;
6
+ translation: React.ReactNode;
7
+ children: React.ReactNode;
8
+ message?: string | null;
9
+ isTranslated: boolean;
10
+ };
11
+ type TransRenderCallbackOrComponent = {
12
+ component?: undefined;
13
+ render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
14
+ } | {
15
+ component?: React.ComponentType<TransRenderProps> | null;
16
+ render?: undefined;
17
+ };
18
+ type TransProps = {
19
+ id: string;
20
+ message?: string;
21
+ values?: Record<string, unknown>;
22
+ components?: {
23
+ [key: string]: React.ElementType | any;
24
+ };
25
+ formats?: MessageOptions["formats"];
26
+ comment?: string;
27
+ children?: React.ReactNode;
28
+ } & TransRenderCallbackOrComponent;
29
+ /**
30
+ * Version of `<Trans>` component without using a Provider/Context React feature.
31
+ * Primarily made for support React Server Components (RSC)
32
+ *
33
+ * @experimental the api of this component is not stabilized yet.
34
+ */
35
+ declare function TransNoContext(props: TransProps & {
36
+ lingui: {
37
+ i18n: I18n;
38
+ defaultComponent?: ComponentType<TransRenderProps>;
39
+ };
40
+ }): React.ReactElement<any, any> | null;
41
+
42
+ export { TransNoContext, type TransProps, type TransRenderCallbackOrComponent, type TransRenderProps };
@@ -0,0 +1,42 @@
1
+ import React, { ComponentType } from 'react';
2
+ import { MessageOptions, I18n } from '@lingui/core';
3
+
4
+ type TransRenderProps = {
5
+ id: string;
6
+ translation: React.ReactNode;
7
+ children: React.ReactNode;
8
+ message?: string | null;
9
+ isTranslated: boolean;
10
+ };
11
+ type TransRenderCallbackOrComponent = {
12
+ component?: undefined;
13
+ render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
14
+ } | {
15
+ component?: React.ComponentType<TransRenderProps> | null;
16
+ render?: undefined;
17
+ };
18
+ type TransProps = {
19
+ id: string;
20
+ message?: string;
21
+ values?: Record<string, unknown>;
22
+ components?: {
23
+ [key: string]: React.ElementType | any;
24
+ };
25
+ formats?: MessageOptions["formats"];
26
+ comment?: string;
27
+ children?: React.ReactNode;
28
+ } & TransRenderCallbackOrComponent;
29
+ /**
30
+ * Version of `<Trans>` component without using a Provider/Context React feature.
31
+ * Primarily made for support React Server Components (RSC)
32
+ *
33
+ * @experimental the api of this component is not stabilized yet.
34
+ */
35
+ declare function TransNoContext(props: TransProps & {
36
+ lingui: {
37
+ i18n: I18n;
38
+ defaultComponent?: ComponentType<TransRenderProps>;
39
+ };
40
+ }): React.ReactElement<any, any> | null;
41
+
42
+ export { TransNoContext, type TransProps, type TransRenderCallbackOrComponent, type TransRenderProps };
@@ -0,0 +1,132 @@
1
+ import React from 'react';
2
+
3
+ const tagRe = /<([a-zA-Z0-9]+)>(.*?)<\/\1>|<([a-zA-Z0-9]+)\/>/;
4
+ const nlRe = /(?:\r\n|\r|\n)/g;
5
+ const voidElementTags = {
6
+ area: true,
7
+ base: true,
8
+ br: true,
9
+ col: true,
10
+ embed: true,
11
+ hr: true,
12
+ img: true,
13
+ input: true,
14
+ keygen: true,
15
+ link: true,
16
+ meta: true,
17
+ param: true,
18
+ source: true,
19
+ track: true,
20
+ wbr: true,
21
+ menuitem: true
22
+ };
23
+ function formatElements(value, elements = {}) {
24
+ const uniqueId = makeCounter(0, "$lingui$");
25
+ const parts = value.replace(nlRe, "").split(tagRe);
26
+ if (parts.length === 1)
27
+ return value;
28
+ const tree = [];
29
+ const before = parts.shift();
30
+ if (before)
31
+ tree.push(before);
32
+ for (const [index, children, after] of getElements(parts)) {
33
+ let element = typeof index !== "undefined" ? elements[index] : void 0;
34
+ if (!element || voidElementTags[element.type] && children) {
35
+ if (!element) {
36
+ console.error(
37
+ `Can't use element at index '${index}' as it is not declared in the original translation`
38
+ );
39
+ } else {
40
+ console.error(
41
+ `${element.type} is a void element tag therefore it must have no children`
42
+ );
43
+ }
44
+ element = React.createElement(React.Fragment);
45
+ }
46
+ if (Array.isArray(element)) {
47
+ element = React.createElement(React.Fragment, {}, element);
48
+ }
49
+ tree.push(
50
+ React.cloneElement(
51
+ element,
52
+ { key: uniqueId() },
53
+ // format children for pair tags
54
+ // unpaired tags might have children if it's a component passed as a variable
55
+ children ? formatElements(children, elements) : element.props.children
56
+ )
57
+ );
58
+ if (after)
59
+ tree.push(after);
60
+ }
61
+ return tree;
62
+ }
63
+ function getElements(parts) {
64
+ if (!parts.length)
65
+ return [];
66
+ const [paired, children, unpaired, after] = parts.slice(0, 4);
67
+ const triple = [paired || unpaired, children || "", after];
68
+ return [triple].concat(getElements(parts.slice(4, parts.length)));
69
+ }
70
+ const makeCounter = (count = 0, prefix = "") => () => `${prefix}_${count++}`;
71
+
72
+ function TransNoContext(props) {
73
+ const {
74
+ render,
75
+ component,
76
+ id,
77
+ message,
78
+ formats,
79
+ lingui: { i18n, defaultComponent }
80
+ } = props;
81
+ const values = { ...props.values };
82
+ const components = { ...props.components };
83
+ if (values) {
84
+ Object.keys(values).forEach((key) => {
85
+ const value = values[key];
86
+ const valueIsReactEl = React.isValidElement(value) || Array.isArray(value) && value.every(React.isValidElement);
87
+ if (!valueIsReactEl)
88
+ return;
89
+ const index = Object.keys(components).length;
90
+ components[index] = value;
91
+ values[key] = `<${index}/>`;
92
+ });
93
+ }
94
+ const _translation = i18n && typeof i18n._ === "function" ? i18n._(id, values, { message, formats }) : id;
95
+ const translation = _translation ? formatElements(_translation, components) : null;
96
+ if (render === null || component === null) {
97
+ return translation;
98
+ }
99
+ const FallbackComponent = defaultComponent || RenderFragment;
100
+ const i18nProps = {
101
+ id,
102
+ message,
103
+ translation,
104
+ isTranslated: id !== translation && message !== translation,
105
+ children: translation
106
+ // for type-compatibility with `component` prop
107
+ };
108
+ if (render && component) {
109
+ console.error(
110
+ "You can't use both `component` and `render` prop at the same time. `component` is ignored."
111
+ );
112
+ } else if (render && typeof render !== "function") {
113
+ console.error(
114
+ `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`
115
+ );
116
+ } else if (component && typeof component !== "function") {
117
+ console.error(
118
+ `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
119
+ );
120
+ return React.createElement(FallbackComponent, i18nProps, translation);
121
+ }
122
+ if (typeof render === "function") {
123
+ return render(i18nProps);
124
+ }
125
+ const Component = component || FallbackComponent;
126
+ return React.createElement(Component, i18nProps, translation);
127
+ }
128
+ const RenderFragment = ({ children }) => {
129
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
130
+ };
131
+
132
+ export { TransNoContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/react",
3
- "version": "4.4.1",
3
+ "version": "4.5.0",
4
4
  "sideEffects": false,
5
5
  "description": "React components for translations",
6
6
  "main": "./dist/index.cjs",
@@ -43,14 +43,24 @@
43
43
  "exports": {
44
44
  ".": {
45
45
  "require": {
46
- "types": "./dist/index.d.ts",
46
+ "types": "./dist/index.d.cts",
47
47
  "default": "./dist/index.cjs"
48
48
  },
49
49
  "import": {
50
- "types": "./dist/index.d.ts",
50
+ "types": "./dist/index.d.mts",
51
51
  "default": "./dist/index.mjs"
52
52
  }
53
53
  },
54
+ "./server": {
55
+ "require": {
56
+ "types": "./dist/server.d.ts",
57
+ "default": "./dist/server.cjs"
58
+ },
59
+ "import": {
60
+ "types": "./dist/server.d.ts",
61
+ "default": "./dist/server.mjs"
62
+ }
63
+ },
54
64
  "./package.json": "./package.json"
55
65
  },
56
66
  "files": [
@@ -63,19 +73,17 @@
63
73
  },
64
74
  "dependencies": {
65
75
  "@babel/runtime": "^7.20.13",
66
- "@lingui/core": "4.4.1",
67
- "use-sync-external-store": "^1.2.0"
76
+ "@lingui/core": "4.5.0"
68
77
  },
69
78
  "devDependencies": {
70
79
  "@lingui/jest-mocks": "*",
71
80
  "@testing-library/react": "^14.0.0",
72
81
  "@types/react": "^18.2.13",
73
- "@types/use-sync-external-store": "^0.0.3",
74
82
  "eslint-plugin-react": "^7.32.2",
75
83
  "eslint-plugin-react-hooks": "^4.6.0",
76
84
  "react": "^18.2.0",
77
85
  "react-dom": "^18.2.0",
78
- "unbuild": "^1.1.2"
86
+ "unbuild": "2.0.0"
79
87
  },
80
- "gitHead": "e7103c9f06a493e5871086e121f037309b0b742c"
88
+ "gitHead": "62c92d1f8c60b3890bdda870f307fa780d423080"
81
89
  }