I'm using apollo for authentication of my nextJS application. Now I need to add i18n support and I'm facing some basic problems:
Mainly the problem is to handle class Index extends React.Component, which is how my page and the components looks like vs. the function version used in the example.
In the documentation there is this example how to implement i18next to a page of the application:
import React from 'react';
import Link from 'next/link';
import { translate } from 'react-i18next';
import i18n from '../i18n';
function Index({ t, initialI18nStore }) {
return (
<div>
{t('welcome')}
<p>{t('common:integrates_react-i18next')}</p>
<Link href="/page2"><a>{t('link.gotoPage2')}</a></Link>
</div>
);
}
const Extended = translate(['home', 'common'], { i18n, wait: process.browser })(Index);
// Passing down initial translations
// use req.i18n instance on serverside to avoid overlapping requests set the language wrong
Extended.getInitialProps = async ({ req }) => {
if (req && !process.browser) return i18n.getInitialProps(req, ['home', 'common']);
return {};
};
export default Extended;
But my page is using class Index extends React.Component. So I don't know how to implement t and initialI18nStore into my component.
Also I do not understand how to add the getInitialProps into my existing one:
Page
import React from 'react'
import cookie from 'cookie'
import { withApollo, compose } from 'react-apollo'
import withData from '../lib/withData'
import redirect from '../lib/redirect'
import checkLoggedIn from '../lib/checkLoggedIn'
class Index extends React.Component {
static async getInitialProps (context, apolloClient) {
const { loggedInUser } = await checkLoggedIn(context, apolloClient)
if (!loggedInUser.user) {
// If not signed in, send them somewhere more useful
redirect(context, '/signin')
}
return { loggedInUser }
}
render () {
return (
<div>
Hello {this.props.loggedInUser.user.name}!<br />
<button onClick={this.signout}>Sign out</button>
</div>
)
}
};
export default compose(
withData,
withApollo
)(Index)