Angular 翻译服务,在嵌套中插入参数 json
Angular translate service, interpolate params in nested json
在 angular 翻译服务中,在正常翻译 json 中插入参数效果很好。但是在嵌套 json 中,插值参数不起作用。
我的JSON:
"SampleField": {
"SampleValidation": {
"MIN": "Value should not be less than {{min}}",
"MAX": "Value should not be more than {{max}}",
}
}
我的Angular代码:
ngOnInit(): void {
this.translateService.get('SampleField.Validation', {
// using hard coded value just as a sample
min: 0, max: 2000
}).subscribe(translation => {
console.log(translation);
});
}
预期输出:
{
MIN: "Value should not be less than 0",
MAX: "Value should not be greater than 2000"
}
实际输出:
{
MIN: "Value should not be less than {{min}}",
MAX: "Value should not be greater than {{max}}"
}
根据 source of ngx-translate 插值仅适用于字符串:
export abstract class TranslateParser {
/**
* Interpolates a string to replace parameters
* "This is a {{ key }}" ==> "This is a value", with params = { key: "value" }
* @param expr
* @param params
* @returns {string}
*/
abstract interpolate(expr: string | Function, params?: any): string;
这意味着您可能需要使用键数组而不是非叶元素:
this.translateService.get([
'SampleField.Validation.MIN',
'SampleField.Validation.MAX'
], {
// using hard coded value just as a sample
min: 0, max: 2000
}).subscribe(translation => {
console.log(translation);
});
您也可以使用 translate
管道来执行此操作,这样您就可以删除对组件的附加服务依赖性。
<p *ngIf="!item?.isRemovable">
{{'i18n.dashboard.heading'
| translate:{'TEXTTOFIND': 'STRINGTOREPLACE'} }}
</p>
只需确保您的密钥 i18n.test.key
已插入此 TEXTTOFIND
。如下所示。
"heading": "This is with the interpolated {{TEXTTOFIND}} text."
请注意标题字符串中的 {{}}
,STRINGTOREPLACE
可以是您想要的任何内容。
在 angular 翻译服务中,在正常翻译 json 中插入参数效果很好。但是在嵌套 json 中,插值参数不起作用。
我的JSON:
"SampleField": {
"SampleValidation": {
"MIN": "Value should not be less than {{min}}",
"MAX": "Value should not be more than {{max}}",
}
}
我的Angular代码:
ngOnInit(): void {
this.translateService.get('SampleField.Validation', {
// using hard coded value just as a sample
min: 0, max: 2000
}).subscribe(translation => {
console.log(translation);
});
}
预期输出:
{
MIN: "Value should not be less than 0",
MAX: "Value should not be greater than 2000"
}
实际输出:
{
MIN: "Value should not be less than {{min}}",
MAX: "Value should not be greater than {{max}}"
}
根据 source of ngx-translate 插值仅适用于字符串:
export abstract class TranslateParser {
/**
* Interpolates a string to replace parameters
* "This is a {{ key }}" ==> "This is a value", with params = { key: "value" }
* @param expr
* @param params
* @returns {string}
*/
abstract interpolate(expr: string | Function, params?: any): string;
这意味着您可能需要使用键数组而不是非叶元素:
this.translateService.get([
'SampleField.Validation.MIN',
'SampleField.Validation.MAX'
], {
// using hard coded value just as a sample
min: 0, max: 2000
}).subscribe(translation => {
console.log(translation);
});
您也可以使用 translate
管道来执行此操作,这样您就可以删除对组件的附加服务依赖性。
<p *ngIf="!item?.isRemovable">
{{'i18n.dashboard.heading'
| translate:{'TEXTTOFIND': 'STRINGTOREPLACE'} }}
</p>
只需确保您的密钥 i18n.test.key
已插入此 TEXTTOFIND
。如下所示。
"heading": "This is with the interpolated {{TEXTTOFIND}} text."
请注意标题字符串中的 {{}}
,STRINGTOREPLACE
可以是您想要的任何内容。