问题描述
Angular i18n docs 清楚地说明了本地化应用程序,但并未提及图书馆。
ng add @angular/localize
不修改 angular.json
,ng extract-i18n
失败并出现以下错误:
angular.json
An unhandled exception occurred: No projects support the 'extract-i18n' target.
在 extract-i18n
中添加 angular.json
目标
Data path "" should have required property 'browserTarget'.
解决方法
截至 2021 年 3 月,这是 not yet supported。
但是有 a workaround :
首先像安装普通应用一样安装 @angular/localize
:
ng add @angular/localize
在 package.json
中,添加以下脚本以扫描构建文件中的本地化字符串:
"i18n": "localize-extract -r . -s \"dist/efp-widgets/fesm2015/*.js\" -f xlf -o messages.xlf"
注意 localize-extract
来自 @angular/localize
;
如果在 TypeScript 中使用 $localize
,不要忘记在 @angular/localize/init
中注册 public-api.ts
:
import '@angular/localize/init'
然后将 i18n
脚本添加到 package.json
中的主构建脚本:
"build": "ng build && npm run i18n"