Angular translate variable replacement

Joe Allen picture Joe Allen · May 9, 2017 · Viewed 16.3k times · Source

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 !

Answer

Joe Allen picture Joe Allen · May 9, 2017

Here the answer to do it directly in the HTML template :

{{ 'user.autocomplete.no_user' | translate:{ username: user.searchUser } }}