// ./app/services/translations
import i18n from 'react-i18next';

const getSupportedLocales = () => i18n.getLanguages()

export const integrate = (namespace, objects, id, keys) => {
    let lngs = Languages;
    let translations = { }
    lngs.map((lng) => translations[lng] = {});
    objects.map((object) => {
        let objId = object[id];
        keys.map((key) => {
            lngs.map((lng) => translations[lng][`${key}_${objId}`] = object[`${key}_${lng}`])
        })
    })
    lngs.map((lng) => i18n.addResources(lng, namespace, translations[lng]));
}

export const Languages = getSupportedLocales()
// ./app/sagas/startup.js

import Translation from '../services/translations'

function* fetch() {
    const {countries} = yield call(API.fetchCountries);
    yield call(Translation.integrate, 'country', countries, 'code', ['name', 'prefix'])
}

JSFiddle

If API response in a preferred way, say

let videos = [
  {
      video_id: '123',
      duration: 456,
      zh: {
          title: 'ZHZH',
        actors: ['ZH1', 'ZH2', 'ZH3'],
        subtitle: 'ZHZHZH'
      },
      en: {
          title: 'ENEN',
        actors: ['EN1', 'EN2', 'EN3']
      }
  },
]

JSFiddle

results matching ""

    No results matching ""