question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pass "values" directly to <Trans /> component

See original GitHub issue

I updated to version 3 (3.8.1) from version 2 (2.9.1). Had to make a few adaptions but nothing too serious. I like the new version 👍

Describe the bug The is one problem after upgrading that I need help with: I have been using the <Trans /> component from @lingui/macro heavily throughout the code, mostly like this:

<Trans
  id="some.message.id"
  values={{ /* values passed in directly */ }}
  comment="Variables: describing all the values passed in"  
>
    English placeholder text
</Trans>

Now, it seems that values is not part of the type definition of Trans anymore, even though the actual translation works perfectly fine when values is specified. Is that a bug in the type definition or am I using it wrong?

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

import { Trans } from "@lingui/macro"

export default function App() {
  const values = { foo: "bar" }
   return (<Trans id="my.test.message" values={values} comment="Variables: foo (test string)">
      This should be translated and variables should be available!
   </Trans>);
}

message catalogue:

msgid "my.test.message"
msgstr "Translated message for foo: {foo}"

Expected behavior values can be supplied directly to Trans as a prop. Or a different strategy to achieve the same goal.

Alternatives I tried

I tried adding the values prop ty the type definition myself locally, but I cannot manage to make it work. Maybe I am doing it wrong?

import { TransProps } from "@lingui/macro";
import { ComponentType } from "react";

declare module "@lingui/macro" {
  /**
   * allow passing values to <Trans /> component directly
   */
  export const Trans: ComponentType<TransProps & { values: {} }>;
}

Additional context

  • jsLingui version 3.8.1
  • Babel version @babel/core@7.10.1
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.)
    I am using create-react-app with @rescripts/cli in order to change some config without extracting

This is my lingui.config.js:

module.exports = {
  catalogs: [
    {
      path: "src/locales/{locale}/messages",
      include: ["src/"],
      exclude: ["**/node_modules/**"],
    }
  ],
  locales: [
    "en",
    "de",
    "it",
    // ... some more
    "pseudo-LOCALE",
  ],
  format: "po",
  sourceLocale: "en",
  pseudoLocale: "pseudo-LOCALE",
  compileNamespace: "ts",
  fallbackLocales: {
    it: "en",
    // ... some more
  },
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
semoalcommented, Mar 31, 2021

Ah damn, you’re right the type is wrong. https://github.com/lingui/js-lingui/blob/main/packages/macro/index.d.ts#L31

Let me fix this and I’ll release it in a few minutes

0reactions
whytspacecommented, Apr 1, 2021

What version of Typescript are you using mate?

3.9.3

4.2.3 is the latest now. I guess it’s about time that I upgrade. 😃

edit:

The problem was not actually caused by the typescript version. I used an interface instead of a type. Whilst usually almost the same, they differ in behaviour significantly in this case. (read more)

type ICustomObjectType = {
  foo: number;
  bar: number;
}

interface ICustomObjectInterface {
  foo: number;
  bar: number;
}

// WORKS!
const TestType() {
   const values: ICustomObjectType = { foo: 1, bar: 2 }
   return <Trans id="my.test.message" values={values} />;
}

// ERROR!
const TestInterface() {
   const values: ICustomObjectInterface = { foo: 1, bar: 2 }
   return <Trans id="my.test.message" values={values} />;
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot pass several components to <Trans /> #737 - GitHub
I have a text, where i have to pass array values, each of them should be wrapped in bold. Currently the only option...
Read more >
React-i18n Trans Component with translations that contain ...
The only way I managed to make it work was to use Trans like this: ... your JSON file values={{ // The variables...
Read more >
Trans Component - react-i18next documentation
You can pass in variables to get interpolated into the translation string by using objects containing those key:values.
Read more >
react-i18next | Yarn - Package Manager
Starting with v7.0.0 Trans component per default won't add a parent div around content passed as children. It will just return it's children....
Read more >
How to properly internationalize a React application using ...
We have used the Trans component for the first text and the ... pass the i18n instance to react-i18next. .use(initReactI18next) // init ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found