Skip to content

Commit e9c7d82

Browse files
authored
Ensure default value is used even if it is an empty string (#3)
1 parent 930e788 commit e9c7d82

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/ng-lazy-translate/src/lib/translate.service.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,13 @@ describe(`LazyTranslateService`, () => {
134134
});
135135

136136
it(`should return the default value if no translation is found and a default value is provided`, async () => {
137-
const result = firstValueFrom(service.translate('common.no-value', undefined, 'default'));
137+
const result = firstValueFrom(service.translate('common.no-value', undefined, ''));
138138

139139
await delay();
140140

141141
httpMock.expectOne(TEST_ASSET_PATHS[`en.common`]).flush({});
142142

143-
expect(await result).toBe('default');
143+
expect(await result).toBe('');
144144
});
145145

146146
it(`the value should update when the language changes`, async () => {

packages/ng-lazy-translate/src/lib/translate.service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ export class LazyTranslateService {
8080
distinctUntilChanged((previous, next) => isEqual(previous, next)),
8181
concatMap(() => (isString(key) ? of(key) : NEVER)),
8282
mergeMap(k => from(this.getKey(k))),
83-
map(result => (isNullOrUndefined(result) ? defaultValue || key || '' : result(this.flattenParams(params))))
83+
map(result => {
84+
if (isNullOrUndefined(result)) {
85+
if (!isNullOrUndefined(defaultValue)) {
86+
return defaultValue;
87+
}
88+
89+
return key || '';
90+
}
91+
92+
return result(this.flattenParams(params));
93+
})
8494
);
8595
}
8696

0 commit comments

Comments
 (0)