如何将翻译后的字符串传递给 react-jhipster 中的另一个组件?

How do I pass a translated string to another component in react-jhipster?

当我想在组件中添加翻译后的字符串时,我可以使用翻译组件(来自 react-jhipster):

<p><Translation contentKey="my.key">Some text</Translation></p>

但是如果我想将翻译后的字符串传递给另一个组件,我该怎么做呢?喜欢:

<MyComponent text={{translate("my.key")}} />

正如您在评论中所说,您正在使用 react-jhipster 库。

据我所知,您可以从 react-jhipster.

导入 translate (contentKey: string, interpolate?: any, children?: string) 函数

尝试:import {translate} from 'react-jhipster'

Source: https://github.com/jhipster/react-jhipster/blob/master/src/language/translate.tsx


而且你不需要像 props 一样总是传递它,你也可以在组件的主体中使用它。