Backend Fallback

Do you want to define a fallback which uses local translations?

Browser fallback with local / bundled translations

With i18next you can configure a fallback backend to be used in the browser. It will try to load from your primary backend (in this case from your locize backend) and if the primary backend is not reachable or does not serve translations, your second backend (in this case local or bundled translations) will be used. This is all possible thanks to the chained backend.

import i18next from "i18next";
import ChainedBackend from "i18next-chained-backend";
import LocizeBackend from "i18next-locize-backend";
import resourcesToBackend from "i18next-resources-to-backend";
const bundledResources = {
en: {
translations: {
key: 'value'
}
}
};
i18next
.use(ChainedBackend)
.init({
fallbackLng: "en",
// ... your i18next config
backend: {
backends: [
LocizeBackend,
resourcesToBackend(bundledResources)
],
backendOptions: [{
projectId: "[PROJECTID]",
apiKey: "[APIKEY]",
version: "[VERSION]",
referenceLng: "en"
}]
}
});

You can also lazy load the in memory translations, i.e. when using webpack

import i18next from "i18next";
import ChainedBackend from "i18next-chained-backend";
import LocizeBackend from "i18next-locize-backend";
import resourcesToBackend from "i18next-resources-to-backend";
i18next
.use(ChainedBackend)
.init({
fallbackLng: "en",
// ... your i18next config
backend: {
backends: [
LocizeBackend,
resourcesToBackend((lng, ns, clb) => {
import(`./locales/${lng}/${ns}.json`)
.then((resources) => clb(null, resources))
.catch(clb)
})
],
backendOptions: [{
projectId: "[PROJECTID]",
apiKey: "[APIKEY]",
version: "[VERSION]",
referenceLng: "en"
}]
}
});

Server side fallback with filesystem

On server side you can also use the i18next-fs-backend for example instead of the in memory fallback.

import i18next from "i18next";
import ChainedBackend from "i18next-chained-backend";
import LocizeBackend from "i18next-locize-backend";
import FsBackend from "i18next-fs-backend";
i18next
.use(ChainedBackend)
.init({
fallbackLng: "en",
// ... your i18next config
backend: {
backends: [
LocizeBackend,
FsBackend
],
backendOptions: [{
projectId: "[PROJECTID]",
apiKey: "[APIKEY]",
version: "[VERSION]",
referenceLng: "en"
}, {
loadPath: './locales_cache/{{lng}}/{{ns}}.json'
}]
}
});

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