Angular -Translations: display translation for dynamically found translation key

My angular controller can generate messages that I intend to translate using angular translations.

In the controller, I currently assign a variable to the broadcast key , for example:

$scope.info = "core.projectconfig.created"; 

where this key has its own translation, indicated as

  core.projectconfig.created <=> 'Project {{projectName}} created successfully' 

As you can see, I also need to replace the project name in the translation.

I tried something like this in my opinion

  <P translate="{{info}}", translate-values="{projectName : projectData.ProjectName}"></p> 

but that will not work. How can I translate a dynamically found translation key, and also add a scope variable to the translated string?

+7
javascript angularjs angular-translate
source share
1 answer

You can enter the $ translate directive, which is provided by https://github.com/angular-translate/angular-translate in your controller.

Later use $ translate for the dynamic key:

 $translate(translation_key) 

Your message may use scope variables, as shown below:

 $translate(translation_key, { scope_variable_key: $scope_value }) 

for example: $translate("core.projectconfig.created", { projectName: $scope.projectData. ProjectName })

+3
source share

All Articles