首页 > 解决方案 > Angular 9:TypeScript 中的 i18n

问题描述

我研究了 Angular 9 中的新 i18n 功能。https://angular.io/guide/i18n

如何翻译 TypeScript 中的文本,例如 SnackBar 消息?

标签: angularangular-materialinternationalizationangular9

解决方案


检查此博客https://blog.ninja-squad.com/2019/12/10/angular-localize/

来自官方文档:https ://angular.io/api/localize/init/$localize

@Component({
  template: '{{ title }}'
})
export class HomeComponent {
  title = $localize`You have 10 users`;
}

您必须手动将其添加到您的 messages.fr.xlf

<trans-unit id="6480943972743237078">
  <source>You have 10 users</source>
  <target>Vous avez 10 utilisateurs</target>
</trans-unit>

不要忘记重新服务您的角度应用程序。

更新ID

@Component({
  template: '{{ title }}'
})
export class HomeComponent {
  title = $localize`:@@6480943972743237078:`;
}

https://github.com/angular/angular/blob/252966bcca91ea4deb0e52f1f1d0d3f103f84ccd/packages/localize/init/index.ts#L31


推荐阅读