65

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", 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

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'));
}}/>

Any ideas? Thanks!

4
  • This entirely depends on the component. It may or may not support line breaks. Did you try to set the text directly without i18n translation? Commented Aug 24, 2017 at 8:04
  • I did set the text directly and it doesn't work either. I'm setting the text for the dialog via props. You think that might cause the problem?
    – Nocebo
    Commented Aug 24, 2017 at 8:08
  • Just look at this question, it may help you: stackoverflow.com/questions/2392766/multiline-strings-in-json
    – emma93
    Commented Aug 24, 2017 at 8:08
  • 1
    I think that should be straightforward in i18n library.
    – Dgloria
    Commented Oct 6, 2021 at 13:13

7 Answers 7

168

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"
        // ----------------^ new line char
      }
    }
  }
}, function(err, t) {
  // initialized and ready to go!
  root.innerHTML = i18next.t('key');
});
#root {
  white-space: pre-line;
}
<script src="https://unpkg.com/[email protected]/dist/umd/i18next.min.js"></script>

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

4
  • In my case the json-editor BabelEdit escaped the \n to \\n
    – pico_prob
    Commented Sep 29, 2020 at 12:12
  • I'm not familiar with BabelEdit but line break (the one that creates when you hit Enter) is not writing \n in the editor but press Enter.
    – felixmosh
    Commented Sep 29, 2020 at 12:31
  • 2
    Works perfectly fine in 2022. Thank you
    – ztefanie
    Commented Jan 5, 2022 at 10:17
  • 2
    Be aware: You might not want this behavior everywhere! Just add the style white-space: pre-line; (or white-space: pre; for that matter) to the element enclosing your text.
    – Andru
    Commented Jul 5, 2022 at 18:14
8

You can add specifically to TooltipLink styles:

white-space: pre-line;

then, your \n in locale will work.

1
  • 2
    This is the best solution in my case. I'm using Tailwind CSS, so I've used whitespace-pre-line class. Commented Feb 8, 2023 at 13:00
8

For those that do not want to add css code all over their React components, you can use the Trans component in the react-i18next package.

Create a translation key-value similar to:

{
  "message": "This is a line.<1/>This is another line.<1/>Yet another line"
}

Then, in your React component, use the <Trans/> component as follows

<Trans i18nKey="message" components={{ 1: <br /> }} />

The translation value will be rendered as

enter image description here

See https://github.com/i18next/react-i18next/issues/282

4

for example you write below text in JSON language file.

text:"Hello \n How are you? \n what are you doing?"

and then in return part write

<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>

well, when call split method, try to create list and seperate from '\n' and with method map type each of them on paragraph so can break line ;))

1
  • Hi, welcome to StackOverflow. Could you clarify your answer a bit please, the descriptive text is difficult to understand.
    – MandyShaw
    Commented Sep 25, 2018 at 19:09
2

For me, directly using this <br/> inside a string worked e.g

{ loremIpsum: "Lorem <br/> ipsum" }

<Trans
  i18nKey='loremIpsum'
/>

0

For more complex cases, you could be using <Trans /> component and any HTML tags and even create custom ones.

Example:

<Trans
  components={{ boldSpan: <MyTextComponent strong component="span" variant="body" /> }}
  i18nKey="YourNamespace:TEXT_KEY"
/>

While your JSON file (YourNamespace.json):

{
  "TEXT_KEY" : "<boldSpan>Lorem ipsum</boldSpan><br/>",
}

For more details, check here: https://react.i18next.com/latest/trans-component

0

you can simbly use dangerouslySetInnerHTML like this

{"text":"first <br/> second"}

then

<div dangerouslySetInnerHTML={{ __html: t('text') }}></div>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.