2018-05-09 13:06:37 +00:00
|
|
|
import { translate, loadNamespaces } from 'react-i18next'
|
2018-03-07 07:42:14 +00:00
|
|
|
import { getInitialProps, I18n } from '../i18n'
|
2018-03-05 11:46:13 +00:00
|
|
|
|
|
|
|
export const withI18next = (namespaces = ['common']) => ComposedComponent => {
|
2018-03-07 07:42:14 +00:00
|
|
|
const Extended = translate(namespaces, { i18n: I18n, wait: process.browser })(
|
2018-03-05 11:46:13 +00:00
|
|
|
ComposedComponent
|
|
|
|
)
|
|
|
|
|
|
|
|
Extended.getInitialProps = async (ctx) => {
|
|
|
|
const composedInitialProps = ComposedComponent.getInitialProps
|
|
|
|
? await ComposedComponent.getInitialProps(ctx)
|
|
|
|
: {}
|
|
|
|
|
2018-05-09 13:06:37 +00:00
|
|
|
const i18nInitialProps = ctx.req
|
|
|
|
? getInitialProps(ctx.req, namespaces)
|
|
|
|
: await loadNamespaces({
|
|
|
|
components: [{ props: { namespaces } }],
|
|
|
|
i18n: I18n,
|
|
|
|
});
|
2018-03-05 11:46:13 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...composedInitialProps,
|
|
|
|
...i18nInitialProps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Extended
|
|
|
|
}
|