I would like to use a variable in my translation but I don't know how to do it in my template.
In my HTML template :
<md-input-container class="md-block">
<md-autocomplete md-no-cache="vm.noCache"
md-search-text="user.searchUser"
md-items="userItem in vm.querySearch(user.searchUser)"
md-item-text="userItem.display"
md-selected-item-change="vm.selectedItemChange(userItem, user)"
md-min-length="0"
required>
<md-item-template>
<span md-highlight-text="user.searchUser" md-highlight-flags="^i">{{userItem.display}}</span>
</md-item-template>
<md-not-found>
{{ 'user.autocomplete.no_user' | translate:'{ username: user.searchUser }' }}
</md-not-found>
</md-autocomplete>
</md-input-container>
and my translation file (json) :
"user": {
"autocomplete": {
"no_user" : "No user has been found ({{username}})",
}
}
How can I use user.searchUser to show that message ?
Thank very much !
Here the answer to do it directly in the HTML template :
{{ 'user.autocomplete.no_user' | translate:{ username: user.searchUser } }}
This is how you can do it in Angular 5+
translation file:
{
"HELLO": "Hello {user}"
}
Template:
{ 'HELLO' | translate: { user: "World" } }
Displays: Hello World
You can create an object containing all your translation parameters and use it for the translation like this :
{{ 'user.autocomplete.no_user' | translate:translationParameters }}
Look at this plunker how I did it, for more details it's explain at the end of the section Variable replacement in translate filter of Angular Translate doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With