I18n using Angular Translate StaticFilesLoader

I am following this tutorial in ng-newsletter to apply i18n to my application using Angular-Translate. The application works fine when I include translations in the app.js file, but it's hard for me to get StaticFilesLoader to work. This is my app.js file with commented working code -

angular.module('myApp',
[
'ngCookies',
'ngRoute',
'ngResource',
'pascalprecht.translate',
'myApp.services',
'myApp.directives',
'myApp.controllers',
]);


angular.module('myApp.services', ['ngResource']);
angular.module('myApp.directives', []);
angular.module('myApp.controllers', []);


angular.module('myApp')
.config(['$httpProvider', '$translateProvider', function($httpProvider, $translateProvider) {
$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
console.log($httpProvider.defaults);

/*$translateProvider.translations('en', {
    HEADLINE: 'This is my home page',
    HOME: 'Home',
    SETTINGS: 'Settings',
    LOGOUT: 'Log Out',
    EDIT: 'Edit' ,
    DELETE: 'Delete' ,
    PASSWORD: 'Password' ,
    CONFIRM_PASSWORD: 'Confirm Password' ,
    BUTTON_TEXT_EN: 'english',
    BUTTON_TEXT_DE: 'german'

  })
  .translations('de', {
    HEADLINE: 'Dies ist der Homepage',
    HOME: 'Zuhause',
    SETTINGS: 'Einstellungen',
    LOGOUT: 'Ausloggen',
    EDIT: 'Bearbeiten' ,
    DELETE: 'Löschen' ,
    PASSWORD: 'Passwort' ,
    CONFIRM_PASSWORD: 'Passwort Bestätigen' ,
    BUTTON_TEXT_EN: 'englisch',
    BUTTON_TEXT_DE: 'deutsch'
  }); */

  $translateProvider.preferredLanguage('en');

  $translateProvider.useStaticFilesLoader({
          prefix: '/languages/',
          suffix: '.json'
        });

}]);

I added two files to my application, en.json and de.json and a folder called / languages. When I try to start the application, I get an error message:

Uncaught Error: [$injector:unpr] Unknown provider: $translateStaticFilesLoaderProvider <- $translateStaticFilesLoader

How to declare it as a dependency? I thought this was part of pascalprecht.translate.

+4
source share
1 answer

Add also to your file:

angular.module("pascalprecht.translate").factory("$translateStaticFilesLoader",["$q","$http",function(a,b){return function(c){if(!c||!angular.isString(c.prefix)||!angular.isString(c.suffix))throw new Error("Couldn't load static files, no prefix or suffix specified!");var d=a.defer();return b({url:[c.prefix,c.key,c.suffix].join(""),method:"GET",params:""}).success(function(a){d.resolve(a)}).error(function(){d.reject(c.key)}),d.promise}}]);

angular -translate-loader-static-files.min.js .

: https://github.com/angular-translate/bower-angular-translate-loader-static-files

+7

All Articles