Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular-Translations: display translation for dynamically found translation key

My angular controller may generate the messages, that I intent to translate using angular-translations.

In controller I currently assign a variable to translation key, such as:

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

where that key has its translation specified as

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

As you can see, I also need to substutute projectName in translation.

I tried something like this in my view

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

but it does not work. How I can translate dynamically-found translation key and also add a scope variable into the translated line?

like image 886
onkami Avatar asked Mar 07 '14 14:03

onkami


1 Answers

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

Later use $translate like this for dynamic key:

$translate(translation_key)

Scope variables in your message can be used like below:

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

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

like image 131
Monika Avatar answered Sep 24 '22 19:09

Monika