Skip to content

Commit 0954608

Browse files
authored
Merge pull request #1580 from ngx-translate/feat/instant/undefined-value
feat(service/instant, get): accept empty string
2 parents b1a178f + 73cdb63 commit 0954608

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ export class TranslateService implements ITranslateService {
511511
interpolateParams?: InterpolationParameters,
512512
): Observable<Translation> {
513513
if (!isDefinedAndNotNull(key) || !key.length) {
514-
throw new Error(`Parameter "key" is required and cannot be empty`);
514+
return of("");
515515
}
516516
// check if we are loading a new translation to use
517517
if (this.pending) {
@@ -583,7 +583,7 @@ export class TranslateService implements ITranslateService {
583583
interpolateParams?: InterpolationParameters,
584584
): Translation {
585585
if (!isDefinedAndNotNull(key) || key.length === 0) {
586-
throw new Error('Parameter "key" is required and cannot be empty');
586+
return "";
587587
}
588588

589589
const result = this.getParsedResult(key, interpolateParams);

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,17 @@ describe("TranslateService", () => {
227227
});
228228
});
229229

230-
it("should throw if you forget the key", () => {
230+
it("Should ignore if you forget the key", () => {
231231
translate.use("en");
232232

233-
expect(() => {
234-
const key: Record<string, string> = {};
235-
translate.get(key["x"]);
236-
}).toThrowError('Parameter "key" is required and cannot be empty');
237-
238-
expect(() => {
239-
translate.get("");
240-
}).toThrowError('Parameter "key" is required and cannot be empty');
233+
const key: Record<string, string> = {};
234+
translate.get(key["x"]).subscribe((res: Translation) => {
235+
expect(res).toEqual("");
236+
});
241237

242-
expect(() => {
243-
const key: Record<string, string> = {};
244-
translate.instant(key["x"]);
245-
}).toThrowError('Parameter "key" is required and cannot be empty');
238+
translate.get("").subscribe((res: Translation) => {
239+
expect(res).toEqual("");
240+
});
246241
});
247242

248243
it("should be able to get translations with nested keys", () => {
@@ -1066,24 +1061,20 @@ describe("TranslateService (Error Conditions and Recovery)", () => {
10661061

10671062
describe("Invalid Parameter Handling", () => {
10681063
it("should handle null parameters gracefully", () => {
1069-
expect(() => {
1070-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1071-
translate.get(null as any);
1072-
}).toThrowError('Parameter "key" is required and cannot be empty');
1073-
});
1064+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1065+
translate.get(null as any).subscribe((res: Translation) => {
1066+
expect(res).toEqual("");
1067+
});
10741068

1075-
it("should handle undefined parameters gracefully", () => {
1076-
expect(() => {
1077-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1078-
translate.get(undefined as any);
1079-
}).toThrowError('Parameter "key" is required and cannot be empty');
1080-
});
1069+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1070+
translate.get(undefined as any).subscribe((res: Translation) => {
1071+
expect(res).toEqual("");
1072+
});
10811073

1082-
it("should handle non-string parameters gracefully", () => {
1083-
expect(() => {
1084-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1085-
translate.get(123 as any);
1086-
}).toThrowError('Parameter "key" is required and cannot be empty');
1074+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1075+
translate.get(123 as any).subscribe((res: Translation) => {
1076+
expect(res).toEqual("");
1077+
});
10871078
});
10881079

10891080
it("should handle array with invalid elements", () => {

0 commit comments

Comments
 (0)