This project was forked from i18next-fetch-backend
and adapted to use axios instead of global.fetch
This is a simple i18next backend to be used in the browser. It will load resources from a backend server using axios.
This backend is most useful when XMLHttpRequest
is not available, such as with Service Worker contexts or when running in a non-shimmed node server environment.
Wiring up:
import i18next from 'i18next';
import axiosBackend from 'i18next-axios-backend';
i18next
.use(axiosBackend)
.init(i18nextOptions);
- As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.
The same options supported by i18next-xhr-backend are supported here, except for those used by XMLHttpRequest
. Instead, you can provide an init
option that will be provided to axios
.
{
// path where resources get loaded from, or a function
// returning a path:
// function(lngs, namespaces) { return customPath; }
// the returned path will interpolate lng, ns if provided like giving a static path
loadPath: '/locales/{{lng}}/{{ns}}.json',
// path to post missing resources
addPath: 'locales/add/{{lng}}/{{ns}}',
// parse data after it has been fetched
// in example use https://www.npmjs.com/package/json5
// here it removes the letter a from the json (bad idea)
parse: function(data) { return data.replace(/a/g, ''); },
// init option for fetch, for example
init: {
timeout: 1000,
auth: {
username: 'janedoe',
password: 's00pers3cret'
}
}
}
Options can be passed in:
preferred - by setting options.backend in i18next.init:
import i18next from 'i18next';
import axiosBackend from 'i18next-axios-backend';
i18next
.use(axiosBackend)
.init({
backend: options
});
on construction:
import AxiosBackend from 'i18next-axios-backend';
const fetch = new AxiosBackend(null, options);
via calling init:
import AxiosBackend from 'i18next-axios-backend';
const axiosBackend = new AxiosBackend();
fetch.init(options);
Because of an issue in how IE used to handle inheritance of static properties, the following is necessary in order to support the old browsers:
import i18next from 'i18next';
import AxiosBackend from 'i18next-axios-backend';
AxiosBackend.type = 'backend';
i18next
.use(AxiosBackend)
.init(/* ... */);