首页 > 解决方案 > 从 Angular 2+ 中的组件翻译文本

问题描述

我想在 Angular 2+ 应用程序中翻译来自 .ts 组件的文本。

现在我已经使用官方指南 ( https://angular.io/guide/i18n ) 翻译了 HTML 模板中的文本,但找不到从 .ts 组件翻译文本的方法。

例如,我想翻译页面的标题

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

有什么办法可以很好地做到这一点?

标签: angularinternationalization

解决方案


对于角度翻译,请使用http://www.ngx-translate.com/ 此。这是一个很棒的本地化插件。在您的目录中制作 i18n json 文件并轻松工作。

检查这个 plunkerhttps://plnkr.co/edit/WccVZSBM0rUgq2sXSUbe?preview


推荐阅读