Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
i18next-chained-backend
Advanced tools
The i18next-chained-backend package is a backend layer for i18next that allows chaining multiple backends. This is useful for scenarios where you want to load translations from multiple sources, such as a local file system, a remote server, or a database, in a specific order.
Chaining Multiple Backends
This feature allows you to chain multiple backends together. In this example, the translations are first attempted to be loaded from the local storage. If they are not found there, the HTTP backend is used as a fallback to load the translations from a remote server.
const i18next = require('i18next');
const ChainedBackend = require('i18next-chained-backend').default;
const HttpBackend = require('i18next-http-backend').default;
const LocalStorageBackend = require('i18next-localstorage-backend').default;
i18next.use(ChainedBackend).init({
backend: {
backends: [
LocalStorageBackend, // primary
HttpBackend // fallback
],
backendOptions: [
{ /* options for local storage */ },
{ loadPath: '/locales/{{lng}}/{{ns}}.json' } // options for http backend
]
}
});
Custom Backend Order
This feature allows you to define a custom order for the backends. In this example, a custom backend is used as the primary source, followed by local storage, and finally the HTTP backend as a fallback.
const i18next = require('i18next');
const ChainedBackend = require('i18next-chained-backend').default;
const HttpBackend = require('i18next-http-backend').default;
const LocalStorageBackend = require('i18next-localstorage-backend').default;
const CustomBackend = require('./myCustomBackend').default;
i18next.use(ChainedBackend).init({
backend: {
backends: [
CustomBackend, // primary
LocalStorageBackend, // secondary
HttpBackend // fallback
],
backendOptions: [
{ /* options for custom backend */ },
{ /* options for local storage */ },
{ loadPath: '/locales/{{lng}}/{{ns}}.json' } // options for http backend
]
}
});
The i18next-http-backend package is used to load translations from a remote server via HTTP. It is useful for applications that need to fetch translations dynamically from a server. Compared to i18next-chained-backend, it does not support chaining multiple backends but can be used as one of the backends in a chained setup.
The i18next-localstorage-backend package is used to store translations in the browser's local storage. This can improve performance by reducing the need to fetch translations from a remote server on every page load. Similar to i18next-http-backend, it does not support chaining but can be used as part of a chained backend setup.
The i18next-fs-backend package is used to load translations from the file system. This is particularly useful for server-side applications or during development. It does not support chaining multiple backends but can be used in conjunction with i18next-chained-backend to provide file system-based translations as one of the sources.
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.
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.
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);
window.i18nextChainedBackend
{
// 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);
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
}
});
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:
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
})
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.
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!
With using locize you directly support the future of i18next and react-i18next.
4.6.2
FAQs
backend layer for i18next to chain backends
The npm package i18next-chained-backend receives a total of 130,372 weekly downloads. As such, i18next-chained-backend popularity was classified as popular.
We found that i18next-chained-backend demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.