2016-10-03 56 views
0

我使用的一个项目i18next,不能得到解决,包括翻译文件的HTML标签,并让他们正确地呈现。HTML标签阵营

.json翻译文件的一个例子:

"en": { 
    "product": { 
    "header": "Welcome, <strong>User!</strong>" 
    } 
} 

有一个excellent answer to this question,但是与JQuery的。我没有使用JQuery,我的项目是反应,这里是我的设置:

import i18next from 'i18next'; 
import en from 'locales/en'; 

i18next. 
    init({ 
    lng: 'en', 
    fallbackLng: false, 
    resources: en, 
    debug: false, 

    interpolation: { 
     escapeValue: false 
    } 
    }); 

export default i18next.t.bind(i18next); 

在部分我:

import t from 'i18n'; 

t('product.header') 

的HTML我想:

Welcome, <strong>User!</strong> 

Html我得到:

Welcome, &lt;strong&gt;User!&lt;/strong&gt 

比KS

回答

3

还没有反应过来,i18next的问题 - 你就不能添加HTML到一个反应元素。从XSS漏洞做出反应的意愿,你的安全和逃生内容:

更多的细节和可能的解决方案:https://facebook.github.io/react/tips/dangerously-set-inner-html.html

,但如果你把用户的内容有没有逃过你打开你的网站XSS攻击知道。

更安全的阅读反应-i18next自述: https://github.com/i18next/react-i18next#interpolate-component

它让你拆分内容

所以“最好”的解决方案 - 至少我们做什么 - 在翻译用降价并使用如:https://github.com/acdlite/react-remarkable将其转换为格式化的内容。

+0

随着V4.4.0我们增加了一个新的组件,使之简单,包括反应的组分到您的翻译https://react.i18next.com/components/trans-component.html – jamuhl