This repository has been archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Language switcher using the Context API
Makes it possible to switch language using the top right corner dropdown. Note this uses a new `LanguageUpdate` component yet to be unit tested. closes #61 and closes #62
- Loading branch information
1 parent
67ba5e7
commit 1b6292c
Showing
8 changed files
with
108 additions
and
24 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import React from 'react'; | ||
import { IntlProvider } from 'react-intl'; | ||
import { HashRouter as Router } from 'react-router-dom'; | ||
import './App.css'; | ||
import { IntlContext } from './contexts/IntlContext'; | ||
import Headers from './components/Headers'; | ||
import Footers from './components/Footers'; | ||
import Container from './components/Container'; | ||
import { messages } from './utils/locales'; | ||
|
||
|
||
const App = () => ( | ||
<Router> | ||
<div className="App"> | ||
<Headers /> | ||
<Container /> | ||
<Footers /> | ||
</div> | ||
</Router> | ||
); | ||
const App = () => { | ||
const [locale] = React.useContext(IntlContext); | ||
return ( | ||
<Router> | ||
<IntlProvider locale={locale} messages={messages[locale]}> | ||
<div className="App"> | ||
<Headers /> | ||
<Container /> | ||
<Footers /> | ||
</div> | ||
</IntlProvider> | ||
</Router> | ||
); | ||
}; | ||
|
||
export default App; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { IntlProvider } from 'react-intl'; | ||
import IntlContextProvider from './contexts/IntlContext'; | ||
import App from './App'; | ||
|
||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render(<IntlProvider locale="en"><App /></IntlProvider>, div); | ||
ReactDOM.render(<IntlContextProvider><App /></IntlContextProvider>, div); | ||
ReactDOM.unmountComponentAtNode(div); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import { arrayOf, func, string } from 'prop-types'; | ||
import { IntlContext } from '../contexts/IntlContext'; | ||
import { locales } from '../utils/locales'; | ||
|
||
const DropdownItem = ({ text, onClick }) => ( | ||
<button className="dropdown-item" type="button" onClick={() => onClick(text)}>{text}</button> | ||
); | ||
DropdownItem.propTypes = { | ||
text: string.isRequired, | ||
onClick: func.isRequired, | ||
}; | ||
|
||
const DropdownMenu = ({ items, onClick }) => ( | ||
<div className="dropdown-menu" aria-labelledby="navbarDropdown"> | ||
{items.map(item => <DropdownItem text={item} onClick={onClick} key={item} />)} | ||
</div> | ||
); | ||
DropdownMenu.propTypes = { | ||
items: arrayOf(string).isRequired, | ||
onClick: func.isRequired, | ||
}; | ||
|
||
const LanguageUpdate = () => { | ||
const [locale, setLocale] = React.useContext(IntlContext); | ||
|
||
return ( | ||
<li className="nav-item dropdown"> | ||
<button | ||
className="nav-link btn button-link dropdown-toggle" | ||
id="navbarDropdown" | ||
data-toggle="dropdown" | ||
aria-haspopup="true" | ||
aria-expanded="false" | ||
type="button" | ||
> | ||
<i className="fas fa-flag" /> | ||
{locale.toUpperCase()} | ||
</button> | ||
<DropdownMenu | ||
items={locales} | ||
onClick={newLocale => setLocale(newLocale)} | ||
/> | ||
</li> | ||
); | ||
}; | ||
|
||
export default LanguageUpdate; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { element } from 'prop-types'; | ||
|
||
|
||
export const IntlContext = React.createContext(); | ||
|
||
const IntlContextProvider = ({ children }) => { | ||
const [locale, setLocale] = React.useState('en'); | ||
return ( | ||
<IntlContext.Provider value={[locale, setLocale]}> | ||
{children} | ||
</IntlContext.Provider> | ||
); | ||
}; | ||
IntlContextProvider.propTypes = { | ||
children: element.isRequired, | ||
}; | ||
|
||
export default IntlContextProvider; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import messagesEs from '../translations/es.json'; | ||
import messagesFr from '../translations/fr.json'; | ||
|
||
|
||
const messages = { | ||
en: null, | ||
es: messagesEs, | ||
fr: messagesFr, | ||
}; | ||
const locales = Object.keys(messages); | ||
|
||
export { messages, locales }; |