在JSON字符串中反应i18n断行

问题描述 投票:2回答:3

我正在使用i18next进行反应https://github.com/i18next/react-i18next。我正努力在我的JSON语言文件中打破字符串中的行。

这是我已经尝试过的,不会破坏新的一行:

  • line: "This is a line. \n This is another line. \n Yet another line"enter image description here
  • line: ("This is a line."+ <br/> + "This is another line. \n Yet another line")enter image description here
  • line: ('This is a line. <br/> This is another line. \n Yet another line')enter image description here

我显然试着在每个句子后面换一个新行。这就是我所说的:

<TooltipLink onClick={() => {
    this.toggleHelpTextDialog(t('test:test.line'));
}}/>

有任何想法吗?谢谢!

html json reactjs i18next
3个回答
4
投票

你可以在翻译文本中用css white-space: pre-line;\n来做。

https://codesandbox.io/s/o95o8849jz


1
投票

<br />是正确的方法,但它不会进入文本或翻译。示例:

<div>
  {'my text line 1'}
  <br />
  {'my text line 2'}
</div>

但是,如果你想在文本中保留换行符(\ n),那么使用dangerouslySetInnerHTML这样做:

const text = "This is a line. \n This is another line. \n Yet another line";
<div dangerouslySetInnerHTML={text} />

0
投票

例如,您在JSON语言文件中写下以下文本。

文字:“你好\你好吗?\你在做什么?”

然后作为回报部分写

<div id='app'><div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<script>
      render() {
          return (
              text.split('\n').map(c => {
                  return ( <p> {c} </p>) 
                   });
                 )
               }
              

              ReactDOM.render(
                <BreakLine / >
                document.getElementById('app')
              )
</script>

好吧,当调用split方法时,尝试创建列表并从'\ n'中分离并使用方法映射类型将它们分别放在段落上以便断行;))

© www.soinside.com 2019 - 2024. All rights reserved.