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

Discrepancy in lazy translations #1432

Closed
stovmascript opened this issue Feb 13, 2023 · 2 comments
Closed

Discrepancy in lazy translations #1432

stovmascript opened this issue Feb 13, 2023 · 2 comments

Comments

@stovmascript
Copy link
Contributor

Describe the bug
The documentation about lazy translations shows how you can use defineMessage and pass it to either Trans or i18n._(). When I use i18n._(), extracting messages prints out Missing message ID, skipping. warnings.

To Reproduce
You can use the code provided in the documentation and run lingui extract:

// src/I18nTest.js
import { i18n } from '@lingui/core'
import { defineMessage, Trans } from '@lingui/macro'

const favoriteColors = [
  defineMessage({ message: 'Red' }),
  defineMessage({ message: 'Orange' }),
  defineMessage({ message: 'Yellow' }),
  defineMessage({ message: 'Green' }),
]

export default function ColorList() {
  return (
    <ul>
      {favoriteColors.map((color, index) => (
        <li key={index}>
          <Trans id={color.id} />
        </li>
      ))}
    </ul>
  )
}

export function getTranslatedColorNames() {
  return favoriteColors.map((color) => i18n._(color))
}

Expected behavior
Both methods should work the same, without printing warnings.

Additional context
Add any other context about the problem here.

  • jsLingui version 3.17.1 (yarn lingui --version prints "dev"?)
  • Babel version 7.20.12
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.)
    • using "@lingui/swc-plugin": "^0.2.0"
    • with Next.js in a JS codebase
      // lingui.config.js
      extractBabelOptions: {
        presets: ['@babel/preset-react'],
      },
      
      // next.config.js
      experimental: {
        swcPlugins: [
          [
            '@lingui/swc-plugin',
            {
              // the same options as in .swcrc
            },
          ],
        ],
      },
@timofei-iatsenko
Copy link
Collaborator

Yes, that's a bug. Unfortunately there are no obvious solution, because either it's break backward compatibility with existing code or will disable this validation at all, which is also kinda breaking change.

I will definitely consider this issue in v4. Thanks for reporting.

As for now, as workaround you can rename i18n object to something and pass descriptor into it. The extractor matching only by identifier name, so this would work

const _i18n = i18n;

export function getTranslatedColorNames() {
  return favoriteColors.map((color) => _i18n._(color))
}

@timofei-iatsenko
Copy link
Collaborator

Just checked this on the next branch, and it seems the issue got fixed by #1358

I even added test, which i don't remember 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants