我試圖在我的ui-router配置文件routes.js中使用$ translate.use(),這樣我就可以切換切換語言狀態,但我得到一個
未捕獲的錯誤:[$注射器:modulerr]未能實例化模塊frontApp由於: 錯誤:[$注射器:unpr]未知提供商:$翻譯
這裏是我的文件。
'use strict';
(function() {
angular.module('frontApp')
.run(['$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}]
)
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider', '$translate', function($urlRouterProvider, $stateProvider, $locationProvider, $translate){
$urlRouterProvider
.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: '/views/home.html',
controller: 'HomeCtrl'
})
.state('home.rules', {
url: '^/rules',
templateUrl: '/views/rules.html'
})
.state('home.terms', {
url: '^/terms',
templateUrl: '/views/terms.html'
})
.state('home.faq', {
url: '^/faq',
templateUrl: '/views/faq.html'
})
.state('language', {
controller: function() {
console.log('ok');
$translate.use(($translate.use() === 'fr') ? 'en' : 'fr');
}
});
$locationProvider.html5Mode(true);
}]);
}());
我也有包含我的翻譯,當不試圖注入和使用$ translate.use()在上面routes.js一個translation.js文件文件我用它使用TranslateController沒有問題:
'use strict';
(function() {
angular.module('frontApp')
.config(['$translateProvider', function($translateProvider) {
$translateProvider
.translations('fr', {
language: 'English',
otherLanguage: 'en',
cssClass: 'french',
linkHome: 'Accueil',
linkRules: 'Règlement',
linkTerms: 'Termes et conditions',
linkFAQ: 'FAQ',
})
.translations('en', {
language: 'Français',
otherLanguage: 'fr',
cssClass: 'english',
linkHome: 'Home',
linkRules: 'Rules',
linkTerms: 'Terms and conditions',
linkFAQ: 'FAQ',
});
$translateProvider.preferredLanguage('fr');
}])
.controller('TranslateController', ['$translate', '$scope', '$location', function ($translate, $scope, $location) {
// The URLs need to be updated
var host = $location.host();
if (host === 'localhost' || host === '0.0.0.0') {
$translate.use('fr');
}
else if (host === 'localhost-en') {
$translate.use('en');
}
$scope.switchLanguage = function() {
$translate.use(($translate.use() === 'fr') ? 'en' : 'fr');
};
}]);
}());
這裏是app.js:
'use strict';
angular
.module('frontApp', [
'ui.router',
'ngAnimate',
'ngCookies',
'ngResource',
'ngSanitize',
'ngTouch',
'pascalprecht.translate'
]);
我缺少什麼?非常感謝您的時間和幫助。這裏
謝謝!其實我創建了一個LanguageSwitcher服務,並得到相同的錯誤。所以現在你的幫助能夠正確使用它。再次感謝! – Gab
很高興看到先生,享受可能UI-Router;)偉大的圖書館......;) –