最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular translate service, interpolate params in nested json - Stack Overflow

programmeradmin4浏览0评论

In angular translation service, interpolating params in normal translation json works well. But in nested json, interpolating params is not working.

My JSON:

  "SampleField": {
    "SampleValidation": {
      "MIN": "Value should not be less than {{min}}", 
      "MAX": "Value should not be more than {{max}}", 
    }
  }

My Angular Code:

ngOnInit(): void {
  this.translateService.get('SampleField.Validation', {
    // using hard coded value just as a sample
    min: 0, max: 2000
  }).subscribe(translation => {
    console.log(translation);
  });
}

Expected Output:

{
    MIN: "Value should not be less than 0",
    MAX: "Value should not be greater than 2000"
}

Actual Output:

{
    MIN: "Value should not be less than {{min}}",
    MAX: "Value should not be greater than {{max}}"
}

In angular translation service, interpolating params in normal translation json works well. But in nested json, interpolating params is not working.

My JSON:

  "SampleField": {
    "SampleValidation": {
      "MIN": "Value should not be less than {{min}}", 
      "MAX": "Value should not be more than {{max}}", 
    }
  }

My Angular Code:

ngOnInit(): void {
  this.translateService.get('SampleField.Validation', {
    // using hard coded value just as a sample
    min: 0, max: 2000
  }).subscribe(translation => {
    console.log(translation);
  });
}

Expected Output:

{
    MIN: "Value should not be less than 0",
    MAX: "Value should not be greater than 2000"
}

Actual Output:

{
    MIN: "Value should not be less than {{min}}",
    MAX: "Value should not be greater than {{max}}"
}
Share Improve this question edited Jan 29, 2021 at 16:24 Sibeesh Venu 21.8k17 gold badges115 silver badges152 bronze badges asked Sep 3, 2017 at 16:57 Aqeel AshiqAqeel Ashiq 2,1857 gold badges27 silver badges59 bronze badges 1
  • Just a typo issue ? SampleField.Validation => SampleField. SampleValidation – dpellier Commented Sep 4, 2017 at 9:20
Add a ment  | 

2 Answers 2

Reset to default 11

According to the source of ngx-translate interpolation works only on strings:

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 means you might need to use an array of keys instead of a non-leaf element:

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);
});

You can also do this by using the translate pipe, so that you can remove the additional service dependency to your ponent.

<p *ngIf="!item?.isRemovable">
{{'i18n.dashboard.heading' 
| translate:{'TEXTTOFIND': 'STRINGTOREPLACE'} }}
</p>

Just make sure that your key i18n.test.key has this TEXTTOFIND interpolated. Something like below.

"heading": "This is with the interpolated {{TEXTTOFIND}} text."

Note the {{}} in the heading string and the STRINGTOREPLACE can be anything you wish.

发布评论

评论列表(0)

  1. 暂无评论