Skip to content

Commit

Permalink
feature(react): introduce TransNoContext component for future RSC sup…
Browse files Browse the repository at this point in the history
…port (#1738)
  • Loading branch information
timofei-iatsenko authored Aug 2, 2023
1 parent 3fd88b0 commit 8c1c70c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/react/src/Trans.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { setupI18n } from "@lingui/core"
import { mockConsole } from "@lingui/jest-mocks"
import { PropsWithChildren } from "react"
import { TransNoContext } from "./Trans"

describe("Trans component", () => {
/*
Expand Down Expand Up @@ -410,5 +411,20 @@ describe("Trans component", () => {
)
expect(markup.queryByTestId("is-translated")?.innerHTML).toEqual("false")
})

describe("TransNoContext", () => {
it("Should render without provider/context", () => {
const translation = render(
<TransNoContext
id="All human beings are born free and equal in dignity and rights."
lingui={{ i18n: i18n }}
/>
).container.textContent

expect(translation).toEqual(
"Všichni lidé rodí se svobodní a sobě rovní co do důstojnosti a práv."
)
})
})
})
})
22 changes: 19 additions & 3 deletions packages/react/src/Trans.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from "react"
import React, { ComponentType } from "react"

import { useLingui } from "./I18nProvider"
import { formatElements } from "./format"
import type { MessageOptions } from "@lingui/core"
import { I18n } from "@lingui/core"

export type TransRenderProps = {
id: string
Expand Down Expand Up @@ -35,8 +36,23 @@ export type TransProps = {
} & TransRenderCallbackOrComponent

export function Trans(props: TransProps): React.ReactElement<any, any> | null {
const { i18n, defaultComponent } = useLingui()
const { render, component, id, message, formats } = props
const lingui = useLingui()
return React.createElement(TransNoContext, { ...props, lingui })
}

export function TransNoContext(
props: TransProps & {
lingui: { i18n: I18n; defaultComponent?: ComponentType<TransRenderProps> }
}
): React.ReactElement<any, any> | null {
const {
render,
component,
id,
message,
formats,
lingui: { i18n, defaultComponent },
} = props

const values = { ...props.values }
const components = { ...props.components }
Expand Down

1 comment on commit 8c1c70c

@vercel
Copy link

@vercel vercel bot commented on 8c1c70c Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.