k.v*_*ent 4 translation internationalization angular-i18n angular angular-dynamic-components
我正在使用一个输入字段组件,可以使用表单将其嵌入到不同的父组件中。
在输入子组件中,我有一个 i18n 翻译键作为使用插值的变量,我想根据客户的选择从父组件动态生成它。
输入.组件.ts:
<div i18n="{{labelTextKey}}">{{labelText}}</div>
<div>
<input matInput [required]="required"
[name]="name"
[(ngModel)]="value"
[type]="type">
</div>
Run Code Online (Sandbox Code Playgroud)
表单.组件.ts:
<app-input [labelText]="'Second name'"
[labelTextKey]="'@@LabelSecondName'"
[name]="'secondName'"
[ngModel] = "secondName"
[type] = "'text'"
</app-input>
Run Code Online (Sandbox Code Playgroud)
问题是,运行应用程序时,翻译发生在将密钥传递给子组件中的变量之前,因此 key/id: 没有翻译@@LabelSecondName
。
因此,labelText 保留原始语言。我得到的不是翻译,而是一种随机生成的数字,并且由于这些数字作为密钥不存在于XLF(版本 2.0)文件中,因此文本/标签不会被翻译。
错误信息:
Missing translation for message "8901569964118207331"
该行为在某种程度上是预期的,因为变量:没有及时labelTextKey
获得传递的值: 。@@LabelSecondName
一直在寻找,但无法找到正确的解决方案。这个问题似乎和我的比较接近,但又不完全一样,而且也没有答案。
问题已解决。解决方案:
子组件中不需要 i18n 标签,只需在父组件中使用它,如下所示:
<app-input i18n-labelText="@@LabelSecondName" labelText="Second name"></app-input>
Run Code Online (Sandbox Code Playgroud)
所以,整个代码将如下所示:
<app-input [labelText]="'Second name'"
i18n-labelText="@@LabelSecondName"
[name]="'secondName'"
[ngModel] = "secondName"
[type] = "'text'"
</app-input>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2349 次 |
最近记录: |