user3679607
user3679607

Reputation: 163

AngularJS: How to use translation (i18n) in controller for popup?

I', writing an app in angularjs and have to do the i18n. Everything's fine but i did the errorhandling and there's the problem: i don't know how to use the translation in controller for popups.

My controller looks like this:

function showErrorPopup($ionicPopup, $ionicHistory, $location, $translate, error) {
    if (error.status == 404) {
         errorId = ...

         $ionicPopup.show({
              title: '{{ERROR.TITLE | translate}}></span>',
              template: '{{ERROR.errorId| translate}}',
              buttons: [{ text: 'OK' }]
         });   
    }
    else if (error.data.ExceptionMessage != null || error.data.ExceptionMessage != "" || error.data.ExceptionMessage != undefined) {
        errorId = ...

        $ionicPopup.show({
              title: '{{ERROR.TITLE | translate}}></span>',
              template: '{{ERROR.errorId| translate}}',
              buttons: [{ text: 'OK' }]
         });   
    }
    else {
        errorId = ...

        $ionicPopup.show({
              title: '{{ERROR.TITLE | translate}}></span>',
              template: '{{ERROR.errorId| translate}}',
              buttons: [{ text: 'OK' }]
         });   
    }
}

This is my controller. It depends what's the result, but i always want to show a popup with the error (title of the error = ERROR.TITLE, text of the error = ERROR.errorId).

The ERROR and his TITLE or his ID (like 100, 200...) are defined in a json file.

Can you help me to resolve this? If you have to know something more, just ask.

Upvotes: 2

Views: 1994

Answers (1)

Andrei Lesnitsky
Andrei Lesnitsky

Reputation: 1088

use $translate service

var translatedErrorTitle = $translate.instant(ERROR.TITLE);
var translatedErrorId    = $translate.instant(ERROR.errorId);

More info

Upvotes: 4

Related Questions