Skip to content

Instantly share code, notes, and snippets.

@theodesp
Last active April 6, 2020 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodesp/e6594c6e1729d6496d19105dcc990157 to your computer and use it in GitHub Desktop.
Save theodesp/e6594c6e1729d6496d19105dcc990157 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { I18nextProvider } from 'react-i18next';
import { I18nProvider } from '@wapps/gatsby-i18n';
import { PhraseAppProvider } from 'react-i18next-phraseapp';
import setupI18next from './setupI18next';
const withI18nextPhraseApp = (options = {}) => Comp => {
class I18n extends Component {
constructor(props) {
super(props);
const { pageContext } = props;
this.i18n = setupI18next(
pageContext.fallbackLng,
pageContext.i18nextOptions,
);
this.activateLng();
}
activateLng = () => {
const { data, pageContext } = this.props;
if (data.locales) {
data.locales.edges.forEach(({ node }) => {
const { lng, ns, data } = node;
if (!this.i18n.hasResourceBundle(lng, ns)) {
this.i18n.addResources(lng, ns, JSON.parse(data));
}
});
}
this.i18n.changeLanguage(pageContext.lng);
};
componentDidUpdate(prevProps) {
if (this.props.pageContext.lng !== prevProps.pageContext.lng) {
this.activateLng();
}
}
render() {
return (
<I18nextProvider i18n={this.i18n}>
<PhraseAppProvider config={ window.PHRASEAPP_CONFIG }>
<I18nProvider {...this.props.pageContext}>
<Comp {...this.props} />
</I18nProvider>
</PhraseAppProvider>
</I18nextProvider>
);
}
}
return I18n;
};
export default withI18nextPhraseApp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment