Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular translate variable replacement

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 !

like image 203
Joe Allen Avatar asked May 09 '17 15:05

Joe Allen


3 Answers

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

{{ 'user.autocomplete.no_user' | translate:{ username: user.searchUser } }}
like image 198
Joe Allen Avatar answered Nov 14 '22 12:11

Joe Allen


This is how you can do it in Angular 5+

translation file:

{
  "HELLO": "Hello {user}"
}

Template:

{ 'HELLO' | translate: { user: "World" } }

Displays: Hello World

like image 24
Sebastian Viereck Avatar answered Nov 14 '22 13:11

Sebastian Viereck


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

like image 2
JeanJacques Avatar answered Nov 14 '22 12:11

JeanJacques