Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: (lingui/core) i18n error if id is undefined #1938

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading