Skip to content
On this page

i18next

How an app with i18next connects to Locize. How i18next itself works, its options and its plugins, is explained on i18next.com.

Set it up with your agent

Your coding agent sets up i18next and connects it to Locize: Getting started. The rest of this page is what the agent sets up, for when you do it yourself.

Files in your repository

Your app loads its translation files as usual, and Locize keeps them current. Either use locize-cli, or the Locize commands of i18next-cli, which also extracts the keys from your code:

bash
npx i18next-cli extract
npx i18next-cli locize-sync

Both send new keys of your source language and write the translations back into your files. In CI, the GitHub Actions do the same.

From the CDN

Your app loads its translations at runtime from the CDN, with i18next-locize-backend:

bash
npm install i18next-locize-backend
js
import i18next from 'i18next'
import Backend from 'i18next-locize-backend'

i18next
  .use(Backend)
  .init({
    fallbackLng: 'en',
    backend: {
      projectId: '<your-project-id>',
      version: 'production',
      cdnType: 'standard'
    }
  })
  • version is the version your app loads, latest by default. For production, use a version you publish on purpose: Best practices.
  • cdnType is standard by default; set pro if your project uses the Pro CDN.
  • A private version needs private: true and a key, so only a server can load it.
  • On a server, the backend loads the translations again every hour. In a serverless function, bundle the translations at build instead: Caching and fallback.

New keys while you develop

With saveMissing, i18next sends a key your code uses but Locize does not have yet, in your source language. It needs an API key with write access, so turn it on only while you develop:

js
const isDev = process.env.NODE_ENV === 'development'

i18next
  .use(Backend)
  .init({
    fallbackLng: 'en',
    saveMissing: isDev,
    backend: {
      projectId: '<your-project-id>',
      apiKey: isDev ? process.env.LOCIZE_API_KEY : undefined,
      referenceLng: 'en'
    }
  })
  • referenceLng is your project's source language, en by default.
  • In the browser, the backend sends new keys only from localhost, unless you change allowedAddOrUpdateHosts; on a server it sends them whenever saveMissing is on.
  • Never ship the key in a production build: anyone could read it there. More: Best practices.

With automatic translation on, the new keys come back translated.

Editing on your page

The InContext editor lets people change texts directly on your running app.

Several projects in one app

An app can load from several Locize projects, for example a shared one and its own. With i18next, add i18next-locize-backend once per project through i18next-chained-backend:

js
import ChainedBackend from 'i18next-chained-backend'
import LocizeBackend from 'i18next-locize-backend'

i18next
  .use(ChainedBackend)
  .init({
    fallbackLng: 'en',
    backend: {
      backends: [LocizeBackend, LocizeBackend],
      backendOptions: [
        { projectId: '<shared-project-id>', version: 'production' },
        { projectId: '<app-project-id>', version: 'production' }
      ]
    }
  })

The first project that has a namespace delivers it; keys of the same namespace are not merged, so give each project its own namespace names. Keep saveMissing off in this setup. For texts per customer, use tenants instead of several projects.

Example apps