2

I'm trying to use $translate.use() in my ui-router config file routes.js so I can switch toggle language with a state but I get an

Uncaught Error: [$injector:modulerr] Failed to instantiate module frontApp due to: Error: [$injector:unpr] Unknown provider: $translate

Here is my file.

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

I also have a translation.js file containing my translations and when not trying to inject and use $translate.use() in the above routes.js file I have no problem using it using the 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' );
            };
        }]);
}());

Here is the app.js:

'use strict';

angular
  .module('frontApp', [
    'ui.router',
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngTouch',
    'pascalprecht.translate'
  ]);

What am I missing ? Many thanks for your time and help.

Gab
  • 2,216
  • 4
  • 34
  • 61

1 Answers1

3

Solution here would be to move the DI definition of the '$translate' - where it belongs, i.e. into controller definition:

 .state('language', {
 // instead of this
 // controller: function() {

 // use this
 controller: function($translate) {

      console.log('ok');
      $translate.use(($translate.use() === 'fr') ? 'en' : 'fr' );
    }
 });

or even better controller: ['$translate', function($translate) {... }]

Why is current solution not working? Well, let's have a look at this extract from the above code:

// I. CONFIG phase
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider', '$translate'
       , function($urlRouterProvider, $stateProvider, $locationProvider, $translate){
    ...
    $stateProvider
        ...
        .state('language', {
            // II. CONTROLLER - RUN phase
            controller: function() {

As we can see, we ask for $translate above in the config phase. So we recieve the provider... to be configured.

But the controller definition is expecting to be provided with a servic - already configured. It must have its own DI ... because it will be executed in a RUN phase...

Also check this Q & A for more details about provider in config vs run phase - Getting Error: Unkown provider: $urlMatcherFactory

Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Thank you! Actually I created a LanguageSwitcher service and was getting the same error. So now with your help was able to use it correctly. Thanks again! – Gab Jan 10 '15 at 18:48
  • Great to see that sir, Enjoy might UI-Router ;) great library ... ;) – Radim Köhler Jan 10 '15 at 18:49