Angular translate provider - Is there a way to replace shared variables?

In my application, many of my translation strings should include a username. For example: Hello {{user_name}}! (The username must be part of the translation string, since the position of the name in the string depends on the language))

Attribute setting method: {{"TRANSLATE_ID" | translate:{user_name:myUserName}}} {{"TRANSLATE_ID" | translate:{user_name:myUserName}}}

Since {{user_name}} appears in more than 100 translation lines, I don’t want to send the user_name parameter so many times. I would like to have a way to set this parameter to only one.

I cannot replace the line {{user_name}} in config, because the username is set asynchronously (fetched from the server) and is not available when translateProvider sets the lines.

thanks

+5
source share
1 answer

AngularTranslate allows you to translate tags from html itself or inside a controller through a promise. I believe that the elegant way to do what you want is to create a method / function in your controller that processes the translation the way you want, and then call that method / function in your html, passing the username as a parameter.

Specify the username in your JSON translation files as "USERNAME", for example, "Hello, USERNAME!", Translate the method created on the controller using AngularTranslate, and then replace the string by replacing the word USERNAME with the method parameter (real username ) and use this line as the return value of the method that will be called in your html.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1271691/


All Articles