githubEdit

Caching

Browser caching with local storage

With i18next you can configure a cache layer to be used in the browser. It will load and cache resources from localStoragearrow-up-right and can be used in combination with the chained backendarrow-up-right.

import i18next from "i18next";
import ChainedBackend from "i18next-chained-backend";
import HttpBackend from "i18next-http-backend";
import LocalStorageBackend from "i18next-localstorage-backend";

i18next
  .use(ChainedBackend)
  .init({
    fallbackLng: "en",
    // ... your i18next config
    backend: {
      backends: [
        LocalStorageBackend,
        HttpBackend
      ],
      backendOptions: [{
        expirationTime: 7 * 24 * 60 * 60 * 1000 // 7 days
      }, {
        loadPath: '/locales/{{lng}}/{{ns}}.json'
      }]
    }
  });

Server side caching with filesystem

With i18next you can configure a cache layer to be used on server side. It will load and cache resources from the filesystemarrow-up-right and can be used in combination with the chained backendarrow-up-right.

React-native caching with AsyncStorage

With i18next you can configure a cache layer to be used on react-native. It will load and cache resources from the AsyncStoragearrow-up-right and can be used in combination with the chained backendarrow-up-right.

Next.js caching with next-i18next

With next-i18nextarrow-up-right v16+, you can use client-side caching with i18next-chained-backendarrow-up-right and i18next-localstorage-backendarrow-up-right. Translations are bundled at build time (via resourceLoader for App Router or serverSideTranslations for Pages Router), and the client can fetch fresh translations from a backend with localStorage caching.

For a complete example using both App Router and Pages Router with Locizearrow-up-right, have a look at the next-i18next-locize examplearrow-up-right.

App Router (client-side caching with Locize)

Pages Router (client-side caching with Locize)

triangle-exclamation

Last updated