Translating defaultProps with t()
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. 😄
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 usedefineMessage
insidedefaultProps
(since this is an object that is evaluated only once) object and then translate the message inside the component like this: