[英]Angular ngx-translate - How to set a default value for missing param in translation value
I wish to have a default value for expected parameter in translation value, in case the programmer doesn't providing one.我希望翻译值中的预期参数有一个默认值,以防程序员不提供。
For Example, for this key in en.json
:例如,对于
en.json
这个键:
"NoRecordsWereFound": "No matching {{records}} were found for your search.",
In the HTML, when providing the records
parameters, like here:在 HTML 中,当提供
records
参数时,如下所示:
<span>
{{('NoRecordsWereFound' | translate:{ records: 'books' })}}
</span>
I'll get in the Browser:我将进入浏览器:
No matching books were found for your search.没有找到符合您搜索条件的图书。
But in case of not providing the parameter, like here:但是如果不提供参数,就像这里:
<span>
{{'NoRecordsWereFound' | translate}}
</span>
Instead of getting this: No matching {{records}} were found for your search.而不是得到这个:没有找到匹配的{{records}}为您的搜索找到。
I'll get some default value for it, like:我会得到一些默认值,比如:
No matching defaultValue were found for your search.没有为您的搜索找到匹配的defaultValue 。
Is it possible?是否可以? Thanks!
谢谢!
Ngx-translate does not provide such a feature, but you have plenty of options in Angular to achive that. Ngx-translate 没有提供这样的功能,但你在 Angular 中有很多选择来实现这一点。 Here are some proposals:
以下是一些建议:
You can use *ngIf to reference to a different translation:您可以使用 *ngIf 来引用不同的翻译:
<span *ngIf="books">
{{('NoRecordsWereFound' | translate:{ records: books })}}
</span>
<span *ngIf="!books">
{{('NoRecordsWereFound Default' | translate )}}
</span>
You can always set a default value on init of the component or if you did some method call, you can set a default value for erroneous responses.您始终可以在组件的 init 上设置默认值,或者如果您进行了某些方法调用,则可以为错误响应设置默认值。
@Component({
selector: 'my-app',
template: `
<span>
{{('NoRecordsWereFound' | translate:{ records: books })}}
</span>
`,
})
export class AppComponent {
books = 'default value';
}
You can also use the store to set default states for different scenarios.您还可以使用商店为不同的场景设置默认状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.