react-i18next v10 lazy loading does not work
See original GitHub issueJust updated react-i18next to version 10 and after renaming all withNamespaces tot withTranslation I noticed that lazy loading was broken. I created a little sample to reproduce:
const Foo = withTranslation(['foo'])(({ t }: WithTranslation) => {
return (<div>{t('foo:text')}</div>);
});
const Bar = withTranslation(['bar'])(({ t }: WithTranslation) => {
return (<div>{t('bar:text')}</div>);
});
class Test extends React.PureComponent<{}> {
state = {
visible: false,
};
constructor(props: {}) {
super(props);
this.onClick = this.onClick.bind(this);
}
render() {
const { visible } = this.state;
return (
<div>
<button onClick={this.onClick}>Click</button>
{visible ? <Bar/> : <Foo/>}
</div>
);
}
private onClick() {
this.setState({
visible: true,
});
}
}
Now, when running I see the translated text ‘foo’. When I click the button it renders the Foo component but I see the translation key ‘text’ instead. Before, It downloaded the new translation file (foo.json) and translated correctly.
My config looks like:
import i18n from 'i18next';
import Backend from 'i18next-xhr-backend';
import { initReactI18next } from 'react-i18next';
i18n
.use(Backend)
.use(initReactI18next)
.init({
lng: 'en',
fallbackLng: 'en',
ns: ['general'],
defaultNS: 'general',
react: {
wait: true,
},
});
Reverting back to react-i18next v9 works as expected with this example (except that I need to use the old 'reactI18nextModule ’ instead of the new ‘initReactI18next’)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Add lazy loading to React i18next with React Suspense
Error: withI18nextTranslation(Unknown) suspended while rendering, but no fallback UI was specified. Step 1: Install i18n-xhr-backend NPM module. First you will ...
Read more >Step by step guide (v9) - react-i18next documentation
a) Adding lazy loading for translations All needed to be done is adding another package called i18next-http-backend and using that.
Read more >next-i18next - npm
Because resources are loaded once when the server is started, any changes made to your translation JSON files in development will not be...
Read more >lazy loading on i18n react questions - Stack Overflow
I have been throug the documentation and tried the 'useTranslation (hook)' and 'withTranslation', both dont seem to work since they load all the ......
Read more >Upgrading from AngularJS to Angular
This is a much neater way to organize things than a small number of large files, but it doesn't work that well if...
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 Free
Top 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
Guess i know what is going on…will need to check what i can do to fix this … later today
@MartinGK you’re aware that
withTranslation
is a HOC and uses a hookuseTranslation
- reading docs might help