Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React i18n break lines in JSON String

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!

like image 697
Nocebo Avatar asked Aug 24 '17 07:08

Nocebo


People also ask

How to print a <br /> tag in react?

If you want to print a <br/> tag in react I wanted to point out that a neat workaround is to use the <Trans> component from i18next. const customInviteMessageByCreator = "DEMO APPOINTMENT<br/>This room is to test your video room."; You can write your variable wrapped in a Trans component like so:

How to write below text in JSON language file?

for example you write below text in JSON language file. text:"Hello How are you? what are you doing?" and then in return part write

How to break a line in a list using split method?

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


3 Answers

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>
like image 110
felixmosh Avatar answered Oct 23 '22 05:10

felixmosh


You also can do it with CSS that's very easy to do:

CSS:

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

HTML:

<script src="https://unpkg.com/[email protected]/dist/umd/i18next.min.js"></script>

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

It will interpret the \n in the translation JSON as a linebreak!!!

like image 5
Philip König Avatar answered Oct 23 '22 07:10

Philip König


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

like image 4
AmirSalar Avatar answered Oct 23 '22 05:10

AmirSalar