文档
初始设置
首先将 Angular 的本地化包添加到您的项目中:
ng add @angular/localize
该命令将进行以下更改:
- 将@angular/localize包添加到您的依赖项中
- 将“@angular/localize/init”添加到angular.json中的polyfills数组中
- 将以下引用添加到您的 main.ts 中:
/// <reference types="@angular/localize" />
它还会将 extract-i18n 配置添加到 angular.json 中:
"extract-i18n": {
"builder": "ng-extract-i18n-merge:ng-extract-i18n-merge",
"options": {
"buildTarget": "your-app:build",
"format": "xlf",
"outputPath": "src/locale",
"includeContext": true,
"resetTranslationState": true,
"targetFiles": [
"messages.fr.xlf",
"messages.vi.xlf"
]
}
}
注意:我们将在配置中稍后修改 outputPath 以匹配我们的工作流程。
Angular i18n CLI Tool
To simplify the management of Angular i18n configuration, you can use our CLI tool:
npm install --save-dev @softwarity/angular-i18n-cli
This tool provides several features to streamline your i18n workflow:
- Easy initialization of i18n configuration in your Angular project
- Simple addition and removal of locales
- Automatic configuration of build settings for each locale
- Proper setup of XLIFF file formats
安装翻译工具
用于管理翻译文件,我们将使用 ng-extract-i18n-merge,它简化了提取和合并翻译的过程:
npm install --save-dev ng-extract-i18n-merge
此工具提供了几个优势:
- 自动提取您应用程序中的可翻译字符串
- 将新翻译与现有翻译合并为一步完成
- 在更新过程中保留现有翻译
- 支持多种目标语言
配置文件
package.json
"scripts": {
...
"extract-i18n": "ng extract-i18n",
"build": "ng build --localize"
...
}
angular.json
在 angular.json 中更新 i18n 配置:
"i18n": {
"sourceLocale": "en",
"locales": {
"fr": {
"translation": "src/locale/messages.fr.xlf"
},
"vi": {
"translation": "src/locale/messages.vi.xlf"
}
}
},
将标准的 extract-i18n 配置替换为 ng-extract-i18n-merge:
"extract-i18n": {
"builder": "ng-extract-i18n-merge:ng-extract-i18n-merge",
"options": {
"buildTarget": "your-app:build",
"format": "xlf",
"outputPath": "src/locale",
"includeContext": true,
"resetTranslationState": true,
"targetFiles": [
"messages.fr.xlf",
"messages.vi.xlf"
]
}
}
重要配置选项:
- 构建目标: 您的应用程序的构建目标名称
- 输出路径: 翻译文件将存储的目录
- 包含上下文:在XLIFF文件中包含源文件信息
- 重置翻译状态:标记所有翻译为在更改时需要审核
- 目标文件:要生成/更新的翻译文件列表
Build configurations
在构建部分为每种语言添加配置:
"architect": {
"build": {
"configurations": {
"fr": {
"localize": ["fr"]
},
"vi": {
"localize": ["vi"]
}
}
}
}
准备组件以进行翻译
为了使您的组件可以进行翻译,您需要标记需要翻译的文本。Angular 提供了两种方法来实现这一点:
Angular 文档:标记文本以进行翻译
了解有关如何为翻译准备您的应用程序的更多信息。
模板翻译
在您的模板中使用 i18n 属性来标记需要翻译的文本:
<tag attribute="This is a sample attribute text that needs translation."
i18n-attribute="@@ATTRIBUTE_TEXT"
i18n="@@TAG_TEXT">This is a sample text that needs translation.</tag>
// EXAMPLE
<button title="Title button" i18n-title="@@TITLE_BUTTON"i18n="@@LABEL_BUTTON">label button</button>
@@prefix定义了翻译的自定义ID。建议使用SNAKE_CASE以保持一致性。
组件翻译
在您的组件代码中,对于文本,请使用 $localize 模板标签:
import { Component } from '@angular/core';
@Component({
selector: 'app-example',
template: '...'
})
export class ExampleComponent {
title = $localize`:@@PAGE_TITLE:Welcome to our application`;
errorMessage = $localize`:@@ERROR_MESSAGE:An error occurred while processing your request`;
}
构建和提供
构建带翻译的应用程序
构建您的应用程序,使其包含所有翻译:
npm run build
或者为特定语言构建(以法语为例):
ng build --configuration=fr
开发服务器
在开发期间使用翻译为您的应用程序提供服务:
ng serve --configuration=fr
⚠️ 重要提示
在添加新的可翻译内容或修改现有翻译之后:
ng extract-i18n
- 一次性提取和更新所有翻译文件
这个简化的工作流程确保您的翻译文件始终与源内容保持最新,从而使国际化过程更加高效且无错误。