Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Fallback

Fallback behavior in i18next: fallback languages, fallback namespaces, fallback keys and default values, and what a raw key in the UI means.

Doing graceful fallbacks is a core principle of i18next. This enables you to display the most accurate content possible, while not repeating content over and over.

Language fallback

Variant resolving - fallback from dialects or scripts

By default, if a variant (containing region, script, etc) is not found, i18next will look for the same key in the broader version of that language. With this in mind, a common strategy if you're supporting language variants is to write common text inside the pure language, specifying only what differs in the variants.

Example:

// fallback to one language
i18next.init({
  lng: "en-GB",
  resources: {
    "en-GB": {
      "translation": {
        "i18n": "Internationalisation"
      }
    },
    "en": {
      "translation": {
        "i18n": "Internationalization",
        "i18n_short": "i18n"
      }
    }
  }
}, () => {
  i18next.t('i18n'); // -> finds "Internationalisation"
  i18next.t('i18n_short'); // -> falls back to "en": "i18n"

  // force using en
  i18next.t('i18n', { lng: 'en' }); // -> "Internationalization"
});

Fallback to different languages

If you can not provide the preferred language for a user, you can specify another language as fallback. This is useful to indicate the main language or, for instance, if you want to keep the fallbacks different per region.

Since v27 there is no fallback language by default (fallbackLng: false): set it to the language your app is written in. Before v27 the default was dev, a developer language: with saveMissing, new keys were sent to it, and translators turned them into, for instance, proper English. fallbackLng: 'dev' still does that.

Namespace fallback

i18next by default loads its translations from one file named translation. However, you can configure it to load from multiple files, called namespaces.

Besides defining multiple namespaces to load from, you also can set fallback namespaces. Thus, if a key to translate isn't found in the given namespace, it will look it up in the indicated fallbacks.

app.json

common.json

Sample

Key fallback

Key not found

If a key does not return a value the key acts as fallback:

If a key does not return a value the selected path acts as fallback:

Seeing raw keys in your UI usually means a translation is missing, not that the fallback is misconfigured. With saveMissing enabled and a backend that implements the create function, those keys are collected while you develop instead of being spotted in production. Using i18next-locize-backend, they appear in your Locize project ready to translate, no manual JSON edits. Built by the i18next team.

So you could configure i18next to have the key being the fallback instead of loading a fallback language:

de.json

de.json

While this works and might reduce files to load it makes the management of translations a lot harder as you will need to update changes to fallback values in code and JSON files.

Possible - but not recommended.

Missing values for existing keys

In addition to the above, if you want missing values to fallback to the key in cases where the keys (e.g. got extracted by a code parser) exist in your JSON translation file with empty string as value, you need this setting (the default since v27):

Calling with fallback keys

Calling the t function with an array of keys enables you to translate dynamic keys providing a non specific fallback value.

As a sample think of an error code you get and you like to show a specific warning in some cases:

translation.json

Sample

Last updated