Skip to content

Instantly share code, notes, and snippets.

@egebilican
Created March 27, 2019 14:49
Show Gist options
  • Save egebilican/edcec97e6dcd4c0243f2d9a3abb05bdb to your computer and use it in GitHub Desktop.
Save egebilican/edcec97e6dcd4c0243f2d9a3abb05bdb to your computer and use it in GitHub Desktop.
INTL-IntlProviderComponent
import React, { PureComponent } from "react";
import { IntlProvider, addLocaleData } from "react-intl";
import trLocaleData from "react-intl/locale-data/tr";
const trTranslationMessages = require("../translations/tr.json");
const enTranslationMessages = require("../translations/en.json");
addLocaleData(trLocaleData);
const messages = {
tr: trTranslationMessages,
en: enTranslationMessages
};
export default class IntlProviderComponent extends PureComponent {
constructor(props) {
super(props);
this.state = {
locale: "tr"
};
}
setLocale(locale) {
this.setState(() => ({ locale }));
}
render() {
return (
<IntlProvider locale={this.state.locale} messages={messages[this.state.locale]}>
<>
<button onClick={() => this.setLocale("tr")}>TR</button>
<button onClick={() => this.setLocale("en")}>EN</button>
{this.props.children}
</>
</IntlProvider>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment