next.js: Logic of with-react-i18next is incompatible with _app.js

Examples bug report / feature request

Example name

Describe the bug / feature request

Looks like with18next() HOC in with-react-i18next example is incompatible with _app.js (added in Next v6). Although wrapping each page into the HOC is still possible, this approach has at least these two drawbacks:

  • The layout defined in _app.js cannot be translated since the components end up above with18next(['ns'])(PageComponent).
  • Going between pages flushes the DOM tree since with18next() creates separate wrapper components on each page (I may be not 100% here).

The with-apollo example has been updated recently and ApolloProvider is now a part of _app.js, which is pretty neat. It’d be great if with-react-i18next could undergo a similar transformation. I’ve just tried to propose this change myself, but am giving up after over three hours 😅

There’s one caveat that should be taken into account when implementing the new logic: a collection of namespaces used by a page needs to be passed into _app.js. I suggest that these namespaces can be either sourced from getInitialProps():

export default class MyPage extends Component {
  static getInitialProps() {
    return { i18nextNamespaces: ['ns1', 'ns2'] };
  }
  render() {
    return <div>{this.props.t('hello')}</div>;
  }
}

Alternatively, i18nextNamespaces can be defined in a static property to avoid implementing getInitialProps() in the simplest cases:

export default class MyPage extends Component {
  static public i18nextNamespaces = ['ns1', 'ns2'];
  render() {
    return <div>{this.props.t('hello')}</div>;
  }
}
const MyPage = ({t}) => <div>{t('hello')}</div>;
MyPage.i18nextNamespaces = ['ns1', 'ns2'];
export default MyPage;

Ideally, these namespaces should be detected automatically depending on which ones are used down the tree (translate(['ns1'])(Component1), translate(['ns2', 'ns3'])(Component2)). This magic behaviour would be similar to what Apollo does with executing all queries in the tree on the server and building client-side cache based on that. However, I’m not sure how easy it would be to implement this trick in react-i18next.

System information

  • Version of Next.js: 6.0.3

cc @jamuhl

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 41 (27 by maintainers)

Most upvoted comments

This seems to work okay:

https://github.com/ckeeney/with-react-i18next-app

It has some hackish stuff that may or may not be needed now, but I needed to take a break and just wanted to post my progress here.

@MaksRamenskiy @LiangNex @imagine10255 @slorenzo @kachkaev anyone had time to look deeper into - https://github.com/i18next/react-i18next/tree/master/example/nextjs_withAppJS

Would be great if we find a solution to this and update the sample provided here.

When the layout is declared inside the page component, all dom nodes end up flushed, which may be problematic due to loss of react state or for performance reasons.

Thank you for honestly sharing your thoughts @jamuhl. I’ll ping you if I give the investigation another go and realize that i18next needs tweaking.

I guess I’ll have to move Apollo back to page wrapper for now since i18next does not work well inside it.

@jamuhl I tried you example (nextjs_withAppJS) and got error:

Warning: Did not expect server HTML to contain a <div> in <div>.

@MaksRamenskiy @LiangNex @imagine10255 @slorenzo @kachkaev

I removed the typescript stuff from i18n.js (having that default and instance export - so there is no mix depending on server or webpack - and made a new sample to checkout here: https://github.com/i18next/react-i18next/tree/master/example/nextjs_withAppJS

Feedback if you still encounter any issues on that with the changes done would be welcome … if working i will update the sample here - if not PR on the sample would be welcome (or changes so i see the problem - as at my machine all seems to work)

@imagine10255 @kachkaev this _app.js thing is a little weird - or i do not really understand it to much…a little pain as pageProps are rather inconsistent.

What i came up with - and seems to work - while not overly elegant (@kachkaev i’m sure you could improve that) is using render props and dependent on pageProps has i18n (i guess that’s server render) or not using that or the …/i18n -> from root folder:

import App, { Container } from "next/app";
import { I18n as I18nR } from "react-i18next";
import { I18n } from "../i18n";

export default class MyApp extends App {
  render() {
    const { Component, pageProps } = this.props;
    return (
      <Container>
        <I18nR ns="common" i18n={pageProps && pageProps.i18n || I18n}>
          {
            (t) => (
              <div>
                <h1>{t('common:integrates_react-i18next')}</h1>
                <Component {...pageProps} />
              </div>
            )
          }
        </I18nR>
      </Container>
    );
  }
}