-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #609 from langpavel/feature/react-intl
react-intl
- Loading branch information
Showing
39 changed files
with
963 additions
and
35 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Action creators | ||
|
||
Action Creators should go there |
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,55 @@ | ||
import fetch from '../core/fetch'; | ||
import { | ||
SET_LOCALE_START, | ||
SET_LOCALE_SUCCESS, | ||
SET_LOCALE_ERROR, | ||
} from '../constants'; | ||
|
||
export function setLocale({ locale }) { | ||
return async (dispatch) => { | ||
dispatch({ | ||
type: SET_LOCALE_START, | ||
payload: { | ||
locale, | ||
}, | ||
}); | ||
|
||
try { | ||
const resp = await fetch('/graphql', { | ||
method: 'post', | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: `query{intl(locale:${JSON.stringify(locale)}){id,message}}`, | ||
}), | ||
credentials: 'include', | ||
}); | ||
if (resp.status !== 200) throw new Error(resp.statusText); | ||
const { data } = await resp.json(); | ||
const messages = data.intl.reduce((msgs, msg) => { | ||
msgs[msg.id] = msg.message; // eslint-disable-line no-param-reassign | ||
return msgs; | ||
}, {}); | ||
dispatch({ | ||
type: SET_LOCALE_SUCCESS, | ||
payload: { | ||
locale, | ||
messages, | ||
}, | ||
}); | ||
} catch (error) { | ||
dispatch({ | ||
type: SET_LOCALE_ERROR, | ||
payload: { | ||
locale, | ||
error, | ||
}, | ||
}); | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
} |
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,11 @@ | ||
import { SET_RUNTIME_VARIABLE } from '../constants'; | ||
|
||
export function setRuntimeVariable({ name, value }) { | ||
return { | ||
type: SET_RUNTIME_VARIABLE, | ||
payload: { | ||
name, | ||
value, | ||
}, | ||
}; | ||
} |
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
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,42 @@ | ||
/* eslint-disable no-shadow */ | ||
|
||
import React, { PropTypes } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { setLocale } from '../../actions/intl'; | ||
|
||
function LanguageSwitcher({ currentLocale, availableLocales, setLocale }) { | ||
const isSelected = locale => locale === currentLocale; | ||
return ( | ||
<div> | ||
{availableLocales.map(locale => ( | ||
<span key={locale}> | ||
{isSelected(locale) ? ( | ||
<span>{locale}</span> | ||
) : ( | ||
<a | ||
href={`?locale=${locale}`} | ||
onClick={(e) => { | ||
setLocale({ locale }); | ||
e.preventDefault(); | ||
}} | ||
>{locale}</a> | ||
)} | ||
{' '} | ||
</span> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
LanguageSwitcher.propTypes = { | ||
currentLocale: PropTypes.string.isRequired, | ||
availableLocales: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
setLocale: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default connect(state => ({ | ||
availableLocales: state.runtime.availableLocales, | ||
currentLocale: state.intl.locale, | ||
}), { | ||
setLocale, | ||
})(LanguageSwitcher); |
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,6 @@ | ||
{ | ||
"name": "LanguageSwitcher", | ||
"version": "0.0.0", | ||
"private": true, | ||
"main": "./LanguageSwitcher.js" | ||
} |
Oops, something went wrong.