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

i18n._(id) throws error if id param passed as undefined #1926

Closed
1 of 3 tasks
abdul-kissflow opened this issue Apr 30, 2024 · 0 comments · Fixed by #1938
Closed
1 of 3 tasks

i18n._(id) throws error if id param passed as undefined #1926

abdul-kissflow opened this issue Apr 30, 2024 · 0 comments · Fixed by #1938

Comments

@abdul-kissflow
Copy link
Contributor

abdul-kissflow commented Apr 30, 2024

Describe the bug

i18n._(id) throws error if id param passed as undefined

To Reproduce

This is just an example.

import { i18n } from "@lingui/core"

export default function ErrorComponent(errorMsg) {
   return (
        <p>{i18n._(errorMsg) || <Trans>There is an error</Trans>}</p>
   )
}

Expected behavior

  • if errorMsg is type of string, it should return and show translated message
  • if errorMsg is undefined, it should return undefined so that or condition message displays.

Additional context

It was working in v2.7.2

  • typeof id === "object" check - returns false, if id is undefined as expected
  _(
    id: string | Object,
    values: Object = {},
    { defaults, formats = {} }: MessageOptions = {}
  ) {
    // Expand message descriptor
    if (id && typeof id === "object") {
      values = id.values
      defaults = id.defaults
      formats = id.formats
      id = id.id
    }

in version > 3.0.0, this got changed.

  • !isString(id) check - returns true ,if id is undefined. so it further checks values and throws error
  _(descriptor: MessageDescriptor): string
  _(id: string, values?: Values, options?: MessageOptions): string
  _(
    id: MessageDescriptor | string,
    values?: Values,
    options?: MessageOptions
  ): string {
    let message = options?.message
    if (!isString(id)) {
      values = id.values || values
      message = id.message
      id = id.id
    }
  • jsLingui version lingui --version 4.7.0
  • Babel version npm list @babel/core 7.15.5
  • Macro support:
  • I'm using SWC with @lingui/swc-plugin
  • I'm using Babel with babel-macro-plugin
  • I'm not using macro
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, NextJs, Vite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant