我正在尝试在我的应用程序中实现angular-translate。 所以,我发现以下JSFiddle应该演示angular-translate,但是我收到以下错误消息:
'undefined'不是函数(评估'$ translate.uses(key)')
var myApp = angular.module('myApp', ['pascalprecht.translate']);
myApp.config(function($translateProvider) {
$translateProvider.preferredLanguage('fr');
$translateProvider.translations('en', {
MONDAY: 'Monday',
TUESDAY: 'Tuesday',
WEDNESDAY: 'Wednesday',
THURSDAY: 'Thursday',
FRIDAY: 'Friday',
SATURDAY: 'Saturday',
SUNDAY: 'Sunday'
});
$translateProvider.translations('fr', {
MONDAY: 'Lundi',
TUESDAY: 'Mardi',
WEDNESDAY: 'Mercredi',
THURSDAY: 'Jeudi',
FRIDAY: 'Vendredi',
SATURDAY: 'Samedi',
SUNDAY: 'Dimanche'
});
});
myApp.constant('weekDays', [{
id: 1,
key: 'MONDAY'
}, {
id: 2,
key: 'TUESDAY'
}, {
id: 3,
key: 'WEDNESDAY'
}, {
id: 4,
key: 'THURSDAY'
}, {
id: 5,
key: 'FRIDAY'
}, {
id: 6,
key: 'SATURDAY'
}, {
id: 7,
key: 'SUNDAY'
}]);
myApp.controller('MyCtrl', ['$scope', '$translate', 'weekDays',
function($scope, $translate, weekDays) {
$scope.changeLanguage = function(key) {
$translate.uses(key)
}
$scope.days = weekDays;
$scope.name = 'user';
console.log($scope.days);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="http://rawgithub.com/PascalPrecht/bower-angular-translate/master/angular-translate.min.js"></script>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<div>
<button ng-click="changeLanguage('fr')">fr</button>
<button ng-click="changeLanguage('en')">en</button>
</div>
<ul>
<li ng-repeat="day in days">
{{ day.key | translate }}
</li>
</ul>
</div>
</body>
知道为什么会这样吗?
答案 0 :(得分:4)
$translate.uses(key)
已弃用,请改用$translate.use(key)
。
详细了解angular-translate Multi Language和2.0.0 upgrade notification(重大变化)。
var myApp = angular.module('myApp', ['pascalprecht.translate']);
myApp.config(function($translateProvider) {
$translateProvider.preferredLanguage('fr');
$translateProvider.translations('en', {
MONDAY: 'Monday',
TUESDAY: 'Tuesday',
WEDNESDAY: 'Wednesday',
THURSDAY: 'Thursday',
FRIDAY: 'Friday',
SATURDAY: 'Saturday',
SUNDAY: 'Sunday'
});
$translateProvider.translations('fr', {
MONDAY: 'Lundi',
TUESDAY: 'Mardi',
WEDNESDAY: 'Mercredi',
THURSDAY: 'Jeudi',
FRIDAY: 'Vendredi',
SATURDAY: 'Samedi',
SUNDAY: 'Dimanche'
});
});
myApp.constant('weekDays', [{
id: 1,
key: 'MONDAY'
}, {
id: 2,
key: 'TUESDAY'
}, {
id: 3,
key: 'WEDNESDAY'
}, {
id: 4,
key: 'THURSDAY'
}, {
id: 5,
key: 'FRIDAY'
}, {
id: 6,
key: 'SATURDAY'
}, {
id: 7,
key: 'SUNDAY'
}]);
myApp.controller('MyCtrl', ['$scope', '$translate', 'weekDays',
function($scope, $translate, weekDays) {
$scope.changeLanguage = function(key) {
$translate.use(key)
}
$scope.days = weekDays;
$scope.name = 'user';
console.log($scope.days);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="http://rawgithub.com/PascalPrecht/bower-angular-translate/master/angular-translate.min.js"></script>
<body ng-app="myApp">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<div>
<button ng-click="changeLanguage('fr')">fr</button>
<button ng-click="changeLanguage('en')">en</button>
</div>
<ul>
<li ng-repeat="day in days">
{{ day.key | translate }}
</li>
</ul>
</div>
</body>
答案 1 :(得分:1)
我认为你的意思是$ translate.use(key)而不是$ translate.uses(key)。