i18n using Angular Translate StaticFilesLoader

こ雲淡風輕ζ 提交于 2019-12-10 16:08:50

问题


I'm following this tutorial at ng-newsletter to apply i18n to my app using Angular-Translate. The app works fine when I include the translations within my app.js file, but I'm having trouble getting the StaticFilesLoader to work. This is my app.js file with the working code commented out-

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've added two files to my app, en.json and de.json and a folder called /languages. When I try to run the application, I get an error saying:

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

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


回答1:


Include this in your file also:

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

Rename it to angular-translate-loader-static-files.min.js and include it.

It's located here: https://github.com/angular-translate/bower-angular-translate-loader-static-files



来源:https://stackoverflow.com/questions/22011584/i18n-using-angular-translate-staticfilesloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!