-
Notifications
You must be signed in to change notification settings - Fork 12
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 #22 from keremcubuk/react-intl
React intl
- Loading branch information
Showing
55 changed files
with
2,387 additions
and
146 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
This file was deleted.
Oops, something went wrong.
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,13 +1,7 @@ | ||
import { makeSelectLocation } from 'containers/App/selectors'; | ||
// import { makeSelectApp } from 'containers/App/selectors'; | ||
|
||
describe('makeSelectLocation', () => { | ||
describe('makeSelectApp', () => { | ||
it('should select the location', () => { | ||
const router = { | ||
location: { pathname: '/foo' }, | ||
}; | ||
const mockedState = { | ||
router, | ||
}; | ||
expect(makeSelectLocation()(mockedState)).toEqual(router.location); | ||
expect(true).toEqual(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
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 @@ | ||
/* | ||
* | ||
* LanguageToggle | ||
* | ||
*/ | ||
|
||
import React from 'react'; | ||
import { View, Picker } from 'react-native'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { createSelector } from 'reselect'; | ||
|
||
import { appLocales } from 'app/i18n'; | ||
import { changeLocale } from 'containers/LanguageProvider/actions'; | ||
import { makeSelectLocale } from 'containers/LanguageProvider/selectors'; | ||
import messages from './messages'; | ||
|
||
export function LocaleToggle(props) { | ||
return ( | ||
<View> | ||
<Picker | ||
selectedValue={props.locale} | ||
style={{ height: 50, width: 50 }} | ||
onValueChange={itemValue => props.onLocaleToggle(itemValue)}> | ||
{appLocales.map(locale => ( | ||
<Picker.Item key={locale} label={messages[locale].defaultMessage} value={locale} /> | ||
))} | ||
</Picker> | ||
</View> | ||
); | ||
} | ||
|
||
LocaleToggle.propTypes = { | ||
onLocaleToggle: PropTypes.func, | ||
locale: PropTypes.string, | ||
}; | ||
|
||
const mapStateToProps = createSelector( | ||
makeSelectLocale(), | ||
locale => ({ | ||
locale, | ||
}), | ||
); | ||
|
||
export function mapDispatchToProps(dispatch) { | ||
return { | ||
onLocaleToggle: locale => dispatch(changeLocale(locale)), | ||
dispatch, | ||
}; | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps, | ||
)(LocaleToggle); |
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,14 @@ | ||
/* | ||
* | ||
* LanguageProvider actions | ||
* | ||
*/ | ||
|
||
import { CHANGE_LOCALE } from './constants'; | ||
|
||
export function changeLocale(languageLocale) { | ||
return { | ||
type: CHANGE_LOCALE, | ||
locale: languageLocale, | ||
}; | ||
} |
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,7 @@ | ||
/* | ||
* | ||
* LanguageProvider constants | ||
* | ||
*/ | ||
|
||
export const CHANGE_LOCALE = 'app/LanguageToggle/CHANGE_LOCALE'; |
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,44 @@ | ||
/* | ||
* | ||
* LanguageProvider | ||
* | ||
* this component connects the redux state language locale to the | ||
* IntlProvider component and i18n messages (loaded from `app/translations`) | ||
*/ | ||
|
||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { connect } from 'react-redux'; | ||
import { createSelector } from 'reselect'; | ||
import { IntlProvider } from 'react-intl'; | ||
|
||
import { makeSelectLocale } from './selectors'; | ||
|
||
global.Intl = require('intl'); | ||
|
||
export function LanguageProvider(props) { | ||
return ( | ||
<IntlProvider | ||
locale={props.locale} | ||
key={props.locale} | ||
messages={props.messages[props.locale]} | ||
textComponent={React.Fragment}> | ||
{React.Children.only(props.children)} | ||
</IntlProvider> | ||
); | ||
} | ||
|
||
LanguageProvider.propTypes = { | ||
locale: PropTypes.string, | ||
messages: PropTypes.object, | ||
children: PropTypes.element.isRequired, | ||
}; | ||
|
||
const mapStateToProps = createSelector( | ||
makeSelectLocale(), | ||
locale => ({ | ||
locale, | ||
}), | ||
); | ||
|
||
export default connect(mapStateToProps)(LanguageProvider); |
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 @@ | ||
/* | ||
* LocaleToggle Messages | ||
* | ||
* This contains all the text for the LanguageToggle component. | ||
*/ | ||
import { defineMessages } from 'react-intl'; | ||
|
||
export const scope = 'app.containers.LocaleToggle'; | ||
|
||
export default defineMessages({ | ||
en: { | ||
id: `${scope}.en`, | ||
defaultMessage: 'en', | ||
}, | ||
fr: { | ||
id: `${scope}.fr`, | ||
defaultMessage: 'fr', | ||
}, | ||
}); |
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,26 @@ | ||
/* | ||
* | ||
* LanguageProvider reducer | ||
* | ||
*/ | ||
|
||
import produce from 'immer'; | ||
|
||
import { CHANGE_LOCALE } from './constants'; | ||
import { DEFAULT_LOCALE } from '../../i18n'; | ||
|
||
export const initialState = { | ||
locale: DEFAULT_LOCALE, | ||
}; | ||
|
||
/* eslint-disable default-case, no-param-reassign */ | ||
const languageProviderReducer = (state = initialState, action) => | ||
produce(state, draft => { | ||
switch (action.type) { | ||
case CHANGE_LOCALE: | ||
draft.locale = action.locale; | ||
break; | ||
} | ||
}); | ||
|
||
export default languageProviderReducer; |
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,20 @@ | ||
import { createSelector } from 'reselect'; | ||
import { initialState } from './reducer'; | ||
|
||
/** | ||
* Direct selector to the language domain | ||
*/ | ||
|
||
const selectLanguage = state => state.language || initialState; | ||
|
||
/** | ||
* Select the language locale | ||
*/ | ||
|
||
const makeSelectLocale = () => | ||
createSelector( | ||
selectLanguage, | ||
languageState => languageState.locale, | ||
); | ||
|
||
export { selectLanguage, makeSelectLocale }; |
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,15 @@ | ||
import { changeLocale } from '../actions'; | ||
|
||
import { CHANGE_LOCALE } from '../constants'; | ||
|
||
describe('LanguageProvider actions', () => { | ||
describe('Change Local Action', () => { | ||
it('has a type of CHANGE_LOCALE', () => { | ||
const expected = { | ||
type: CHANGE_LOCALE, | ||
locale: 'de', | ||
}; | ||
expect(changeLocale('de')).toEqual(expected); | ||
}); | ||
}); | ||
}); |
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 { render } from '@testing-library/react'; | ||
import { FormattedMessage, defineMessages } from 'react-intl'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import ConnectedLanguageProvider, { LanguageProvider } from '../index'; | ||
import configureStore from '../../../configureStore'; | ||
|
||
import { translationMessages } from '../../../i18n'; | ||
|
||
const messages = defineMessages({ | ||
someMessage: { | ||
id: 'some.id', | ||
defaultMessage: 'This is some default message', | ||
en: 'This is some en message', | ||
}, | ||
}); | ||
|
||
describe('<LanguageProvider />', () => { | ||
it('should render its children', () => { | ||
const children = <h1>Test</h1>; | ||
const { container } = render( | ||
<LanguageProvider messages={messages} locale="en"> | ||
{children} | ||
</LanguageProvider>, | ||
); | ||
expect(container.firstChild).not.toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('<ConnectedLanguageProvider />', () => { | ||
let store; | ||
|
||
beforeAll(() => { | ||
store = configureStore({}); | ||
}); | ||
|
||
it('should render the default language messages', () => { | ||
const { queryByText } = render( | ||
<Provider store={store}> | ||
<ConnectedLanguageProvider messages={translationMessages}> | ||
<FormattedMessage {...messages.someMessage} /> | ||
</ConnectedLanguageProvider> | ||
</Provider>, | ||
); | ||
expect(queryByText(messages.someMessage.defaultMessage)).not.toBeNull(); | ||
}); | ||
}); |
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,22 @@ | ||
import languageProviderReducer from '../reducer'; | ||
import { CHANGE_LOCALE } from '../constants'; | ||
|
||
/* eslint-disable default-case, no-param-reassign */ | ||
describe('languageProviderReducer', () => { | ||
it('returns the initial state', () => { | ||
expect(languageProviderReducer(undefined, {})).toEqual({ | ||
locale: 'en', | ||
}); | ||
}); | ||
|
||
it('changes the locale', () => { | ||
expect( | ||
languageProviderReducer(undefined, { | ||
type: CHANGE_LOCALE, | ||
locale: 'de', | ||
}), | ||
).toEqual({ | ||
locale: 'de', | ||
}); | ||
}); | ||
}); |
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 { selectLanguage } from '../selectors'; | ||
|
||
describe('selectLanguage', () => { | ||
it('should select the global state', () => { | ||
const globalState = {}; | ||
const mockedState = { | ||
language: globalState, | ||
}; | ||
expect(selectLanguage(mockedState)).toEqual(globalState); | ||
}); | ||
}); |
Oops, something went wrong.