Skip to content

Commit d9b3887

Browse files
committed
fix(TranslateService): Reverted the use of Injector and changed Angular 2 requirement to ~beta.0 ins
It was not a good idea to use the Injector to load Http, the real problem was that Ionic 2 uses Angular 2 beta.0 and ng2-translate uses Angular 2 beta.1. The ionic 2 cli was packaging 2 different instances of Angular 2 and ng2-translate couldn't get Http because the provider it used wasn't the same as the one from the main Ionic 2 application. Changing the requirement to ~beta.0 fixes that problem.
1 parent 934860c commit d9b3887

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ It is recommended to instantiate `TranslateService` in the bootstrap of your app
2525
If you add it to the "providers" property of a component it will instantiate a new instance of the service that won't be initialized.
2626

2727
```js
28+
import {HTTP_PROVIDERS} from 'angular2/http';
29+
2830
bootstrap(AppComponent, [
29-
TranslateService // not required, but recommanded to have 1 unique instance of your service
31+
HTTP_PROVIDERS,
32+
TranslateService // not required, but recommended to have 1 unique instance of your service
3033
]);
3134

3235

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"typings": "./ng2-translate.d.ts",
2727
"homepage": "https://github.com/ocombe/ng2-translate",
2828
"dependencies": {
29-
"angular2": "~2.0.0-beta.1",
29+
"angular2": "~2.0.0-beta.0",
3030
"es6-promise": "^3.0.2",
3131
"es6-shim": "^0.33.3",
3232
"reflect-metadata": "0.1.2",

src/translate.service.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Injectable, EventEmitter, Injector} from 'angular2/core';
2-
import {Http, Response, HTTP_PROVIDERS} from 'angular2/http';
1+
import {Injectable, EventEmitter} from 'angular2/core';
2+
import {Http, Response} from 'angular2/http';
33
import {Observable} from 'rxjs/Observable'
44
import 'rxjs/add/observable/fromArray.js';
55
import 'rxjs/add/operator/share.js';
@@ -68,13 +68,8 @@ export class TranslateService {
6868
private defaultLang: string = 'en';
6969
private langs: Array<string>;
7070
private parser: Parser = new Parser();
71-
private http: Http;
7271

73-
constructor() {
74-
// We make sure that HTTP_PROVIDERS has been created & instantiated
75-
// because sometimes it hasn't been provided in bootstrap
76-
var injector = Injector.resolveAndCreate([HTTP_PROVIDERS]);
77-
this.http = injector.get(Http);
72+
constructor(private http: Http) {
7873
this.useStaticFilesLoader();
7974
}
8075

tests/translate.service.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
1+
import {it, beforeEachProviders, inject} from "angular2/testing";
2+
import {provide} from "angular2/core";
3+
import {
4+
BaseRequestOptions, Http, ResponseOptions, Response, HTTP_PROVIDERS, Connection,
5+
XHRBackend
6+
} from "angular2/http";
7+
import {MockBackend, MockConnection} from "angular2/http/testing";
18
import {TranslateService} from '../src/translate.service';
29

310
export function main() {
11+
412
describe('TranslateService', () => {
13+
beforeEachProviders(() => [
14+
BaseRequestOptions,
15+
HTTP_PROVIDERS,
16+
// Provide a mocked (fake) backend for Http
17+
provide(XHRBackend, {useClass: MockBackend}),
18+
TranslateService
19+
]);
20+
21+
522
it('is defined', () => {
623
expect(TranslateService).toBeDefined();
724
});
25+
26+
// this test is async, and yet it works thanks to Zone \o/
27+
it('should be able to get translations for the view', inject([XHRBackend, Http, TranslateService], (xhrBackend, http, translate) => {
28+
var connection: MockConnection; //this will be set when a new connection is emitted from the backend.
29+
xhrBackend.connections.subscribe((c: MockConnection) => connection = c);
30+
31+
// this will load translate json files from src/public/i18n
32+
translate.useStaticFilesLoader();
33+
34+
// the lang to use, if the lang isn't available, it will use the current loader to get them
35+
translate.use('en');
36+
37+
// this will request the translation from the backend because we use a static files loader for TranslateService
38+
translate.get('TEST').subscribe((res: string) => {
39+
expect(res).toEqual('This is a test');
40+
});
41+
42+
// mock response after the xhr request, otherwise it will be undefined
43+
connection.mockRespond(new Response(new ResponseOptions({body: '{"TEST": "This is a test"}'})));
44+
}));
845
});
946
}

0 commit comments

Comments
 (0)