angular - Angular 9 $localize 和 xi18n:“元数据中不支持标记的模板表达式”
问题描述
我最新的 Angular 项目将以四种语言发布。因此,我准备了要包含的应用程序,@angular/localize
如下所述:
设置
Angular: 9.0.7
... animations, cli, common, compiler, compiler-cli, core
... elements, forms, language-service, localize
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.7
@angular-devkit/build-angular 0.900.7
@angular-devkit/build-optimizer 0.900.7
@angular-devkit/build-webpack 0.900.7
@angular-devkit/core 9.0.7
@angular-devkit/schematics 9.0.7
@angular/cdk 9.1.3
@angular/google-maps 9.1.3
@ngtools/webpack 9.0.7
@schematics/angular 9.0.7
@schematics/update 0.900.7
rxjs 6.5.4
typescript 3.7.5
webpack 4.41.2
角.json
// ...
"projects": {
// ...
"my-project": {
"i18n": {
"sourceLocale": "de-DE",
"locales": {
"fr": {
"translation": "messages.fr.xlf",
"baseHref": "/fr/"
},
"it": {
"translation": "messages.it.xlf",
"baseHref": "/it/"
},
"en": {
"translation": "messages.en.xlf",
"baseHref": "/en/"
}
}
// ...
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"allowJs": true,
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
为了翻译代码中的字符串(例如路由路径),我使用了新函数$localize
,例如:
// ...
export class Translations {
static paths: TranslatedPathsInterface = {
// App Routing
home: $localize`startseite`,
team: $localize`mitarbeiter`,
about: $localize`ueber-uns`
// ...
};
}
当使用ng serve
. 当我想提取标记的字符串以ng xi18n
进行翻译时,就会出现问题。在控制台中,同样的错误出现了好几次:
ERROR in src/app/shared/translations.ts(39,15): Error during template compile of Translations
Tagged template expressions are not supported in metadata.
这基本上告诉我编译器不喜欢这个$localize
函数。
我更新了所有软件包。但是现在,我很茫然。
任何提示将不胜感激!我还能尝试什么?
2020-03-23 更新
我做了一些更多的研究,发现这个错误只有在与路由路径定义结合使用时才会发生。任何其他翻译都可以正常工作。
所以这有效:
const something = $localize`etwas`;
这不起作用:
const routes: Routes = [
{
path: '',
redirectTo: $localize`startseite`,
pathMatch: 'full'
},
{
path: $localize`startseite`,
component: HomeComponent
}
// ...
];
@NgModule( {
imports: [ RouterModule.forRoot( routes, routerOptions ) ],
exports: [ RouterModule ]
} )
// ...
解决方案
我找到了解决此问题的方法。看起来如果你用函数编译器包装你的翻译不会抛出错误。例子:
const getTranslation = () => {
return $localize`:@@title.tasks:Tasks`;
};
export const taskRoutes: Routes = [
{
path: '',
component: ListTaskComponent,
data: {
breadcrumb: 'List',
title: getTranslation(),
status: true,
description: 'List of tasks',
}
}
];
似乎这是@angular/localize
包中的一个问题,但在他们修复之前,可以通过这种方式完成
另一种解决方案可能是从 licalization 编译中跳过 ts 文件,但我在ng i18n
推荐阅读
- rust - 错误消息不清楚:返回引用当前函数拥有的数据的值
- swift - Swift - 从第二个函数调用完成处理程序
- python - 多键按下在 python selenium 中不起作用
- functional-programming - 是否有相当于 Python 的 itertools.accumulate 的 Elixir 函数?
- python - 在 pandas 数据框中找到列值位于列表中的行的索引
- java - 无法解决 github maven 依赖
- c++ - 使用 LinkedList 类指向头节点与仅使用 Node* 的内在好处
- javascript - 如何在这些组件之间添加正常空间?
- xamarin.forms - 在 Forms UWP 中,自定义标题视图中的内容视图的后退导航无法与自定义渲染器一起正常工作
- r - 对所有 r 个用户,Logistic 回归模型