Using Angular Translate inside Controller

It’s not really Ionic related but it’s been driving me crazy for half a day already.

How can I access Angular Translate strings from inside my controller?

From what I can find in the docs, I could inject $translate and do something like $translate('AWESOME_STRING');
But I want to use three separate translations in, in my case, an alert: message, title and button label.

I read that $translate should return a promise which in turn I could use to do something like:

$q
    .all([$translate('ALERT.TITLE'), $translate('ALERT.MESSAGE'), $translate('ALERT.BUTTON')])
    .then(function (translations) {
        // Array of translations can be used here in an alert
    });

But that all ain’t working so far. What am I doing wrong? Does anyone have some experience with Angular Translate?

Did you try to use $filter(‘translate’)(‘ALERT.TITLE’) ?

2 Likes

From the website http://angular-translate.github.io/docs/#/guide/03_using-translate-service

app.controller('Ctrl', ['$scope', '$translate', function ($scope, $translate) {
  $translate(['HEADLINE', 'PARAGRAPH', 'NAMESPACE.PARAGRAPH']).then(function (translations) {
    $scope.headline = translations.HEADLINE;
    $scope.paragraph = translations.PARAGRAPH;
    $scope.namespaced_paragraph = translations['NAMESPACE.PARAGRAPH'];
  });
}]);
1 Like

This worked for me, thanks!

1 Like

Glad it helped :smile:

Getting back at my old topic, I wrote an extensive answer on SO about this: