I don't know redux at all, so I might have missed something important so that my suggestion may be naive and doesn't work.
If so, my apologies, and don't shoot me too hard :)
That said, here is how I'd work:
- first add a registry for the currently added langs
- then merely use the right one depending on
action.locale
if it exists in registry, or keepinitialState
if not
Actually it results in something like this:
import { FRENCH } from '../../public/messages/fr';
import { ENGLISH } from '../../public/messages/en';
const initialState = {
lang: FRENCH.lang,
messages: FRENCH.messages
};
const langs = {
fr: FRENCH,
en: ENGLISH
}
export const localeReducer = (state = initialState, action) => {
switch (action.type) {
case 'LOCALE_SELECTED':
if (langs(actionlangs[action.locale)locale]) {
return {
...initialState,
lang: langs(actionlangs[action.locale)locale].lang,
messages: langs(actionlangs[action.locale)locale].messages
};
}
default:
return state;
}
};