Skip to content

Commit 156cd62

Browse files
authored
fix: 🐛 pipe return empty string by default (#192)
1 parent e6cf89e commit 156cd62

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

projects/ngneat/transloco/src/lib/tests/pipe/pipe.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ describe('TranslocoPipe', () => {
2121
spyOn(pipe as any, 'updateValue').and.callThrough();
2222
});
2323

24+
it('should return empty string as default', () => {
25+
pipe = new TranslocoPipe(translateServiceMock, null, 'es', cdrMock);
26+
expect(pipe.transform('title', {})).toBe('');
27+
});
28+
2429
it('should use provided language', fakeAsync(() => {
2530
spyOn(translateServiceMock, 'translate').and.callThrough();
2631
pipe = new TranslocoPipe(translateServiceMock, null, 'es', cdrMock);
@@ -55,7 +60,12 @@ describe('TranslocoPipe', () => {
5560

5661
it('should load scope translation with multiple provided scopes', fakeAsync(() => {
5762
spyOn(translateServiceMock, 'translate').and.callThrough();
58-
pipe = new TranslocoPipe(translateServiceMock, [{ scope: 'lazy-page', alias: 'lazyPageAlias' }, { scope: 'admin-page', alias: 'adminPageAlias' }], null, cdrMock);
63+
pipe = new TranslocoPipe(
64+
translateServiceMock,
65+
[{ scope: 'lazy-page', alias: 'lazyPageAlias' }, { scope: 'admin-page', alias: 'adminPageAlias' }],
66+
null,
67+
cdrMock
68+
);
5969
(pipe as any).listenToLangChange = true;
6070
pipe.transform('lazyPageAlias.title', {});
6171
runLoader();

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ScopeResolver } from './scope-resolver';
1515
})
1616
export class TranslocoPipe implements PipeTransform, OnDestroy {
1717
private subscription: Subscription | null = null;
18-
private lastValue: string | undefined;
18+
private lastValue: string = '';
1919
private lastKey: string | undefined;
2020
private listenToLangChange: boolean;
2121
private path: string;
@@ -54,8 +54,10 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
5454
active: activeLang
5555
});
5656

57-
return Array.isArray(this.providerScope) ?
58-
forkJoin((<TranslocoScope[]>this.providerScope).map(providerScope => this.resolveScope(lang, providerScope)))
57+
return Array.isArray(this.providerScope)
58+
? forkJoin(
59+
(<TranslocoScope[]>this.providerScope).map(providerScope => this.resolveScope(lang, providerScope))
60+
)
5961
: this.resolveScope(lang, this.providerScope);
6062
}),
6163
listenOrNotOperator(this.listenToLangChange)
@@ -76,9 +78,9 @@ export class TranslocoPipe implements PipeTransform, OnDestroy {
7678
}
7779

7880
private resolveScope(lang: string, providerScope: TranslocoScope): Observable<Translation | Translation[]> {
79-
let resolvedScope = this.scopeResolver.resolve({ inline: undefined, provider: providerScope });
80-
this.path = this.langResolver.resolveLangPath(lang, resolvedScope);
81-
const inlineLoader = resolveInlineLoader(providerScope, resolvedScope);
82-
return this.translocoService._loadDependencies(this.path, inlineLoader);
81+
let resolvedScope = this.scopeResolver.resolve({ inline: undefined, provider: providerScope });
82+
this.path = this.langResolver.resolveLangPath(lang, resolvedScope);
83+
const inlineLoader = resolveInlineLoader(providerScope, resolvedScope);
84+
return this.translocoService._loadDependencies(this.path, inlineLoader);
8385
}
8486
}

src/assets/i18n/inline/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"title": "Inline Loaders English"
3+
}

src/assets/i18n/inline/es.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"title": "Inline Loaders Spanish"
3+
}

0 commit comments

Comments
 (0)