Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put new line in string to translate

I'm using ngx-translate.

How can I put a line break in a string to translate ?

In my template I have :

{{'STRING_TO_TRANSLATE' | translate}}

In my en.json:

{
"STRING_TO_TRANSLATE": "text on first line. <br> or \n don't work. Text on second line"
}
like image 703
Lev Avatar asked Aug 22 '17 13:08

Lev


3 Answers

You can use \n but you will have to provide some styling.

So in your template use this:

<div class="my-translated-paragraph">
    {{'STRING_TO_TRANSLATE' | translate}}
</div>

Your en.json:

{
"STRING_TO_TRANSLATE": "text on first line.\nText on second line"
}

Your (s)CSS file:

.my-translated-paragraph{
    white-space: pre-wrap;
}

More info an the magic behind white-space: https://stackoverflow.com/a/42354356/3757110

See also a github Issue about this: https://github.com/angular-translate/angular-translate/issues/595

like image 181
jvoigt Avatar answered Nov 08 '22 16:11

jvoigt


It works! But instead of

{{'STRING_TO_TRANSLATE' | translate}}

You should do

<div [innerHTML]="'STRING_TO_TRANSLATE' | translate"></div>

<br/>s should work just fine, but in other cases you may need some additional 'safe html pipe', i.e:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({name: 'mySafeHtmlPipe'})
export class SafeHtmlPipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {
  }

  public transform(htmlContent) {
    return this.sanitizer.bypassSecurityTrustHtml(htmlContent);
  }
}
like image 36
Pijotrek Avatar answered Nov 08 '22 15:11

Pijotrek


This case can be solved one way or another, but IMHO the real solution, without any dirty tricks is:

.container {
    white-space: pre-line;
}

There is a significant difference between pre-wrap and pre-line. The pre-wrap will result in the browser preserving all the whitespaces and so you will get the first line indentation. The pre-line will collapse our whitespace sequence into a single whitespace and no indentation will occur.

You can read more on that in the MDN docs, actual link below:

https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

like image 5
Tyiliyra Avatar answered Nov 08 '22 15:11

Tyiliyra