Skip to content

Commit f70db69

Browse files
committed
feat: 🎸 add scope to event payload
1 parent 88f5112 commit f70db69

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,11 @@ function prependScope(inlineLoader, scope) {
7474
export function resolveInlineLoader(providerScope: TranslocoScope | null, scope: string): InlineLoader | null {
7575
return hasInlineLoader(providerScope) ? prependScope(providerScope.loader, scope) : null;
7676
}
77+
78+
export function getEventPayload(lang: string) {
79+
return {
80+
scope: getScopeFromLang(lang) || null,
81+
langName: getLangFromScope(lang),
82+
lang
83+
};
84+
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ describe('load', () => {
1717
)
1818
.subscribe(spy);
1919
loadLang(service);
20-
expect(spy).toHaveBeenCalledWith({ lang: 'en' });
20+
expect(spy).toHaveBeenCalledWith({ lang: 'en', langName: 'en', scope: null });
21+
}));
22+
23+
it('should trigger loaded event once loaded - scope', fakeAsync(() => {
24+
const spy = jasmine.createSpy();
25+
service.events$
26+
.pipe(
27+
filter((e: any) => e.type === 'translationLoadSuccess'),
28+
pluck('payload')
29+
)
30+
.subscribe(spy);
31+
loadLang(service, 'admin-page/en');
32+
expect(spy).toHaveBeenCalledWith({ lang: 'admin-page/en', langName: 'en', scope: 'admin-page' });
2133
}));
2234

2335
it('should load the translation using the loader', fakeAsync(() => {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { TRANSLOCO_MISSING_HANDLER, TranslocoMissingHandler } from './transloco-
1919
import { TRANSLOCO_INTERCEPTOR, TranslocoInterceptor } from './transloco.interceptor';
2020
import { TRANSLOCO_FALLBACK_STRATEGY, TranslocoFallbackStrategy } from './transloco-fallback-strategy';
2121
import { mergeConfig } from './merge-config';
22-
import { getLangFromScope, getScopeFromLang } from './shared';
22+
import { getEventPayload, getLangFromScope, getScopeFromLang } from './shared';
2323
import { getFallbacksLoaders } from './get-fallbacks-loaders';
2424
import { resolveLoader } from './resolve-loader';
2525

@@ -196,7 +196,7 @@ export class TranslocoService implements OnDestroy {
196196
const value = translation[key];
197197

198198
if (!value) {
199-
return this.handleMissingKey(key, value, params);
199+
return this._handleMissingKey(key, value, params);
200200
}
201201

202202
return this.parser.transpile(value, params, translation);
@@ -336,7 +336,10 @@ export class TranslocoService implements OnDestroy {
336336
this.setTranslation(newValue, lang);
337337
}
338338

339-
handleMissingKey(key: string, value: any, params?: HashMap) {
339+
/**
340+
* @internal
341+
*/
342+
_handleMissingKey(key: string, value: any, params?: HashMap) {
340343
if (this.config.missingHandler.allowEmpty && value === '') {
341344
return '';
342345
}
@@ -425,9 +428,7 @@ export class TranslocoService implements OnDestroy {
425428
this.events.next({
426429
wasFailure: !!this.failedLangs.size,
427430
type: 'translationLoadSuccess',
428-
payload: {
429-
lang
430-
}
431+
payload: getEventPayload(lang)
431432
});
432433

433434
this.failedCounter = 0;
@@ -467,9 +468,7 @@ export class TranslocoService implements OnDestroy {
467468
this.failedCounter++;
468469
this.events.next({
469470
type: 'translationLoadFailure',
470-
payload: {
471-
lang
472-
}
471+
payload: getEventPayload(lang)
473472
});
474473

475474
return this.load(resolveLang);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { Observable } from 'rxjs';
2-
31
export type HashMap<T = any> = { [key: string]: T };
42

53
export type LoadedEvent = {
64
type: 'translationLoadSuccess';
75
wasFailure: boolean;
86
payload: {
7+
scope: string | null;
8+
langName: string;
9+
/** @deprecated */
910
lang: string;
1011
};
1112
};
1213

1314
export type FailedEvent = {
1415
type: 'translationLoadFailure';
15-
payload: {
16-
lang: string;
17-
};
16+
payload: LoadedEvent['payload'];
1817
};
1918

2019
export type TranslocoEvents = LoadedEvent | FailedEvent;

0 commit comments

Comments
 (0)