0
votes

I want to translate text from the .ts components in a Angular 2+ application.

Right now I've already translated text from the HTML template using the official guide (https://angular.io/guide/i18n) but can't find a way to translate text from the .ts component.

For example, I would like to translate the title of the page

import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.css']
})
export class ExampleComponent implements OnInit {

  constructor(private title: Title) {}

  ngOnInit() {
    this.title.setTitle('I would like to have this title translated');
  }
}

Any way to achieve that nicely?

1

1 Answers

0
votes

For angular translate use http://www.ngx-translate.com/ this. This is a great plugin for localization. Made the i18n json file in your directory and work easily.

Check this plunker https://plnkr.co/edit/WccVZSBM0rUgq2sXSUbe?preview