Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

i18next-chained-backend

i18next840.3kMIT4.6.2TypeScript support: included

backend layer for i18next to chain backends

i18next, i18next-backend

readme

Introduction

npm version

This is a i18next backend to chain multiple other backends and caches. So you can define an additional caching backend or fallback backends.

Usage examples for caching configurations can be found here and for fallback backends can be found here.

How it works

For each backend read call (so for each language and namespace combination), it will try to read from from all backends in order until a backend returns some resources. So if it does not find the resource (language + namespace) in the first backend, it goes to the next, etc.

Getting started

Source can be loaded via npm or downloaded from this repo.

# npm package
$ npm install i18next-chained-backend

Wiring up:

import i18next from 'i18next';
import Backend from 'i18next-chained-backend';

i18next
  .use(Backend)
  .init(i18nextOptions);
  • As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.
  • If you don't use a module loader it will be added to window.i18nextChainedBackend

Backend Options

{
  // array of existing i18next backends from https://www.i18next.com/overview/plugins-and-utils#backends
  backends: [],

  // array of options in order of backends above
  backendOptions: []
}

Options can be passed in:

preferred - by setting options.backend in i18next.init:

import i18next from 'i18next';
import Backend from 'i18next-chained-backend';

i18next
  .use(Backend)
  .init({
    backend: options
  });

on construction:

  import Backend from 'i18next-chained-backend';
  const Backend = new Backend(null, options);

via calling init:

  import Backend from 'i18next-chained-backend';
  const Backend = new Backend();
  Backend.init(null, options);

caching example

import i18next from 'i18next';
import Backend from 'i18next-chained-backend';
import LocalStorageBackend from 'i18next-localstorage-backend'; // load from local storage
import HttpApi from 'i18next-http-backend'; // have a own http fallback

i18next
  .use(Backend)
  .init({
    backend: {
      backends: [
        LocalStorageBackend,  // primary
        HttpApi  // fallback
      ],
      backendOptions: [{
        projectId: 'myLocizeProjectId'
      }, {
        loadPath: '/locales/{{lng}}/{{ns}}.json' // http api load path for my own fallback
      }],
      // cacheHitMode: 'none' // (default)
      // cacheHitMode: 'refresh' // tries to refresh the cache by loading from the next backend and updates the cache
      // cacheHitMode: 'refreshAndUpdateStore' // tries to refresh the cache by loading from the next backend, updates the cache and also update the i18next resource store
      // reloadInterval: 60 * 60 * 1000 // can be used to reload resources in a specific interval (useful in server environments)
      // refreshExpirationTime: 7 * 24 * 60 * 60 * 1000 // In case of caching with 'refresh' or 'refreshAndUpdateStore', it will only fetch from the next backend if the cached namespace is expired. Only supported if the backend returns the saved timestamp, like i18next-fs-backend, i18next-localstorage-backend
    }
  });

more complete sample

import i18next from 'i18next';
import Backend from 'i18next-chained-backend';
import Locize from 'i18next-locize-backend'; // load from service
import HttpApi from 'i18next-http-backend'; // have a own http fallback

i18next
  .use(Backend)
  .init({
    backend: {
      backends: [
        Locize,  // primary
        HttpApi  // fallback
      ],
      backendOptions: [{
        projectId: 'myLocizeProjectId'
      }, {
        loadPath: '/locales/{{lng}}/{{ns}}.json' // http api load path for my own fallback
      }]
    }
  });

More locize examples can be found here:

TypeScript

To properly type the backend options, you can import the ChainedBackendOptions interface and use it as a generic type parameter to the i18next's init method, e.g.:

import i18n from 'i18next'
import ChainedBackend, { ChainedBackendOptions } from 'i18next-chained-backend'

i18n
  .use(ChainedBackend)
  .init<ChainedBackendOptions>({
    backend: {
      backends: [
        Locize,  // primary
        HttpApi  // fallback
      ],
      backendOptions: [{
        projectId: 'myLocizeProjectId'
      }, {
        loadPath: '/locales/{{lng}}/{{ns}}.json' // http api load path for my own fallback
      }]
    }

    // other i18next options
  })

IMPORTANT ADVICE for the usage in combination with saveMissing/updateMissing

We suggest not to use mutliple backends in combination with saveMissing or updateMissing, because it may happen, that the trigger for this is based on stale data.


Gold Sponsors


localization as a service - locize.com

Needing a translation management? Want to edit your translations with an InContext Editor? Use the orginal provided to you by the maintainers of i18next!

locize

With using locize you directly support the future of i18next and react-i18next.


changelog

4.6.2

  • fix package exports

4.6.1

  • fix package exports

4.6.0

  • fix: separate cjs and mjs typings

4.5.0

  • introduce refreshExpirationTime

4.4.0

  • introduce reloadInterval

4.3.0

  • introduce cacheHitMode

4.2.0

  • Export the backend options type

4.1.0

  • extend to accept also backends with promise / async-await signature

4.0.1

  • typescript: static type prop

4.0.0

  • typescript fix for i18next v22

3.1.0

  • pass down options to create function

3.0.2

  • build to trigger Deno

3.0.1

  • backendOptions can be optional

3.0.0

  • by default handle empty resources like failed backend reads

2.1.0

  • Type PluginOptions properly 18

2.0.1

  • fixes attempt to save data on invalid response

2.0.0

  • removes deprecated jsnext:main from package.json
  • Bundle all entry points with rollup 8
  • note: dist/es -> dist/esm, dist/commonjs -> dist/cjs (individual files -> one bundled file)

1.0.1

  • typescript: fix types 6

1.0.0

  • typescript: add types 5

0.1.2

  • fixes upper bound for backend accessing in load

0.1.1

  • fixes init call with no options (i18next construct class)

0.1.0

  • initial version