2016-10-03 60 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