Skip to content

Commit

Permalink
fix: (lingui/core) i18n error if id is undefined (#1938)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdul-kissflow authored May 23, 2024
1 parent 1293412 commit 36c637a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,23 @@ describe("I18n", () => {
expect(handler).toHaveBeenCalledTimes(2)
})

it("._ should emit missing event for undefined id", () => {
const i18n = setupI18n({
locale: "en",
messages: { en: {} },
})

const handler = jest.fn()
i18n.on("missing", handler)
// @ts-expect-error 'id' should be of 'MessageDescriptor' or 'string' type.
i18n._()
expect(handler).toHaveBeenCalledTimes(1)
expect(handler).toHaveBeenCalledWith({
id: "",
locale: "en",
})
})

describe("params.missing - handling missing translations", () => {
it("._ should return custom string for missing translations", () => {
const i18n = setupI18n({
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ export class I18n extends EventEmitter<Events> {
options?: MessageOptions
): string {
let message = options?.message

if (!id) {
id = ""
}

if (!isString(id)) {
values = id.values || values
message = id.message
Expand Down

0 comments on commit 36c637a

Please sign in to comment.