Search code examples
javascriptangularangular-translate

I need to translate a dynamic variable in angular-translate


Suppose this is the provided json file for the translations.

{
  "TRANSLATION_ID": "{{username}} is logged in.",
  "ANOTHER_ID": "PascalPrecht"
}

And this is the js code:

let expressionToTranslate = "TRANSLATION_ID"
let username = "ANOTHER_ID"

If we do the following:

{{expressionToTranslate | translate:username}}

We will get "ANOTHER_ID is logged in".

How is it possible to actually translate the variable in order to be able to have "PascalPrecht is logged in"?


Solution

  • For anyone interested in solving this problem, I solved it with the following:

    inside the controller/component I used

     $translate(username).then((paragraph)=>{
     username = paragraph
    });
    

    Now username became 'PascalPrecht'. And then rendered

    {{expressionToTranslate | translate:{username: username}}}
    

    in the view.