从 Angular 9 开始,本地化过程发生了变化。查看official doc。
按照以下步骤操作:
- 如果还没有本地化包,请添加:
ng add @angular/localize
- 正如文档中所说:
Angular 存储库包含常见的语言环境。您可以通过在应用的工作区配置文件 (angular.json) 的 sourceLocale 字段中设置源语言环境来更改应用的源语言环境。构建过程(在本指南中将翻译合并到应用程序中进行了描述)使用应用程序的 angular.json 文件来自动设置 LOCALE_ID 令牌并加载语言环境数据。
像这样在angular.json 中设置语言环境(可以找到可用语言环境的列表here):
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"test-app": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"i18n": {
"sourceLocale": "es"
},
....
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
...
"configurations": {
"production": {
...
},
"ru": {
"localize": ["ru"]
},
"es": {
"localize": ["es"]
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "test-app:build"
},
"configurations": {
"production": {
"browserTarget": "test-app:build:production"
},
"ru":{
"browserTarget": "test-app:build:ru"
},
"es": {
"browserTarget": "test-app:build:es"
}
}
},
...
}
},
...
"defaultProject": "test-app"
}
基本上,您需要在i18n 部分定义sourceLocale,并添加具有特定语言环境的构建配置,例如"localize": ["es"]。您可以选择添加它,以便serve 部分
- 使用
build 或serve 构建具有特定语言环境的应用程序:ng serve --configuration=es