|
1 | | -import React, { ComponentType } from "react" |
| 1 | +import React from "react" |
2 | 2 |
|
3 | 3 | import { useLingui } from "./I18nProvider" |
4 | | -import { formatElements } from "./format" |
5 | | -import type { MessageOptions } from "@lingui/core" |
6 | | -import { I18n } from "@lingui/core" |
7 | | - |
8 | | -export type TransRenderProps = { |
9 | | - id: string |
10 | | - translation: React.ReactNode |
11 | | - children: React.ReactNode |
12 | | - message?: string | null |
13 | | - isTranslated: boolean |
14 | | -} |
15 | | - |
16 | | -export type TransRenderCallbackOrComponent = |
17 | | - | { |
18 | | - component?: undefined |
19 | | - render?: |
20 | | - | ((props: TransRenderProps) => React.ReactElement<any, any>) |
21 | | - | null |
22 | | - } |
23 | | - | { |
24 | | - component?: React.ComponentType<TransRenderProps> | null |
25 | | - render?: undefined |
26 | | - } |
27 | | - |
28 | | -export type TransProps = { |
29 | | - id: string |
30 | | - message?: string |
31 | | - values?: Record<string, unknown> |
32 | | - components?: { [key: string]: React.ElementType | any } |
33 | | - formats?: MessageOptions["formats"] |
34 | | - comment?: string |
35 | | - children?: React.ReactNode |
36 | | -} & TransRenderCallbackOrComponent |
| 4 | +import { TransNoContext, TransProps } from "./TransNoContext" |
37 | 5 |
|
38 | 6 | export function Trans(props: TransProps): React.ReactElement<any, any> | null { |
39 | 7 | const lingui = useLingui() |
40 | 8 | return React.createElement(TransNoContext, { ...props, lingui }) |
41 | 9 | } |
42 | | - |
43 | | -/** |
44 | | - * Version of `<Trans>` component without using a Provider/Context React feature. |
45 | | - * Primarily made for support React Server Components (RSC) |
46 | | - * |
47 | | - * @experimental the api of this component is not stabilized yet. |
48 | | - */ |
49 | | -export function TransNoContext( |
50 | | - props: TransProps & { |
51 | | - lingui: { i18n: I18n; defaultComponent?: ComponentType<TransRenderProps> } |
52 | | - } |
53 | | -): React.ReactElement<any, any> | null { |
54 | | - const { |
55 | | - render, |
56 | | - component, |
57 | | - id, |
58 | | - message, |
59 | | - formats, |
60 | | - lingui: { i18n, defaultComponent }, |
61 | | - } = props |
62 | | - |
63 | | - const values = { ...props.values } |
64 | | - const components = { ...props.components } |
65 | | - |
66 | | - if (values) { |
67 | | - /* |
68 | | - Related discussion: https://github.com/lingui/js-lingui/issues/183 |
69 | | -
|
70 | | - Values *might* contain React elements with static content. |
71 | | - They're replaced with <INDEX /> placeholders and added to `components`. |
72 | | -
|
73 | | - Example: |
74 | | - Translation: Hello {name} |
75 | | - Values: { name: <strong>Jane</strong> } |
76 | | -
|
77 | | - It'll become "Hello <0 />" with components=[<strong>Jane</strong>] |
78 | | - */ |
79 | | - |
80 | | - Object.keys(values).forEach((key) => { |
81 | | - const value = values[key] |
82 | | - const valueIsReactEl = |
83 | | - React.isValidElement(value) || |
84 | | - (Array.isArray(value) && value.every(React.isValidElement)) |
85 | | - if (!valueIsReactEl) return |
86 | | - |
87 | | - const index = Object.keys(components).length |
88 | | - |
89 | | - components[index] = value |
90 | | - values[key] = `<${index}/>` |
91 | | - }) |
92 | | - } |
93 | | - |
94 | | - const _translation: string = |
95 | | - i18n && typeof i18n._ === "function" |
96 | | - ? i18n._(id, values, { message, formats }) |
97 | | - : id // i18n provider isn't loaded at all |
98 | | - |
99 | | - const translation = _translation |
100 | | - ? formatElements(_translation, components) |
101 | | - : null |
102 | | - |
103 | | - if (render === null || component === null) { |
104 | | - // Although `string` is a valid react element, types only allow `Element` |
105 | | - // Upstream issue: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20544 |
106 | | - return translation as unknown as React.ReactElement<any, any> |
107 | | - } |
108 | | - |
109 | | - const FallbackComponent: React.ComponentType<TransRenderProps> = |
110 | | - defaultComponent || RenderFragment |
111 | | - |
112 | | - const i18nProps: TransRenderProps = { |
113 | | - id, |
114 | | - message, |
115 | | - translation, |
116 | | - isTranslated: id !== translation && message !== translation, |
117 | | - children: translation, // for type-compatibility with `component` prop |
118 | | - } |
119 | | - |
120 | | - // Validation of `render` and `component` props |
121 | | - if (render && component) { |
122 | | - console.error( |
123 | | - "You can't use both `component` and `render` prop at the same time. `component` is ignored." |
124 | | - ) |
125 | | - } else if (render && typeof render !== "function") { |
126 | | - console.error( |
127 | | - `Invalid value supplied to prop \`render\`. It must be a function, provided ${render}` |
128 | | - ) |
129 | | - } else if (component && typeof component !== "function") { |
130 | | - // Apparently, both function components and class components are functions |
131 | | - // See https://stackoverflow.com/a/41658173/1535540 |
132 | | - console.error( |
133 | | - `Invalid value supplied to prop \`component\`. It must be a React component, provided ${component}` |
134 | | - ) |
135 | | - return React.createElement(FallbackComponent, i18nProps, translation) |
136 | | - } |
137 | | - |
138 | | - // Rendering using a render prop |
139 | | - if (typeof render === "function") { |
140 | | - // Component: render={(props) => <a title={props.translation}>x</a>} |
141 | | - return render(i18nProps) |
142 | | - } |
143 | | - |
144 | | - // `component` prop has a higher precedence over `defaultComponent` |
145 | | - const Component: React.ComponentType<TransRenderProps> = |
146 | | - component || FallbackComponent |
147 | | - |
148 | | - return React.createElement(Component, i18nProps, translation) |
149 | | -} |
150 | | - |
151 | | -const RenderFragment = ({ children }: TransRenderProps) => { |
152 | | - // cannot use React.Fragment directly because we're passing in props that it doesn't support |
153 | | - return <React.Fragment>{children}</React.Fragment> |
154 | | -} |
0 commit comments