Skip to content

Commit ef48ea8

Browse files
authored
feat(service): add getStreamOnTranslationChange method
1 parent 94bdc0d commit ef48ea8

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ To render them, simply use the `innerHTML` attribute with the pipe on any elemen
384384
- `addLangs(langs: Array<string>)`: Add new langs to the list
385385
- `getLangs()`: Returns an array of currently available langs
386386
- `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
387+
- `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.
387388
- `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.
388389
- `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.
389390
- `set(key: string, value: string, lang?: string)`: Sets the translated value of a key

projects/ngx-translate/core/src/lib/translate.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ export class TranslateService {
377377
}
378378
}
379379

380+
/**
381+
* Returns a stream of translated values of a key (or an array of keys) which updates
382+
* whenever the translation changes.
383+
* @returns A stream of the translated key, or an object of translated keys
384+
*/
385+
public getStreamOnTranslationChange(key: string | Array<string>, interpolateParams?: Object): Observable<string | any> {
386+
if (!isDefined(key) || !key.length) {
387+
throw new Error(`Parameter "key" required`);
388+
}
389+
390+
return concat(
391+
this.get(key, interpolateParams),
392+
this.onTranslationChange.pipe(
393+
switchMap((event: TranslationChangeEvent) => {
394+
const res = this.getParsedResult(event.translations, key, interpolateParams);
395+
if (typeof res.subscribe === 'function') {
396+
return res;
397+
} else {
398+
return of(res);
399+
}
400+
})
401+
)
402+
);
403+
}
404+
380405
/**
381406
* Returns a stream of translated values of a key (or an array of keys) which updates
382407
* whenever the language changes.

projects/ngx-translate/core/tests/translate.service.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,53 @@ describe('TranslateService', () => {
204204
});
205205
});
206206

207+
it('should be able to get a stream array', (done: Function) => {
208+
let tr = {"TEST": "This is a test", "TEST2": "This is a test2"};
209+
translate.setTranslation('en', tr);
210+
translate.use('en');
211+
212+
translate.getStreamOnTranslationChange(['TEST', 'TEST2']).subscribe((res: any) => {
213+
expect(res).toEqual(tr);
214+
done();
215+
});
216+
});
217+
218+
it('should initially return the same value for getStreamOnTranslationChange and non-streaming get', (done: Function) => {
219+
translations = {"TEST": "This is a test"};
220+
translate.use('en');
221+
222+
zip(translate.getStreamOnTranslationChange('TEST'), translate.get('TEST')).subscribe((value: [string, string]) => {
223+
const [streamed, nonStreamed] = value;
224+
expect(streamed).toEqual('This is a test');
225+
expect(streamed).toEqual(nonStreamed);
226+
done();
227+
});
228+
});
229+
230+
it('should be able to stream a translation on translation change', (done: Function) => {
231+
translations = {"TEST": "This is a test"};
232+
translate.use('en');
233+
234+
translate.getStreamOnTranslationChange('TEST').pipe(take(3), toArray()).subscribe((res: string[]) => {
235+
const expected = ['This is a test', 'I changed the test value!', 'I changed it again!'];
236+
expect(res).toEqual(expected);
237+
done();
238+
});
239+
translate.setTranslation('en', {"TEST": "I changed the test value!"});
240+
translate.setTranslation('en', {"TEST": "I changed it again!"});
241+
});
242+
243+
it('should interpolate the same param into each streamed value when get strean on translation change', (done: Function) => {
244+
translations = {"TEST": "This is a test {{param}}"};
245+
translate.use('en');
246+
247+
translate.getStreamOnTranslationChange('TEST', {param: 'with param'}).subscribe((res: string[]) => {
248+
const expected = 'This is a test with param';
249+
expect(res).toEqual(expected);
250+
done();
251+
});
252+
});
253+
207254
it('should be able to stream a translation for the current language', (done: Function) => {
208255
translations = {"TEST": "This is a test"};
209256
translate.use('en');

0 commit comments

Comments
 (0)