0

I am using internationalization [i18n] in my angular project. For static content it is working properly. But dynamic content it is not working.

My code is as follows:

Static:

     <div>{{ 'ADD ENTRY' | translate }} </div>

Dynamic:

    <div>{{ status | translate }} </div>

I am getting status code from the back end API, that I am mapping to the variable status in the ts file. In my case I am getting status code as 404. So I reconfigured that 404 in i18n configuration file. But it is not working if I try to translate that variable.But instead of passing that status variable, if I directly pass '404' as string then the internationalization working properly.

My code is as follows:

    <div class="error" *ngIf="status">**{{ '404' | translate }} **</div>

for this internationalization working.

But I need to internationalize the variable status, but facing issue.

My code is as follows:

In HTML:

  <div class="error" *ngIf="status">{{ status | translate }} </div>

In ts:

    status: string;
    this.status = error.status;

I also tried as:

        <div class="error" *ngIf="status">{{"'"+{{status}}+"'"}} | translate}}</div>

But it is not working.

James Z
  • 12,209
  • 10
  • 24
  • 44
user8030367
  • 81
  • 4
  • 18

1 Answers1

0

Try translating that dynamic text in ts file this way:

First set TranslateService:

import { TranslateService } from '@ngx-translate/core';

constructor( private translateService: TranslateService ) { }

and then, in a place where you'll recive the text (which in this case, I assume, is stored in error.status) do this:

this.status = this.translateService.instant(error.status)

and don't use | translate in html then, just put status there.

pbialy
  • 1,025
  • 14
  • 26