Skip to content

Commit f2e5415

Browse files
committed
fix: 🐛 select translation on lang change
also added support for passing in scopes
1 parent 15b7bfb commit f2e5415

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

projects/ngneat/transloco/src/lib/tests/service/selectTranslation.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@ describe('selectTranslation', () => {
77

88
beforeEach(() => (service = createService()));
99

10+
it('should select the active translation scope', fakeAsync(() => {
11+
const spy = jasmine.createSpy();
12+
service.selectTranslation('lazy-page').subscribe(spy);
13+
runLoader();
14+
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['lazy-page/en']));
15+
service.setActiveLang('es');
16+
runLoader();
17+
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['lazy-page/es']));
18+
}));
19+
1020
it('should select the active translation lang', fakeAsync(() => {
1121
const spy = jasmine.createSpy();
1222
service.selectTranslation().subscribe(spy);
1323
runLoader();
1424
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['en']));
25+
service.setActiveLang('es');
26+
runLoader();
27+
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['es']));
1528
}));
1629

1730
it('should select the translation lang when passing one', fakeAsync(() => {
@@ -20,4 +33,11 @@ describe('selectTranslation', () => {
2033
runLoader();
2134
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['es']));
2235
}));
36+
37+
it('should select the translation scope when passing one', fakeAsync(() => {
38+
const spy = jasmine.createSpy();
39+
service.selectTranslation('lazy-page/es').subscribe(spy);
40+
runLoader();
41+
expect(spy).toHaveBeenCalledWith(flatten(mockLangs['lazy-page/es']));
42+
}));
2343
});

projects/ngneat/transloco/src/lib/transloco.service.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,32 @@ export class TranslocoService implements OnDestroy {
336336
*
337337
* @example
338338
*
339-
* selectTranslation().subscribe()
339+
* selectTranslation().subscribe() - will return the current lang translation
340340
* selectTranslation('es').subscribe()
341+
* selectTranslation('admin-page').subscribe() - will return the current lang scope translation
342+
* selectTranslation('admin-page/es').subscribe()
341343
*/
342344
selectTranslation(lang?: string): Observable<Translation> {
343-
const language = lang || this.getActiveLang();
344-
return this.load(language).pipe(map(() => this.getTranslation(language)));
345+
let language$ = this.langChanges$;
346+
if (lang) {
347+
if (this.isLang(lang)) {
348+
language$ = of(lang);
349+
} else {
350+
const { scope } = this.resolveLangAndScope(lang);
351+
const mapToScopeValue = language =>
352+
map(() => flatten(this.translateObject(scope, null, getLangFromScope(language))));
353+
const scopeLangSpecified = getLangFromScope(lang) !== lang;
354+
if (scopeLangSpecified) {
355+
return this.load(lang).pipe(mapToScopeValue(lang));
356+
}
357+
358+
return language$.pipe(
359+
switchMap(currentLang => this.load(`${lang}/${currentLang}`).pipe(mapToScopeValue(currentLang)))
360+
);
361+
}
362+
}
363+
364+
return language$.pipe(switchMap(language => this.load(language).pipe(map(() => this.getTranslation(language)))));
345365
}
346366

347367
/**

0 commit comments

Comments
 (0)