Created
March 27, 2019 14:49
-
-
Save egebilican/edcec97e6dcd4c0243f2d9a3abb05bdb to your computer and use it in GitHub Desktop.
INTL-IntlProviderComponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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