Skip to content

Commit 46241f0

Browse files
authored
feat(i18n): enable jsx element as prop value (#1008)
1 parent 6fbbeba commit 46241f0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

packages/use-i18n/src/__tests__/locales/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"with.identifier": "Are you sure you want to delete {identifier}?",
23
"plurals": "You have {numPhotos, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}",
34
"subtitle": "Here is a subtitle",
45
"tests.test.namespaces": "test",

packages/use-i18n/src/__tests__/usei18n.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,4 +600,28 @@ describe('i18n hook', () => {
600600
expect(result.current.dateFnsLocale?.code).toEqual('en-GB')
601601
})
602602
})
603+
604+
it('should work with a component', async () => {
605+
const { result } = renderHook(() => useTranslation([]), {
606+
wrapper: wrapper({ defaultLocale: 'en' }),
607+
})
608+
const CustomComponent = ({ children }: { children: ReactNode }) => (
609+
<p style={{ fontWeight: 'bold' }}>{children}</p>
610+
)
611+
612+
await waitFor(() => {
613+
expect(
614+
result.current.t('with.identifier', { identifier: <b>My resource</b> }),
615+
).toEqual(['Are you sure you want to delete ', <b>My resource</b>, '?'])
616+
expect(
617+
result.current.t('with.identifier', {
618+
identifier: <CustomComponent>My resource</CustomComponent>,
619+
}),
620+
).toEqual([
621+
'Are you sure you want to delete ',
622+
<CustomComponent>My resource</CustomComponent>,
623+
'?',
624+
])
625+
})
626+
})
603627
})

packages/use-i18n/src/usei18n.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type TranslationsByLocales = Record<string, BaseLocale>
2828

2929
export type InitialTranslateFn = (
3030
key: string,
31-
context?: Record<string, LocaleValue>,
31+
context?: Record<string, LocaleValue | JSX.Element>,
3232
) => string
3333
export type InitialScopedTranslateFn = (
3434
namespace: string,

0 commit comments

Comments
 (0)