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.

react-i18next v10 lazy loading does not work

See original GitHub issue

Just 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jamuhlcommented, Feb 11, 2019

Guess i know what is going on…will need to check what i can do to fix this … later today

0reactions
jamuhlcommented, Oct 15, 2020

@MartinGK you’re aware that withTranslation is a HOC and uses a hook useTranslation - reading docs might help

Read more comments on GitHub >

github_iconTop 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 >

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