angularjs – angular $translate中interpolationId的用途是什么

前端之家收集整理的这篇文章主要介绍了angularjs – angular $translate中interpolationId的用途是什么前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
来自Angular翻译文档:

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

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

What is the purpose of interpolationId parameter…

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

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

…can you also give 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;
  }
]);

猜你在找的Angularjs相关文章