Skip to content

Commit caf2699

Browse files
committed
Fixed NgTranslate
1 parent a63225e commit caf2699

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/lib/jsx/NgTranslate.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import type { translate } from 'angular'
2-
import React, { useMemo } from 'react'
2+
import React, { useEffect, useMemo, useState } from 'react'
33
import { getAngularService } from '../ServiceProvider'
44

55
type Props = {
6-
id: string
6+
children: string
77
substitutions?: unknown
88
}
99

1010
/**
1111
* Helper element that uses Angular's translate service to translate a string
1212
* @returns
1313
*/
14-
const NgTranslate = ({ id, substitutions }: Props) => {
14+
const NgTranslate = ({ children, substitutions }: Props) => {
1515
const $translate = useMemo(() => getAngularService<translate.ITranslateService>('$translate'), [])
16+
const [text, setText] = useState(children.trim())
1617

17-
return <>{$translate.instant(id, substitutions)}</>
18+
useEffect(() => {
19+
const update = () => {
20+
const txKey = children.trim()
21+
setText($translate.instant(txKey, substitutions))
22+
}
23+
if ($translate.isReady()) {
24+
update()
25+
} else {
26+
$translate.onReady(update)
27+
}
28+
}, [children, substitutions])
29+
30+
return <>{text}</>
1831
}
1932

2033
export default NgTranslate

0 commit comments

Comments
 (0)