Search code examples
angularjsinternationalizationangular-translate

angular $translate.instant returns object instead of string


Im using "angular-translate": "^2.15.1" with $translateProvider.useSanitizeValueStrategy('sce')

Inside my controller, I need to pass a translated string value to the $http service.

But below code is returning me TrustedValueHolderType object instead of a string.

defaultMessage = translate.instant('welcome-message.default.message');

enter image description here

Bare minimum plunkr https://plnkr.co/edit/ZPtixjNub9kIExiuGIkp?p=preview Open console (clear all). Then click on DE or EN button and see the log


Solution

  • Yes, as mentioned in the comment, decodeURIComponent might solve it. But here's what is causing this in the first place:

    $translateProvider.useSanitizeValueStrategy('sce')
    

    this encodes your output for $translate globally.. which is why you could get it worked out using decodeURIComponent.

    Maybe you could use a different strategy. The angular-translate guide seems to suggest escape strategy for this.

    Here's a forked plunker that works with escape strategy and without needing to use decodeURIComponent