Skip to content

Commit

Permalink
feat(service): add getStreamOnTranslationChange method
Browse files Browse the repository at this point in the history
  • Loading branch information
MKhowaja authored Feb 5, 2020
1 parent 94bdc0d commit ef48ea8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ To render them, simply use the `innerHTML` attribute with the pipe on any elemen
- `addLangs(langs: Array<string>)`: Add new langs to the list
- `getLangs()`: Returns an array of currently available langs
- `get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Gets the translated value of a key (or an array of keys) or the key if the value was not found
- `getStreamOnTranslationChange(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Returns a stream of translated values of a key (or an array of keys) or the key if the value was not found. Without any `onTranslationChange` events this returns the same value as `get` but it will also emit new values whenever the translation changes.
- `stream(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Returns a stream of translated values of a key (or an array of keys) or the key if the value was not found. Without any `onLangChange` events this returns the same value as `get` but it will also emit new values whenever the used language changes.
- `instant(key: string|Array<string>, interpolateParams?: Object): string|Object`: Gets the instant translated value of a key (or an array of keys). /!\ This method is **synchronous** and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method. If you are not sure then you should use the `get` method instead.
- `set(key: string, value: string, lang?: string)`: Sets the translated value of a key
Expand Down
25 changes: 25 additions & 0 deletions projects/ngx-translate/core/src/lib/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,31 @@ export class TranslateService {
}
}

/**
* Returns a stream of translated values of a key (or an array of keys) which updates
* whenever the translation changes.
* @returns A stream of the translated key, or an object of translated keys
*/
public getStreamOnTranslationChange(key: string | Array<string>, interpolateParams?: Object): Observable<string | any> {
if (!isDefined(key) || !key.length) {
throw new Error(`Parameter "key" required`);
}

return concat(
this.get(key, interpolateParams),
this.onTranslationChange.pipe(
switchMap((event: TranslationChangeEvent) => {
const res = this.getParsedResult(event.translations, key, interpolateParams);
if (typeof res.subscribe === 'function') {
return res;
} else {
return of(res);
}
})
)
);
}

/**
* Returns a stream of translated values of a key (or an array of keys) which updates
* whenever the language changes.
Expand Down
47 changes: 47 additions & 0 deletions projects/ngx-translate/core/tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,53 @@ describe('TranslateService', () => {
});
});

it('should be able to get a stream array', (done: Function) => {
let tr = {"TEST": "This is a test", "TEST2": "This is a test2"};
translate.setTranslation('en', tr);
translate.use('en');

translate.getStreamOnTranslationChange(['TEST', 'TEST2']).subscribe((res: any) => {
expect(res).toEqual(tr);
done();
});
});

it('should initially return the same value for getStreamOnTranslationChange and non-streaming get', (done: Function) => {
translations = {"TEST": "This is a test"};
translate.use('en');

zip(translate.getStreamOnTranslationChange('TEST'), translate.get('TEST')).subscribe((value: [string, string]) => {
const [streamed, nonStreamed] = value;
expect(streamed).toEqual('This is a test');
expect(streamed).toEqual(nonStreamed);
done();
});
});

it('should be able to stream a translation on translation change', (done: Function) => {
translations = {"TEST": "This is a test"};
translate.use('en');

translate.getStreamOnTranslationChange('TEST').pipe(take(3), toArray()).subscribe((res: string[]) => {
const expected = ['This is a test', 'I changed the test value!', 'I changed it again!'];
expect(res).toEqual(expected);
done();
});
translate.setTranslation('en', {"TEST": "I changed the test value!"});
translate.setTranslation('en', {"TEST": "I changed it again!"});
});

it('should interpolate the same param into each streamed value when get strean on translation change', (done: Function) => {
translations = {"TEST": "This is a test {{param}}"};
translate.use('en');

translate.getStreamOnTranslationChange('TEST', {param: 'with param'}).subscribe((res: string[]) => {
const expected = 'This is a test with param';
expect(res).toEqual(expected);
done();
});
});

it('should be able to stream a translation for the current language', (done: Function) => {
translations = {"TEST": "This is a test"};
translate.use('en');
Expand Down

0 comments on commit ef48ea8

Please sign in to comment.