7

From Angular translate documentation:

$translate(translationId[, interpolateParams], interpolationId);

What is the purpose of interpolationId parameter, can you also give an example?

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Thịnh Phạm
  • 2,528
  • 5
  • 26
  • 39

1 Answers1

0

What is the purpose of interpolationId parameter...

The purpose of interpolationId is to reference the identifier returned by getInterpolationIdentifier of a custom interpolation that has been added. You can add a custom interpolation using $translateProvider.addInterpolation.

This is documented better in angular-translate's Pluralization documentation.

...can you also give an example?

Here's an example:

https://plnkr.co/edit/cDpNzZ6T6TSc13Qmji77

HTML

<body ng-controller="MyCtrl">
  $translate('greeting', {}, 'custom'): {{ customGreeting }}
  <br> $translate('greeting', {}): {{ regularGreeting }}
</body>

JavaScript

var app = angular.module('app', [
  'pascalprecht.translate'
]);

app.config(["$translateProvider",
  function($translateProvider){
    $translateProvider.translations('en',{
      "greeting" : "Welcome Dinesh" 
    });

    $translateProvider.preferredLanguage('en');

    $translateProvider.addInterpolation('customInterpolator');
  }
]);

app.controller('MyCtrl', [
  '$scope',
  '$translate',
  function ($scope, $translate) {
    $translate('greeting', {}, 'custom').then(function (result) {
      $scope.customGreeting = result;
    });
    $translate('greeting', {}).then(function (result) {
      $scope.regularGreeting = result;
    });
  }
]);

app.factory('customInterpolator', [
  function () {
    var customInterpolator = {};

    customInterpolator.$get = function () {
    };

    customInterpolator.setLocale = function (locale) {
    };

    customInterpolator.getInterpolationIdentifier = function () {
        return 'custom';
    };

    customInterpolator.interpolate = function (string, interpolateParams) {
        return 'My custom interpolation message';
    };

    return customInterpolator;
  }
]);
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97