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.

Translating defaultProps with t()

See original GitHub issue

Describe the bug When defaultProps are translated with t(), the translated value is never shown.

To Reproduce

For example, here is a simple component.


import React from 'react';
import { IonLoading } from '@ionic/react';
import { t } from '@lingui/macro';

interface MyProps {
  textToShow?: string,
}

const Loading: React.VFC<MyProps> = ({ textToShow }: MyProps) => (
  <IonLoading
    isOpen
    message={textToShow}
  />
);

Loading.defaultProps = {
  textToShow: t({ id: 'loading.text', message: 'Getting Ready...' }),
};

export default Loading;

Run lingui extract. Translate loading.text to something else, such as “Getting ready translated”. Run lingui compile. Change the app language and cause the loading component to trigger. The text will be Getting Ready..., not Getting ready translated.

Expected behavior defaultProps values should use the translation, not the original text.

  • jsLingui version lingui --version 3.8.3
  • Babel version npm list @babel/core @babel/core@7.12.3
  • Your Babel config (e.g. .babelrc) or framework you use (Create React App, Meteor, etc.) create-react-app via Ionic React v5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ptmkennycommented, Apr 12, 2021

Wow, thank you so much, this is very helpful.

This thorough response makes me very glad I chose lingui from among the many i18n js options. 😄

0reactions
sapandiwakarcommented, Apr 26, 2021

For anyone coming here searching to localize defaultProps, there is a small mistake in https://github.com/lingui/js-lingui/issues/1037#issuecomment-816633278. You should use defineMessage inside defaultProps (since this is an object that is evaluated only once) object and then translate the message inside the component like this:

import React from 'react';
import { IonLoading } from '@ionic/react';
import { defineMessage } from '@lingui/macro';
import { i18n } from '@lingui/core';

interface MyProps {
  textToShow?: string,
}

const Loading: React.VFC<MyProps> = ({ textToShow }: MyProps) => (
  <IonLoading
    isOpen
    message={typeof textToShow === 'string' ? textToShow : i18n._(textToShow)}
  />
);

Loading.defaultProps = {
  textToShow: defineMessage({
    id: 'loading.text',
    message: 'Getting Ready...',
  }),
};

export default Loading;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Translate defaultProps not working as before for ... - GitHub
We mostly use class component in numbers of files, we use a file called component.js for the component and index.js for all the...
Read more >
Default Props in React/TypeScript - DEV Community ‍ ‍
And inside getLetterArrayFromOptionalString() , I'm trying to split() that string into an array of letters. But TS won't compile this. It ...
Read more >
Using a function in React defaultprops is returning an object
Im having issues when reading defaultprops of where I use a translation function to read a value mapped to a labelkey. In this...
Read more >
Translating the default value of a prop - Get Help - Vue Forum
I'm using vue-i18n to handle localization in my app. All going well, until I hit this problem that I can't immediately find a...
Read more >
Translation (render prop) - react-i18next documentation
The Translation is a render prop and gets the t function and i18n instance to your component. ... export function MyComponent() {. return...
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