Skip to main content
Corrected a BIG code typo!
Source Link
cFreed
  • 2.8k
  • 15
  • 20

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 keep initialState 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;
  }
};

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 keep initialState 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(action.locale)) {
        return {
          ...initialState,
          lang: langs(action.locale).lang,
          messages: langs(action.locale).messages
        };
      }
    default:
      return state;
  }
};

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 keep initialState 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[action.locale]) {
        return {
          ...initialState,
          lang: langs[action.locale].lang,
          messages: langs[action.locale].messages
        };
      }
    default:
      return state;
  }
};
Source Link
cFreed
  • 2.8k
  • 15
  • 20

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 keep initialState 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(action.locale)) {
        return {
          ...initialState,
          lang: langs(action.locale).lang,
          messages: langs(action.locale).messages
        };
      }
    default:
      return state;
  }
};