7
從角翻譯文檔的目的:
$translate(translationId[, interpolateParams], interpolationId);
什麼是interpolationId
參數的目的,可你也舉個例子?
從角翻譯文檔的目的:
$translate(translationId[, interpolateParams], interpolationId);
什麼是interpolationId
參數的目的,可你也舉個例子?
什麼是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;
}
]);