angular - 从 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');
}
}
有什么办法可以很好地做到这一点?
解决方案
对于角度翻译,请使用http://www.ngx-translate.com/
此。这是一个很棒的本地化插件。在您的目录中制作 i18n json 文件并轻松工作。
检查这个 plunkerhttps://plnkr.co/edit/WccVZSBM0rUgq2sXSUbe?preview
推荐阅读
- ckeditor - TYPO3 电子邮件垃圾邮件防护
- api - 用于获取总 NiFi 排队数据的 API 或其他可查询源
- mysql - 如何使用nodejs将查询和json数据保存到sql server
- java - 如何以编程方式为 Java 中 AWS Cognito 用户池中的已登录用户启用或禁用 MFA?
- spring - 我想知道即使所有 hazelcast 集群都关闭了,我们如何设置客户端继续工作?
- jquery - 确认注销事件无法正常工作
- ruby-on-rails - 为在 Rails 中创建和更新 api 编写小测试
- python - 在 Flask 中生成 pdf 图
- perl - 在 perl 中的文件中创建一个列表
- laravel - 如何在 Laravel 中转换 Mode::find(id) 响应?