2015-11-17 95 views
7

從角翻譯文檔的目的:

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

什麼是interpolationId參數的目的,可你也舉個例子?

回答

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