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:
npx i18next-cli extract
npx i18next-cli locize-syncBoth 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:
npm install i18next-locize-backendimport i18next from 'i18next'
import Backend from 'i18next-locize-backend'
i18next
.use(Backend)
.init({
fallbackLng: 'en',
backend: {
projectId: '<your-project-id>',
version: 'production',
cdnType: 'standard'
}
})versionis the version your app loads,latestby default. For production, use a version you publish on purpose: Best practices.cdnTypeisstandardby default; setproif your project uses the Pro CDN.- A private version needs
private: trueand 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:
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'
}
})referenceLngis your project's source language,enby default.- In the browser, the backend sends new keys only from
localhost, unless you changeallowedAddOrUpdateHosts; on a server it sends them wheneversaveMissingis 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:
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.