【问题标题】:i18next, react and HTML provided as a variablei18next, react and HTML provided as a variable
【发布时间】:2022-12-26 19:55:08
【问题描述】:

I have a project where users can provide a text for searching. This text CAN be HTML, and because of it, we won't escape it, as in the source, it is not escaped.

We provide a whole bunch of functionality, including deleting not needed searches.

We support many languages (including Japanese, among others), and therefore we cannot decide upfront in which place in translation the search we want to delete will be placed. So we use Trans component. And here the problem starts.

React itself handles HTML strings quite well. It just displays source code <a href="example.com">link</a>. But Trans interprets HTML (and displays link) or escapes HTML and displays <a href="example.com">link</a>.

I tried changing options (like escapeValue) or manually working with the string, which is passed as a value to translation. Nothing works as expected.

Is there any way to force Trans to behave as a react component and display HTML source code?

【问题讨论】:

标签: html reactjs i18next react-i18next


【解决方案1】:

I found the solution:

in i18n.init set

interpolation: {
  escapeValue: true
}

Then in your Trans component, use prop:

shouldUnescape={true}

Working codesandbox is here

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2022-12-01
    • 2022-12-27
    • 1970-01-01
    • 2022-12-27
    • 2023-01-15
    • 2022-12-02
    • 2021-03-22
    • 2019-04-08
    相关资源
    最近更新 更多