我正在使用 `react-i18next` 进行翻译,我需要为字符串的某个部分赋予样式,我该怎么做?

I am using `react-i18next` for translation and I need to give style to a certain part of string, how can I do that?

我的 en.json 文件 "outOf100": "{{val}} out of 100" 中有这个字符串,我在 JSX 中像这样使用它 {t('outOf100', {val: <h6>22.45</h6>})}

任何人都可以告诉我如何使 val 看起来像 h6 而字符串的所有其他部分正常。

您需要使用 Trans 组件来格式化带有不同 HTML 标签的字符串。它看起来像这样:

  //inside React-component
  return (
    <Trans i18nKey="outOf100">
      <h6>{{val}}</h6> out of 100.
    </Trans>
  );

  //in en.json
  "outOf100": "<0>{{val}}</0> out of 100"

有关Trans组件的更多信息,请see the docs.