-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): native support react RSC (#1914)
- Loading branch information
1 parent
d185981
commit 0e94f2a
Showing
5 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { TransProps, TransNoContext } from "./TransNoContext" | ||
import React from "react" | ||
import { getI18n } from "./server" | ||
|
||
export function TransRsc( | ||
props: TransProps | ||
): React.ReactElement<any, any> | null { | ||
const i18n = getI18n() | ||
if (!i18n) { | ||
throw new Error( | ||
"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" | ||
) | ||
} | ||
return <TransNoContext {...props} lingui={{ i18n }} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export type { | ||
TransProps, | ||
TransRenderProps, | ||
TransRenderCallbackOrComponent, | ||
} from "./TransNoContext" | ||
|
||
import type { I18nContext } from "./I18nProvider" | ||
import { getI18n } from "./server" | ||
|
||
export { TransRsc as Trans } from "./TransRsc" | ||
|
||
export function useLingui(): I18nContext { | ||
const i18n = getI18n() | ||
if (!i18n) { | ||
throw new Error( | ||
"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" | ||
) | ||
} | ||
|
||
return { | ||
i18n, | ||
_: i18n.t.bind(i18n), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,67 @@ | ||
/// <reference types="react/canary" /> | ||
/** | ||
* This is an entry point for React Server Components (RSC) | ||
* | ||
* The RSC uses a static analysis to find any non-valid function calls in the import graph. | ||
* That means this entry point and it's children should not have any Provider/Context calls. | ||
*/ | ||
export { TransNoContext } from "./TransNoContext" | ||
|
||
export type { | ||
TransProps, | ||
TransRenderProps, | ||
TransRenderCallbackOrComponent, | ||
} from "./TransNoContext" | ||
|
||
import type { I18n } from "@lingui/core" | ||
import React from "react" | ||
|
||
let cache: (() => { current: I18n | null }) | null = null | ||
|
||
const getLinguiCache = () => { | ||
// make lazy initialization of React.cache | ||
// so it will not execute when module just imported | ||
if (!cache) { | ||
cache = React.cache((): { current: I18n | null } => ({ | ||
current: null, | ||
})) | ||
} | ||
|
||
return cache() | ||
} | ||
|
||
/** | ||
* Set Lingui's i18n instance for later use in RSC Components | ||
* | ||
* Example: | ||
* | ||
* ```js | ||
* import { setupI18n } from "@lingui/core"; | ||
* | ||
* const i18n = setupI18n({ | ||
* locale, | ||
* messages: { [locale]: messages }, | ||
* }) | ||
* | ||
* setI18n(i18n); | ||
* ``` | ||
*/ | ||
export function setI18n(i18n: I18n) { | ||
getLinguiCache().current = i18n | ||
} | ||
|
||
/** | ||
* Get Lingui's i18n instance saved for RSC | ||
* | ||
* ```js | ||
* export function generateMetadata() { | ||
* const i18n = getI18n() | ||
* | ||
* return { | ||
* title: t(i18n)`Translation Demo`, | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export function getI18n(): I18n | null { | ||
return getLinguiCache().current | ||
} |