@lingui/react 4.8.0-next.1 → 4.10.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.
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ const server = require('./server.cjs');
4
+ const TransNoContext = require('./shared/react.6b65948d.cjs');
5
+ const React = require('react');
6
+
7
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
8
+
9
+ const React__default = /*#__PURE__*/_interopDefaultCompat(React);
10
+
11
+ function TransRsc(props) {
12
+ const i18n = server.getI18n();
13
+ if (!i18n) {
14
+ throw new Error(
15
+ "You tried to use `Trans` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
16
+ );
17
+ }
18
+ return /* @__PURE__ */ React__default.createElement(TransNoContext.TransNoContext, { ...props, lingui: { i18n } });
19
+ }
20
+
21
+ function useLingui() {
22
+ const i18n = server.getI18n();
23
+ if (!i18n) {
24
+ throw new Error(
25
+ "You tried to use `useLingui` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
26
+ );
27
+ }
28
+ return {
29
+ i18n,
30
+ _: i18n.t.bind(i18n)
31
+ };
32
+ }
33
+
34
+ exports.Trans = TransRsc;
35
+ exports.useLingui = useLingui;
@@ -0,0 +1,11 @@
1
+ import { T as TransProps } from './shared/react.a673be03.cjs';
2
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.cjs';
3
+ import { b as I18nContext } from './shared/react.db09d9c7.cjs';
4
+ import React from 'react';
5
+ import '@lingui/core';
6
+
7
+ declare function TransRsc(props: TransProps): React.ReactElement<any, any> | null;
8
+
9
+ declare function useLingui(): I18nContext;
10
+
11
+ export { TransRsc as Trans, TransProps, useLingui };
@@ -0,0 +1,11 @@
1
+ import { T as TransProps } from './shared/react.a673be03.mjs';
2
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.mjs';
3
+ import { b as I18nContext } from './shared/react.145db227.mjs';
4
+ import React from 'react';
5
+ import '@lingui/core';
6
+
7
+ declare function TransRsc(props: TransProps): React.ReactElement<any, any> | null;
8
+
9
+ declare function useLingui(): I18nContext;
10
+
11
+ export { TransRsc as Trans, TransProps, useLingui };
@@ -0,0 +1,11 @@
1
+ import { T as TransProps } from './shared/react.a673be03.js';
2
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.js';
3
+ import { b as I18nContext } from './shared/react.80f80298.js';
4
+ import React from 'react';
5
+ import '@lingui/core';
6
+
7
+ declare function TransRsc(props: TransProps): React.ReactElement<any, any> | null;
8
+
9
+ declare function useLingui(): I18nContext;
10
+
11
+ export { TransRsc as Trans, TransProps, useLingui };
@@ -0,0 +1,28 @@
1
+ import { getI18n } from './server.mjs';
2
+ import { T as TransNoContext } from './shared/react.1d406965.mjs';
3
+ import React from 'react';
4
+
5
+ function TransRsc(props) {
6
+ const i18n = getI18n();
7
+ if (!i18n) {
8
+ throw new Error(
9
+ "You tried to use `Trans` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
10
+ );
11
+ }
12
+ return /* @__PURE__ */ React.createElement(TransNoContext, { ...props, lingui: { i18n } });
13
+ }
14
+
15
+ function useLingui() {
16
+ const i18n = getI18n();
17
+ if (!i18n) {
18
+ throw new Error(
19
+ "You tried to use `useLingui` in Server Component, but i18n instance for RSC hasn't been setup.\nMake sure to call `setI18n` in root of your page"
20
+ );
21
+ }
22
+ return {
23
+ i18n,
24
+ _: i18n.t.bind(i18n)
25
+ };
26
+ }
27
+
28
+ export { TransRsc as Trans, useLingui };
package/dist/index.cjs CHANGED
@@ -1,21 +1,27 @@
1
+ 'use client';
1
2
  'use strict';
2
3
 
3
4
  const React = require('react');
4
- const server = require('./server.cjs');
5
+ const TransNoContext = require('./shared/react.6b65948d.cjs');
5
6
 
6
7
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
8
 
8
9
  const React__default = /*#__PURE__*/_interopDefaultCompat(React);
9
10
 
10
11
  const LinguiContext = React__default.createContext(null);
11
- function useLingui() {
12
+ const useLinguiInternal = (devErrorMessage) => {
12
13
  const context = React__default.useContext(LinguiContext);
13
14
  if (process.env.NODE_ENV !== "production") {
14
15
  if (context == null) {
15
- throw new Error("useLingui hook was used without I18nProvider.");
16
+ throw new Error(
17
+ devErrorMessage ?? "useLingui hook was used without I18nProvider."
18
+ );
16
19
  }
17
20
  }
18
21
  return context;
22
+ };
23
+ function useLingui() {
24
+ return useLinguiInternal();
19
25
  }
20
26
  const I18nProvider = ({
21
27
  i18n,
@@ -53,8 +59,13 @@ const I18nProvider = ({
53
59
  };
54
60
 
55
61
  function Trans(props) {
56
- const lingui = useLingui();
57
- return React__default.createElement(server.TransNoContext, { ...props, lingui });
62
+ let errMessage = void 0;
63
+ if (process.env.NODE_ENV !== "production") {
64
+ errMessage = `Trans component was rendered without I18nProvider.
65
+ Attempted to render message: ${props.message} id: ${props.id}. Make sure this component is rendered inside a I18nProvider.`;
66
+ }
67
+ const lingui = useLinguiInternal(errMessage);
68
+ return React__default.createElement(TransNoContext.TransNoContext, { ...props, lingui });
58
69
  }
59
70
 
60
71
  exports.I18nProvider = I18nProvider;
package/dist/index.d.cts CHANGED
@@ -1,20 +1,9 @@
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>;
1
+ export { b as I18nContext, I as I18nProvider, a as I18nProviderProps, L as LinguiContext, u as useLingui } from './shared/react.db09d9c7.cjs';
2
+ import React from 'react';
3
+ import { T as TransProps } from './shared/react.a673be03.cjs';
4
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.cjs';
5
+ import '@lingui/core';
17
6
 
18
7
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
19
8
 
20
- export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
9
+ export { Trans, TransProps };
package/dist/index.d.mts CHANGED
@@ -1,20 +1,9 @@
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>;
1
+ export { b as I18nContext, I as I18nProvider, a as I18nProviderProps, L as LinguiContext, u as useLingui } from './shared/react.145db227.mjs';
2
+ import React from 'react';
3
+ import { T as TransProps } from './shared/react.a673be03.mjs';
4
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.mjs';
5
+ import '@lingui/core';
17
6
 
18
7
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
19
8
 
20
- export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
9
+ export { Trans, TransProps };
package/dist/index.d.ts CHANGED
@@ -1,20 +1,9 @@
1
- import React, { ComponentType, FunctionComponent } from 'react';
2
- import { I18n } from '@lingui/core';
3
- import { TransRenderProps, TransProps } from './server.js';
4
- export { TransRenderCallbackOrComponent } from './server.js';
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>;
1
+ export { b as I18nContext, I as I18nProvider, a as I18nProviderProps, L as LinguiContext, u as useLingui } from './shared/react.80f80298.js';
2
+ import React from 'react';
3
+ import { T as TransProps } from './shared/react.a673be03.js';
4
+ export { b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.js';
5
+ import '@lingui/core';
17
6
 
18
7
  declare function Trans(props: TransProps): React.ReactElement<any, any> | null;
19
8
 
20
- export { type I18nContext, I18nProvider, type I18nProviderProps, LinguiContext, Trans, TransProps, TransRenderProps, useLingui };
9
+ export { Trans, TransProps };
package/dist/index.mjs CHANGED
@@ -1,15 +1,21 @@
1
+ 'use client';
1
2
  import React from 'react';
2
- import { TransNoContext } from './server.mjs';
3
+ import { T as TransNoContext } from './shared/react.1d406965.mjs';
3
4
 
4
5
  const LinguiContext = React.createContext(null);
5
- function useLingui() {
6
+ const useLinguiInternal = (devErrorMessage) => {
6
7
  const context = React.useContext(LinguiContext);
7
8
  if (process.env.NODE_ENV !== "production") {
8
9
  if (context == null) {
9
- throw new Error("useLingui hook was used without I18nProvider.");
10
+ throw new Error(
11
+ devErrorMessage ?? "useLingui hook was used without I18nProvider."
12
+ );
10
13
  }
11
14
  }
12
15
  return context;
16
+ };
17
+ function useLingui() {
18
+ return useLinguiInternal();
13
19
  }
14
20
  const I18nProvider = ({
15
21
  i18n,
@@ -47,7 +53,12 @@ const I18nProvider = ({
47
53
  };
48
54
 
49
55
  function Trans(props) {
50
- const lingui = useLingui();
56
+ let errMessage = void 0;
57
+ if (process.env.NODE_ENV !== "production") {
58
+ errMessage = `Trans component was rendered without I18nProvider.
59
+ Attempted to render message: ${props.message} id: ${props.id}. Make sure this component is rendered inside a I18nProvider.`;
60
+ }
61
+ const lingui = useLinguiInternal(errMessage);
51
62
  return React.createElement(TransNoContext, { ...props, lingui });
52
63
  }
53
64
 
package/dist/server.cjs CHANGED
@@ -1,139 +1,28 @@
1
1
  'use strict';
2
2
 
3
+ const TransNoContext = require('./shared/react.6b65948d.cjs');
3
4
  const React = require('react');
4
5
 
5
6
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
6
7
 
7
8
  const React__default = /*#__PURE__*/_interopDefaultCompat(React);
8
9
 
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);
10
+ let cache = null;
11
+ const getLinguiCache = () => {
12
+ if (!cache) {
13
+ cache = React__default.cache(() => ({
14
+ current: null
15
+ }));
66
16
  }
67
- return tree;
17
+ return cache();
18
+ };
19
+ function setI18n(i18n) {
20
+ getLinguiCache().current = i18n;
68
21
  }
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)));
22
+ function getI18n() {
23
+ return getLinguiCache().current;
75
24
  }
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
- // TODO vonovak - remove isTranslated prop in v5 release
111
- isTranslated: id !== translation && message !== translation,
112
- children: translation
113
- // for type-compatibility with `component` prop
114
- };
115
- if (render && component) {
116
- console.error(
117
- "You can't use both `component` and `render` prop at the same time. `component` is ignored."
118
- );
119
- } else if (render && typeof render !== "function") {
120
- console.error(
121
- `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}`
122
- );
123
- } else if (component && typeof component !== "function") {
124
- console.error(
125
- `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}`
126
- );
127
- return React__default.createElement(FallbackComponent, i18nProps, translation);
128
- }
129
- if (typeof render === "function") {
130
- return render(i18nProps);
131
- }
132
- const Component = component || FallbackComponent;
133
- return React__default.createElement(Component, i18nProps, translation);
134
- }
135
- const RenderFragment = ({ children }) => {
136
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, children);
137
- };
138
25
 
139
- exports.TransNoContext = TransNoContext;
26
+ exports.TransNoContext = TransNoContext.TransNoContext;
27
+ exports.getI18n = getI18n;
28
+ exports.setI18n = setI18n;
package/dist/server.d.cts CHANGED
@@ -1,45 +1,44 @@
1
- import React, { ComponentType } from 'react';
2
- import { MessageOptions, I18n } from '@lingui/core';
1
+ export { c as TransNoContext, T as TransProps, b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.cjs';
2
+ import { I18n } from '@lingui/core';
3
+ import 'react';
3
4
 
4
- type TransRenderProps = {
5
- id: string;
6
- translation: React.ReactNode;
7
- children: React.ReactNode;
8
- message?: string | null;
9
- /**
10
- * @deprecated isTranslated prop is undocumented and buggy. It'll be removed in v5 release.
11
- * */
12
- isTranslated: boolean;
13
- };
14
- type TransRenderCallbackOrComponent = {
15
- component?: undefined;
16
- render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
17
- } | {
18
- component?: React.ComponentType<TransRenderProps> | null;
19
- render?: undefined;
20
- };
21
- type TransProps = {
22
- id: string;
23
- message?: string;
24
- values?: Record<string, unknown>;
25
- components?: {
26
- [key: string]: React.ElementType | any;
27
- };
28
- formats?: MessageOptions["formats"];
29
- comment?: string;
30
- children?: React.ReactNode;
31
- } & TransRenderCallbackOrComponent;
32
5
  /**
33
- * Version of `<Trans>` component without using a Provider/Context React feature.
34
- * Primarily made for support React Server Components (RSC)
6
+ * This is an entry point for React Server Components (RSC)
35
7
  *
36
- * @experimental the api of this component is not stabilized yet.
8
+ * The RSC uses a static analysis to find any non-valid function calls in the import graph.
9
+ * That means this entry point and it's children should not have any Provider/Context calls.
37
10
  */
38
- declare function TransNoContext(props: TransProps & {
39
- lingui: {
40
- i18n: I18n;
41
- defaultComponent?: ComponentType<TransRenderProps>;
42
- };
43
- }): React.ReactElement<any, any> | null;
44
11
 
45
- export { TransNoContext, type TransProps, type TransRenderCallbackOrComponent, type TransRenderProps };
12
+ /**
13
+ * Set Lingui's i18n instance for later use in RSC Components
14
+ *
15
+ * Example:
16
+ *
17
+ * ```js
18
+ * import { setupI18n } from "@lingui/core";
19
+ *
20
+ * const i18n = setupI18n({
21
+ * locale,
22
+ * messages: { [locale]: messages },
23
+ * })
24
+ *
25
+ * setI18n(i18n);
26
+ * ```
27
+ */
28
+ declare function setI18n(i18n: I18n): void;
29
+ /**
30
+ * Get Lingui's i18n instance saved for RSC
31
+ *
32
+ * ```js
33
+ * export function generateMetadata() {
34
+ * const i18n = getI18n()
35
+ *
36
+ * return {
37
+ * title: t(i18n)`Translation Demo`,
38
+ * }
39
+ * }
40
+ * ```
41
+ */
42
+ declare function getI18n(): I18n | null;
43
+
44
+ export { getI18n, setI18n };
package/dist/server.d.mts CHANGED
@@ -1,45 +1,44 @@
1
- import React, { ComponentType } from 'react';
2
- import { MessageOptions, I18n } from '@lingui/core';
1
+ export { c as TransNoContext, T as TransProps, b as TransRenderCallbackOrComponent, a as TransRenderProps } from './shared/react.a673be03.mjs';
2
+ import { I18n } from '@lingui/core';
3
+ import 'react';
3
4
 
4
- type TransRenderProps = {
5
- id: string;
6
- translation: React.ReactNode;
7
- children: React.ReactNode;
8
- message?: string | null;
9
- /**
10
- * @deprecated isTranslated prop is undocumented and buggy. It'll be removed in v5 release.
11
- * */
12
- isTranslated: boolean;
13
- };
14
- type TransRenderCallbackOrComponent = {
15
- component?: undefined;
16
- render?: ((props: TransRenderProps) => React.ReactElement<any, any>) | null;
17
- } | {
18
- component?: React.ComponentType<TransRenderProps> | null;
19
- render?: undefined;
20
- };
21
- type TransProps = {
22
- id: string;
23
- message?: string;
24
- values?: Record<string, unknown>;
25
- components?: {
26
- [key: string]: React.ElementType | any;
27
- };
28
- formats?: MessageOptions["formats"];
29
- comment?: string;
30
- children?: React.ReactNode;
31
- } & TransRenderCallbackOrComponent;
32
5
  /**
33
- * Version of `<Trans>` component without using a Provider/Context React feature.
34
- * Primarily made for support React Server Components (RSC)
6
+ * This is an entry point for React Server Components (RSC)
35
7
  *
36
- * @experimental the api of this component is not stabilized yet.
8
+ * The RSC uses a static analysis to find any non-valid function calls in the import graph.
9
+ * That means this entry point and it's children should not have any Provider/Context calls.
37
10
  */
38
- declare function TransNoContext(props: TransProps & {
39
- lingui: {
40
- i18n: I18n;
41
- defaultComponent?: ComponentType<TransRenderProps>;
42
- };
43
- }): React.ReactElement<any, any> | null;
44
11
 
45
- export { TransNoContext, type TransProps, type TransRenderCallbackOrComponent, type TransRenderProps };
12
+ /**
13
+ * Set Lingui's i18n instance for later use in RSC Components
14
+ *
15
+ * Example:
16
+ *
17
+ * ```js
18
+ * import { setupI18n } from "@lingui/core";
19
+ *
20
+ * const i18n = setupI18n({
21
+ * locale,
22
+ * messages: { [locale]: messages },
23
+ * })
24
+ *
25
+ * setI18n(i18n);
26
+ * ```
27
+ */
28
+ declare function setI18n(i18n: I18n): void;
29
+ /**
30
+ * Get Lingui's i18n instance saved for RSC
31
+ *
32
+ * ```js
33
+ * export function generateMetadata() {
34
+ * const i18n = getI18n()
35
+ *
36
+ * return {
37
+ * title: t(i18n)`Translation Demo`,
38
+ * }
39
+ * }
40
+ * ```
41
+ */
42
+ declare function getI18n(): I18n | null;
43
+
44
+ export { getI18n, setI18n };