APSL/redux-i18n

Translate constant files

gautamvij opened this issue · 2 comments

Hi.
I have this use case of translating config files which are outside the scope of react. Though currently, I am passing the context.t to these constant .js files from the components and getting the translated labels.

Is there a better way of doing it ?

PS: awesome package !

+1

I have a middleware to show notifications and this is outside of react control.

import Notifications from "react-notification-system-redux"

import {
  PROFILE_UPDATE_PASSWORD_SUCCESS,
  PROFILE_UPDATE_CONTACT_SUCCESS,
} from "../constants"

export default function notificationMiddleware(store) {
  return next => action => {
    let options
    let level

    switch (action.type) {
      case PROFILE_UPDATE_PASSWORD_SUCCESS:
        options = {
          title: "Success!",
          message: "Password has been successfully changed.",
          position: "tr",
        }
        level = "success"
        break
      case PROFILE_UPDATE_CONTACT_SUCCESS:
        options = {
          title: "Success!",
          message: "Contact details have been successfully modified.",
          position: "tr",
        }
        level = "success"
        break
      default:
        options = null
    }

    if (options) {
      store.dispatch(Notifications.show(options, level))
    }
    next(action)
  }
}

Any clever way to translate those messages, and keeping this as a middleware?

In middleware I was able to access translate function with:

import { getTranslateFunction } from "redux-i18n"

export default function notificationMiddleware({ dispatch, getState }) {
  return next => action => {
    const { i18nState } = getState()
    const translate = getTranslateFunction(i18nState.translations, i18nState.lang, "en")
(...)