Skip to content

An i18next backend to chain multiple backends (add fallbacks, caches, ...)

License

Notifications You must be signed in to change notification settings

i18next/i18next-chained-backend

Repository files navigation

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.


About

An i18next backend to chain multiple backends (add fallbacks, caches, ...)

Resources

License

Stars

Watchers

Forks

Packages

No packages published