From b28d7ea687dc7d1248d51d888bd4fce5d9e732aa Mon Sep 17 00:00:00 2001 From: abdul-kissflow Date: Mon, 20 May 2024 15:17:54 +0530 Subject: [PATCH 1/4] fix: (lingui/core) i18n error if id is undefined --- packages/core/src/i18n.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/i18n.ts b/packages/core/src/i18n.ts index feaf8f9f5..dc550b952 100644 --- a/packages/core/src/i18n.ts +++ b/packages/core/src/i18n.ts @@ -213,7 +213,7 @@ export class I18n extends EventEmitter { options?: MessageOptions ): string { let message = options?.message - if (!isString(id)) { + if (id && !isString(id)) { values = id.values || values message = id.message id = id.id From c50207efd0825170be359f3cfbcecc18b08539bf Mon Sep 17 00:00:00 2001 From: abdul-kissflow Date: Wed, 22 May 2024 13:36:24 +0530 Subject: [PATCH 2/4] fix: (lingui/core) i18n error if id is undefined - test case added --- packages/core/src/i18n.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/core/src/i18n.test.ts b/packages/core/src/i18n.test.ts index 5deccfe63..da0f59189 100644 --- a/packages/core/src/i18n.test.ts +++ b/packages/core/src/i18n.test.ts @@ -290,6 +290,25 @@ describe("I18n", () => { expect(handler).toHaveBeenCalledTimes(2) }) + it("._ should emit missing event for undefined id", () => { + const i18n = setupI18n({ + locale: "en", + messages: { en: {} }, + }) + + const messageID = undefined + + const handler = jest.fn() + i18n.on("missing", handler) + // @ts-ignore + i18n._(messageID) + expect(handler).toHaveBeenCalledTimes(1) + expect(handler).toHaveBeenCalledWith({ + id: undefined, + locale: "en", + }) + }) + describe("params.missing - handling missing translations", () => { it("._ should return custom string for missing translations", () => { const i18n = setupI18n({ From 1d24c0ceab4135511c26130f73bc462ff0be935b Mon Sep 17 00:00:00 2001 From: abdul-kissflow Date: Wed, 22 May 2024 14:05:43 +0530 Subject: [PATCH 3/4] fix: (lingui/core) i18n error if id is undefined - test case - @ts-expect-error desc added --- packages/core/src/i18n.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/src/i18n.test.ts b/packages/core/src/i18n.test.ts index da0f59189..1cb4373aa 100644 --- a/packages/core/src/i18n.test.ts +++ b/packages/core/src/i18n.test.ts @@ -300,11 +300,11 @@ describe("I18n", () => { const handler = jest.fn() i18n.on("missing", handler) - // @ts-ignore + // @ts-expect-error 'id' should be of 'MessageDescriptor' or 'string' type. i18n._(messageID) expect(handler).toHaveBeenCalledTimes(1) expect(handler).toHaveBeenCalledWith({ - id: undefined, + id: messageID, locale: "en", }) }) From 71f9ca1853710be7c9846e733aa78c3843283810 Mon Sep 17 00:00:00 2001 From: abdul-kissflow Date: Thu, 23 May 2024 14:57:06 +0530 Subject: [PATCH 4/4] fix: (lingui/core) i18n error if id is undefined - test case refined --- packages/core/src/i18n.test.ts | 6 ++---- packages/core/src/i18n.ts | 7 ++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/core/src/i18n.test.ts b/packages/core/src/i18n.test.ts index 1cb4373aa..1f09c38e9 100644 --- a/packages/core/src/i18n.test.ts +++ b/packages/core/src/i18n.test.ts @@ -296,15 +296,13 @@ describe("I18n", () => { messages: { en: {} }, }) - const messageID = undefined - const handler = jest.fn() i18n.on("missing", handler) // @ts-expect-error 'id' should be of 'MessageDescriptor' or 'string' type. - i18n._(messageID) + i18n._() expect(handler).toHaveBeenCalledTimes(1) expect(handler).toHaveBeenCalledWith({ - id: messageID, + id: "", locale: "en", }) }) diff --git a/packages/core/src/i18n.ts b/packages/core/src/i18n.ts index dc550b952..ed04e9368 100644 --- a/packages/core/src/i18n.ts +++ b/packages/core/src/i18n.ts @@ -213,7 +213,12 @@ export class I18n extends EventEmitter { options?: MessageOptions ): string { let message = options?.message - if (id && !isString(id)) { + + if (!id) { + id = "" + } + + if (!isString(id)) { values = id.values || values message = id.message id = id.id