在JSON字符串中反应i18n换行 [英] React i18n break lines in JSON String

查看:584
本文介绍了在JSON字符串中反应i18n换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with i18next for react https://github.com/i18next/react-i18next. I'm struggling to break lines within the string in my JSON language file.

这是我已经尝试过的方法,不会中断新行:

This is what I already tried, which doesn't break a new line:

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

我显然尝试在每个句子后换行.这就是我所说的:

I obviously try to make a new line after each sentence. This is how I call it:

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

有什么想法吗?谢谢!

推荐答案

您可以使用CSS white-space: pre-line;& \n在翻译文本中.

You can do it with css white-space: pre-line; & \n in the translation text.

const root = document.getElementById('root');

i18next.init({
  lng: 'en',
  resources: {
    en: {
      translation: {
        "key": "Hello world\nThis sentence should appear on the second line"
      }
    }
  }
}, function(err, t) {
  // initialized and ready to go!
  root.innerHTML = i18next.t('key');
});

#root {
  white-space: pre-line;
}

<script src="https://unpkg.com/i18next@15.0.9/dist/umd/i18next.min.js"></script>

<div id="root"></div>

这篇关于在JSON字符串中反应i18n换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆