angular $ translate中interpolationId的用途是什么?

时间:2015-11-17 01:33:12

标签: angularjs interpolation angular-translate

来自Angular翻译文档:

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

interpolationId参数的目的是什么,你能举个例子吗?

1 个答案:

答案 0 :(得分:0)

  

interpolationId参数的目的是什么......

interpolationId的目的是引用已添加的自定义插值的getInterpolationIdentifier返回的标识符。您可以使用$translateProvider.addInterpolation添加自定义插值。

这在angular-translate的Pluralization documentation中有更好的记录。

  

...你能举个例子吗?

以下是一个例子:

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;
  }
]);