angularjs - Dependency on $translateProvider in factory? -
trying far basic angularjs app work translations support. first create app, configure $translateprovider
. create factory data in set initial stuff in $rootscope
, want set language.
var app = angular.module('app', ['ngroute', 'pascalprecht.translate']); app.config(['$translateprovider', function($translateprovider) { $translateprovider.preferredlanguage('sv'); $translateprovider.usestaticfilesloader({ prefix: '/assets/translations/', suffix: '.json' }); }]); app.factory('data', ['$http', '$rootscope', '$translateprovider', function ($http, $rootscope, $translateprovider) { $http.get('/api/get/state').success(function(data) { $translateprovider.preferredlanguage(data.language); // ... set other stuff here in $rootscope }); } ]);
the problem when run code get:
error: [$injector:unpr] http://errors.angularjs.org/1.2.16/$injector/unpr?p0=%24translateproviderprovider%20%3c-%20%24translateprovider%20%3c-%20data @ error (native) @ https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:6:450 @ https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:35:431 @ object.c [as get] (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:34:13) @ https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:35:499 @ c (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:34:13) @ object.d [as invoke] (https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:34:230) @ https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min.js:36:13
is not possible have dependency this? in case, how can set language when it? might doing things wrong since i'm new angularjs, suggestions how things in different ways welcome.
use $translate
service instead of provider factory.
app.factory('data', ['$http', '$rootscope', '$translate', function ($http, $rootscope, $translate) { $http.get('/api/get/state').success(function(data) { $translate.use(data.language); // ... set other stuff here in $rootscope }); } ]);
and use $translate.use(data.language);
, instead of $translateprovider.preferredlanguage(data.language);
set/change language @ runtime.
Comments
Post a Comment