Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ng2-translate with parameters

I tried to use ng2-translate with parameters in the template:

{{ test | translation:{value:param} }}

And it works perfectly. I would like to build my translation in my typescript and I saw this function:

get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>

But I don't know how to use it.

Do you guys have any example ?

My case is:

en.json:

{ "test":"the level of {value1} and {value2} is low."}

typescript:

let message:string = "";
let parametres = ["1", "2"];
this._translateService.get("test", parametres).subscribe((res:string) => {
    message += res;
});

I would like to have: The level of 1 and 2 is low.

Thanks a lot.

like image 314
Julien Moreau Avatar asked Nov 02 '16 20:11

Julien Moreau


2 Answers

In en_US.json file, follow the below format to include x and y (which will be our parameters that we want to replace with numbers.

{"Showing": "Showing {{x}} of {{y}}"}

In component.ts, min,max will be the variables that we populate.

  min: number;
  max: number;

and following will be part of component.html

<span> {{ 'Showing' | translate:{x: min, y: max} }} </span>
like image 155
rt1989 Avatar answered Sep 21 '22 10:09

rt1989


It works when you provide key/values as parameters instead of an array.

typescript:

let message:string = "";
let parametres = {value1: "1", value2: "2"};
this._translateService.get("test", parametres).subscribe((res:string) => {
    message += res;
});

Also according to the docs you should declare the placeholders with double curly braces.

en.json:

{ "test": "the level of {{value1}} and {{value2}} is low." }
like image 44
Anton Biller Avatar answered Sep 19 '22 10:09

Anton Biller