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

Lingui CLI extraction puts msgid as msgstr #1921

Closed
1 of 3 tasks
EnablingLogic opened this issue Apr 17, 2024 · 3 comments · Fixed by #1936
Closed
1 of 3 tasks

Lingui CLI extraction puts msgid as msgstr #1921

EnablingLogic opened this issue Apr 17, 2024 · 3 comments · Fixed by #1936

Comments

@EnablingLogic
Copy link

Describe the bug
In my application, I have a translation with ID addToCart. Since I use it in multiple files, I've only provided a default message once. Now, when I extract the messages, it's extracted as:

msgid "addToCart"
msgstr "addToCart"

To Reproduce
Steps to reproduce the behavior, possibly with minimal code sample, e.g:

ComponentOne.tsx

import { i18n } from "@lingui/core"

export default function ComponentOne() {
  return i18n._("addToCart")
}

ComponentTwo.tsx

import { i18n } from "@lingui/core"

export default function ComponentTwo() {
  return i18n._({ id: "addToCart", message: "Add to cart" })
}

Now run lingui extract --overwrite --clean with this as lingui.config.js:

module.exports = {
  orderBy: "messageId",
  locales: [ "en", "nl", "de" ],
  fallbackLocales: {
    default: "en"
  },
  sourceLocale: "en",
  catalogs: [
    {
      path: "./locales/{locale}/messages",
      include: [ "**/**/*.ts*" ],
      exclude: [ "node_modules/**", "**/*.d.ts" ]
    }
  ],
  format: "po",
  compileNamespace: "ts"
}

Expected behavior
I expect the message to be extracted as:

msgid "addToCart"
msgstr "Add to cart"

Additional context
Add any other context about the problem here.

  • jsLingui version lingui --version: 4.8.0
  • Babel version npm list @babel/core: 7.24.4
  • 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): I'm using Next.js
@thekip
Copy link
Collaborator

thekip commented Apr 18, 2024

I don't have a capacity to look into it right now, but can provide you with a direction:

Here is a snippet responsible for default message:

const prev = messages[next.id]
// there might be a case when filename was not mapped from sourcemaps
const filename = next.origin[0]
? path.relative(config.rootDir, next.origin[0]).replace(/\\/g, "/")
: ""
const origin: MessageOrigin = [filename, next.origin[1]]
if (prev.message && next.message && prev.message !== next.message) {
throw new Error(
`Encountered different default translations for message ${chalk.yellow(
next.id
)}` +
`\n${chalk.yellow(prettyOrigin(prev.origin))} ${prev.message}` +
`\n${chalk.yellow(prettyOrigin([origin]))} ${next.message}`
)
}
messages[next.id] = {
...prev,
comments: next.comment
? [...prev.comments, next.comment]
: prev.comments,
origin: [...prev.origin, [filename, next.origin[1]]],
}
},

You can debug it and make a PR. Thanks.

@aseerkt
Copy link
Contributor

aseerkt commented May 13, 2024

What is the expected behavior here?. Should it throw error when it encounters two different messages for same ID?

@thekip
Copy link
Collaborator

thekip commented May 13, 2024

Yes, but should ignore instances where message is not provided (take existing if any)

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

Successfully merging a pull request may close this issue.

3 participants