Skip to content

Commit e7b10ef

Browse files
committed
Prevent circular dependencies
Fixes #138
1 parent 11f2dfc commit e7b10ef

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

dist/angular-gettext.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ angular.module('gettext').provider('gettextCatalog', ["gettextPlurals", function
7373
angular.extend(this, provider);
7474

7575
var self = this;
76-
this.$get = /* @ngInject */ ["$http", "$interpolate", "$cacheFactory", "$rootScope", function ($http, $interpolate, $cacheFactory, $rootScope) {
76+
this.$get = /* @ngInject */ ["$injector", function ($injector) {
77+
// lazily inject these to prevent circular dependecies
78+
var $http;
79+
var $interpolate;
80+
var $rootScope;
81+
7782
function Catalog(options) {
7883
angular.extend(this, options);
7984
var self = this;
@@ -95,6 +100,7 @@ angular.module('gettext').provider('gettextCatalog', ["gettextPlurals", function
95100
};
96101

97102
function broadcastUpdated() {
103+
$rootScope = $rootScope || $injector.get('$rootScope');
98104
$rootScope.$broadcast('gettextLanguageChanged');
99105
}
100106

@@ -109,12 +115,16 @@ angular.module('gettext').provider('gettextCatalog', ["gettextPlurals", function
109115
};
110116

111117
this.getString = function (string, scope, context) {
118+
$interpolate = $interpolate || $injector.get('$interpolate');
119+
112120
string = this.getStringForm(string, 0, context) || prefixDebug(string);
113121
string = scope ? $interpolate(string)(scope) : string;
114122
return addTranslatedMarkers(string);
115123
};
116124

117125
this.getPlural = function (n, string, stringPlural, scope, context) {
126+
$interpolate = $interpolate || $injector.get('$interpolate');
127+
118128
var form = gettextPlurals(this.currentLanguage, n);
119129
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
120130
if (scope) {
@@ -125,6 +135,7 @@ angular.module('gettext').provider('gettextCatalog', ["gettextPlurals", function
125135
};
126136

127137
this.loadRemote = function (url) {
138+
$http = $http || $injector.get('$http');
128139
return $http({
129140
method: 'GET',
130141
url: url,
@@ -137,7 +148,7 @@ angular.module('gettext').provider('gettextCatalog', ["gettextPlurals", function
137148
};
138149
}
139150

140-
self.cache = self.cache || $cacheFactory('strings');
151+
self.cache = self.cache || $injector.get('$cacheFactory')('strings');
141152
return new Catalog(self);
142153
}];
143154
}]);

dist/angular-gettext.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/catalog.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ angular.module('gettext').provider('gettextCatalog', function (gettextPlurals) {
6161
angular.extend(this, provider);
6262

6363
var self = this;
64-
this.$get = /* @ngInject */ function ($http, $interpolate, $cacheFactory, $rootScope) {
64+
this.$get = /* @ngInject */ function ($injector) {
65+
// lazily inject these to prevent circular dependecies
66+
var $http;
67+
var $interpolate;
68+
var $rootScope;
69+
6570
function Catalog(options) {
6671
angular.extend(this, options);
6772
var self = this;
@@ -83,6 +88,7 @@ angular.module('gettext').provider('gettextCatalog', function (gettextPlurals) {
8388
};
8489

8590
function broadcastUpdated() {
91+
$rootScope = $rootScope || $injector.get('$rootScope');
8692
$rootScope.$broadcast('gettextLanguageChanged');
8793
}
8894

@@ -97,12 +103,16 @@ angular.module('gettext').provider('gettextCatalog', function (gettextPlurals) {
97103
};
98104

99105
this.getString = function (string, scope, context) {
106+
$interpolate = $interpolate || $injector.get('$interpolate');
107+
100108
string = this.getStringForm(string, 0, context) || prefixDebug(string);
101109
string = scope ? $interpolate(string)(scope) : string;
102110
return addTranslatedMarkers(string);
103111
};
104112

105113
this.getPlural = function (n, string, stringPlural, scope, context) {
114+
$interpolate = $interpolate || $injector.get('$interpolate');
115+
106116
var form = gettextPlurals(this.currentLanguage, n);
107117
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
108118
if (scope) {
@@ -113,6 +123,7 @@ angular.module('gettext').provider('gettextCatalog', function (gettextPlurals) {
113123
};
114124

115125
this.loadRemote = function (url) {
126+
$http = $http || $injector.get('$http');
116127
return $http({
117128
method: 'GET',
118129
url: url,
@@ -125,7 +136,7 @@ angular.module('gettext').provider('gettextCatalog', function (gettextPlurals) {
125136
};
126137
}
127138

128-
self.cache = self.cache || $cacheFactory('strings');
139+
self.cache = self.cache || $injector.get('$cacheFactory')('strings');
129140
return new Catalog(self);
130141
};
131142
});

test/unit/circular.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
describe("Catalog", function () {
2+
// don't pull module out of tests!
3+
4+
it("Shouldn't cause circular dependency error on $http", function () {
5+
var testModule = angular.module("gettext.test", function () {});
6+
testModule.factory("testInterceptor", function (gettextCatalog) {
7+
gettextCatalog.getString("some message");
8+
return {};
9+
});
10+
11+
testModule.config(function ($httpProvider) {
12+
$httpProvider.interceptors.push("testInterceptor");
13+
});
14+
15+
module("gettext", "gettext.test");
16+
inject(function ($http) {
17+
assert.notEqual($http, false);
18+
});
19+
});
20+
});

0 commit comments

Comments
 (0)