第一步是确保你已经在angular.json文件中针对您的语言设置了i18n。例如:
"i18n": {
"sourceLocale": "en-US",
"locales": {
"fr": "src/locale/messages.fr.xlf"
}
},
第二步是在你的模板中使用i18n指令,标记需要翻译的文本:
This text will be translated
第三步是在你的组件中导入$localize,并使用它来翻译类型化字符串。示例:
import { Component } from '@angular/core';
import { $localize } from '@angular/localize';
@Component({
selector: 'app-root',
template: `
{{ title }}
{{ message }}
`,
})
export class AppComponent {
title = $localize`Hello, world!`;
message = $localize`:@@greeting:Welcome to my app, {{ name }}!:{name} is the user's name`;
}
以上三个步骤将确保您的HTML和TypeScript字符串都能够获得正确的本地化翻译。