@lingui/react 4.2.1 → 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 +19 -8
- package/dist/index.d.ts +20 -6
- package/dist/index.mjs +19 -9
- package/package.json +7 -7
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
|
-
|
|
114
|
-
|
|
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
|
|
121
|
-
|
|
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(
|
|
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,20 +22,34 @@ type TransProps = {
|
|
|
22
22
|
components?: {
|
|
23
23
|
[key: string]: React.ElementType | any;
|
|
24
24
|
};
|
|
25
|
-
formats?:
|
|
25
|
+
formats?: MessageOptions["formats"];
|
|
26
|
+
comment?: string;
|
|
26
27
|
children?: React.ReactNode;
|
|
27
28
|
} & TransRenderCallbackOrComponent;
|
|
28
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;
|
|
29
42
|
|
|
30
43
|
type I18nContext = {
|
|
31
44
|
i18n: I18n;
|
|
45
|
+
_: I18n["_"];
|
|
32
46
|
defaultComponent?: ComponentType<TransRenderProps>;
|
|
33
47
|
};
|
|
34
|
-
type I18nProviderProps = I18nContext & {
|
|
48
|
+
type I18nProviderProps = Omit<I18nContext, "_"> & {
|
|
35
49
|
children?: React.ReactNode;
|
|
36
50
|
};
|
|
37
|
-
declare const LinguiContext: React.Context<I18nContext>;
|
|
51
|
+
declare const LinguiContext: React.Context<I18nContext | null>;
|
|
38
52
|
declare function useLingui(): I18nContext;
|
|
39
53
|
declare const I18nProvider: FunctionComponent<I18nProviderProps>;
|
|
40
54
|
|
|
41
|
-
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
|
-
|
|
112
|
-
|
|
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
|
|
119
|
-
|
|
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(
|
|
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
|
+
"version": "4.4.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "React components for translations",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -63,17 +63,17 @@
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@babel/runtime": "^7.20.13",
|
|
66
|
-
"@lingui/core": "4.
|
|
66
|
+
"@lingui/core": "4.4.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
69
|
"@lingui/jest-mocks": "*",
|
|
70
|
-
"@testing-library/react": "^
|
|
71
|
-
"@types/react": "^
|
|
70
|
+
"@testing-library/react": "^14.0.0",
|
|
71
|
+
"@types/react": "^18.2.13",
|
|
72
72
|
"eslint-plugin-react": "^7.32.2",
|
|
73
73
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
74
|
-
"react": "^
|
|
75
|
-
"react-dom": "^
|
|
74
|
+
"react": "^18.2.0",
|
|
75
|
+
"react-dom": "^18.2.0",
|
|
76
76
|
"unbuild": "^1.1.2"
|
|
77
77
|
},
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "c36c0340ced1c4b76d5e1c340785932122449ac9"
|
|
79
79
|
}
|