View as Markdown
On this page

Migration guide

What changes in each major version of i18next, and the steps to move to it.

v26.x.x to v27.0.0

This is a major breaking release. It changes defaults that most setups had to override, gives every instance its own logger and post-processors, refuses language codes that look like markup or paths, and removes options that were broken or had no effect. For most of the runtime changes, i18next 26.7 logs a warning in debug mode (debug: true), so running your app once on 26.7 with debug logging finds most of what concerns you. It does not warn for returnEmptyString, addResources(), createInstance(), keySeparator: false or the TypeScript changes; read those sections below.

Keep the v26 behavior

Where an option exists, one line brings the v26 behavior back:

javascript
// the default escape of v26, '/' included
const entities = {
  '&': '&',
  '<': '&lt;',
  '>': '&gt;',
  '"': '&quot;',
  "'": '&#39;',
  '/': '&#x2F;',
};

i18next.init({
  fallbackLng: 'dev', // better: your main language, e.g. 'en'
  returnEmptyString: true, // an empty translation is a translation again
  cleanCode: false, // keep codes without a region as they are ('EN' stays 'EN')
  // escape '/' again
  interpolation: {
    escape: (str) => str.replace(/[&<>"'/]/g, (c) => entities[c]),
  },
  // a sentence for object hits
  returnedObjectHandler: (key) =>
    `key '${key}' returned an object instead of string.`,
});
typescript
declare module 'i18next' {
  interface CustomTypeOptions {
    strictInterpolationChecks: false;
    returnEmptyString: true;
  }
}

The other changes have no switch; they are described below.

Changed defaults

  • fallbackLng defaults to false instead of ['dev']. dev was meant for the saveMissing workflow: missing keys went to a "developer language". Every app without a fallbackLng therefore requested dev from its backend (with i18next-http-backend /locales/dev/translation.json, which almost no project has). Now i18next loads only the requested language (and its language part, de for de-CH), and saveMissing sends missing keys for the current language. With supportedLngs and no fallbackLng, an unsupported language leaves language and languages unset, as an explicit fallbackLng: false already did in v26. Set fallbackLng to your main language; fallbackLng: 'dev' restores v26 exactly.
  • returnEmptyString defaults to false: an empty translation falls back like a missing value (to the fallback language, the defaultValue or the key), as null already did. This makes the default fit i18next-cli, whose extractor writes '' for keys that are not translated yet in the other languages: those keys now fall back instead of rendering blank. An explicit '' default from code still counts: t('key', '') and t('key', { defaultValue: '' }) return ''. If an empty translation is meant to render nothing in a language (an empty particle or suffix), set returnEmptyString: true, in init() and in CustomTypeOptions.
  • Language codes without a region get their canonical casing, as codes with a region already did ('en-us' became 'en-US'): 'EN' becomes 'en'. Only the casing changes: aliases like tl, iw or eng stay as they are ('TL' becomes 'tl'), and so do codes that are not BCP 47, like en_US or my_custom. i18next 26.7.0 and 26.7.1 warned that v27 turns iw into he or tl into fil; that was wrong and 26.7.2 corrects it. iw, tl and the like stay as they are. If your resources or translation files use upper case (EN), rename them to lower case, or set cleanCode: false to keep codes without a region exactly as they are. cleanCode is deprecated.

TypeScript

  • strictInterpolationChecks is true by default. t() needs the options for a string with {{variables}}, so a variable cannot be left out of a call by accident (at runtime the raw {{name}} reaches the UI). It only concerns projects with typed resources. The three patterns that need a change:
typescript
// 'greeting': 'Hello {{name}}'
t('greeting'); // error since v27
t('greeting', { name: user.name }); // ok

// a wrapper that passes a key on to t() without options
// error once a string has variables
const label = (key: ParseKeys<'common'>) => t(key);
// type its key with ParseKeysWithoutVariables instead: only keys without
// variables
const plainLabel = (key: ParseKeysWithoutVariables<'common'>) => t(key); // ok

// variables filled in by interpolation.defaultVariables
declare module 'i18next' {
  interface CustomTypeOptions {
    defaultVariables: { appName: string }; // appName becomes optional in t()
  }
}

A plural key needs count when its forms use it, also inside a union ((key: 'plain' | 'item') => t(key)), with getFixedT and when other options are passed (t('item', { defaultValue })). To keep the v26 types, set strictInterpolationChecks: false; with parseInterpolation: 'icu' or 'mf2' that also gives back the v26 types (their messages require their arguments either way).

  • returnEmptyString is false in the types too, so a key whose value is '' is typed as the key.
  • compatibilityJSON is removed from the options and CustomTypeOptions. It had no effect since v24; remove it.
  • keySeparator: false in CustomTypeOptions offers only the flat keys (it produced nestedfalsec-style keys and split keys at the text false), and keyPrefix takes the parts of the flat keys before a . ('nested' for 'nested.c'), since getFixedT() joins a key prefix with .. A key prefix combined with selectors is not typed for flat keys.
  • The internal helpers are no longer importable. Every declaration in typescript/t.d.ts and typescript/options.d.ts counted as exported, internal helpers included. The 86 helpers whose name starts with _ or $ are hidden now; every other type of those files (for example TOptionsBase, TFunctionSignature, ParseTReturn, DefaultTReturn, KeysBuilder, AppendKeyPrefix) is exported on purpose. If you imported one of the hidden helpers, please open an issue: exporting it again breaks nothing. The hidden names: $FirstNamespace, $IsResourcesDefined, $PluralBrand, $SelectorKeyBrand, $ValueIfResourcesDefined, _AllContextPrefixes, _ArgsFromEntries, _BuiltInFormatTypeMap, _ContextBases, _ContextPrefixes, _ContextPrefixesOf, _ContextSeparator, _CountIfPlural, _DefaultVariables, _DefinedContextKeys, _DefinedKeys, _DropConflictKeys, _EnableSelector, _FallbackNamespace, _FallbackParse, _HasContextVariant, _HasDefinedKey, _HasPlural, _IcuArg, _IcuArgsMap, _IcuBlock, _IcuCases, _IcuEntries, _IcuFormatType, _IcuSkipQuoted, _IcuStripQuotes, _IcuTrim, _IcuWhitespace, _IfOptionalDefaultOptions, _IfOptionalOptions, _InterpolationFormatSeparator, _InterpolationFormatTypeMap, _InterpolationMapFromEntries, _InterpolationPrefix, _InterpolationSuffix, _IsContextualKey, _IsLiteral, _KeyAndOptions, _KeySeparator, _KeysOf, _KeysWithoutVariables, _LegacyResources, _MergeNamespaces, _MergedResources, _Mf2ArgsMap, _Mf2Entries, _Mf2ExprEntries, _Mf2Expression, _Mf2FunctionType, _Mf2Locals, _Mf2Name, _Mf2Selectors, _Mf2Space, _Mf2Spaces, _Mf2StripEscapes, _Mf2Vars, _NonNeverKeys, _NsResourceStrict, _NsSeparator, _ParseInterpolation, _ParsesMessageFormat, _PerPropMerge, _PluralSeparator, _PrimaryParse, _RequiresMessageOptions, _ResolveEntryType, _Resources, _ReturnEmptyString, _ReturnNull, _ReturnObjects, _SelectorArrayOptionsArg, _SelectorOptionsArg, _SelectorsReturn, _StrictInterpolationChecks, _StrictKeyChecks, _StripFormatOptions, _UnescapePrefix, _UnescapeSuffix, _UnionToIntersection, _ValueOf, _WithDefaultVariables. _CompatibilityJSON was removed together with compatibilityJSON.
  • Plugin option types belong to the plugins. locizeLastUsed is declared by locize-lastused 5.1.0 and later. ReactOptions stays importable from 'i18next', but react-i18next fills it in (17.1.13 and later, and 18): the react options are checked when react-i18next is part of the program.

Logger and post-processors per instance

In v26 the logger and the post-processors were shared by all instances: the init() of one instance rewired the logging of all others, and a post-processor use()d on one instance worked in every instance. Now they belong to the instance:

  • An instance from createInstance() does not see what the default i18next instance, or any other instance, registered. Call use() on each instance.

Before (v26), this worked because the default instance registered sprintf for everybody:

javascript
import i18next from 'i18next';
import sprintf from 'i18next-sprintf-postprocessor';

i18next.use(sprintf);
const instance = i18next.createInstance();
await instance.init({ postProcess: 'sprintf' /* ... */ });

After (v27):

javascript
import i18next from 'i18next';
import sprintf from 'i18next-sprintf-postprocessor';

const instance = i18next.createInstance();
await instance.use(sprintf).init({ postProcess: 'sprintf' /* ... */ });
  • A clone (cloneInstance(), one per request with i18next-http-middleware) keeps the logger and the post-processors of its parent, also those its parent registers later. What a clone registers itself stays its own.
  • A plugin that calls use() from its own init() registers right away, as before.
  • i18next.services.logger is the instance's logger, with forward() as before.
  • In debug mode, a postProcess name that is not registered on the instance logs a warning.

Language codes from outside

A language code from lng, changeLanguage(), a language detector or loadLanguages() that does not have the shape of isSafeLanguageCode() is refused, unless it is configured. The shape: letters, digits, -, _, @ and . (no ..), at most 128 characters, not a prototype key. Configured means listed in supportedLngs, in a fallbackLng that is not a function or in preload, or a language in resources, as given to init(); a fallbackLng function and languages added later (loadLanguages(), addResourceBundle()) do not count. Without supportedLngs, a detector that returned "><img src=x onerror=alert(1)> made that string i18next.language, which apps put into <html lang>, and backends were asked to load it.

changeLanguage() continues as with an unsupported code: with the detector's next code, else the fallback language, else none (with fallbackLng: false, a code refused at init() leaves language unset). loadLanguages() skips the code. BCP 47 (en-US, zh-Hant-TW, en-x-pirate), POSIX (en_US.UTF-8@euro, sr@latin) and other codes with _ pass. To use a code with other characters, for example b+sr+Latn, list it in supportedLngs. In debug mode a refused code logs a warning. With supportedLngs nothing changes for you: the input is matched as before ('de-DE;q=0.9' still gives 'de'), and only an unsafe match is refused, which only nonExplicitSupportedLngs can return ('de-<x>' for a supported 'de').

Output changes

  • / is no longer escaped. The default escape turned $10/month into $10&#x2F;month and dates in react-i18next's Trans into 17&#x2F;12&#x2F;2025. &, <, >, " and ' are still escaped, which covers HTML text and quoted attributes; a / cannot end either. Update snapshot tests that contain &#x2F;. A custom interpolation.escape is not affected; one that escapes / too keeps the v26 output (see "Keep the v26 behavior" above).
  • An object or array without returnObjects is handled like a missing key: t() returns the default value if one is passed, else the key, with appendNamespaceToMissingKey, parseMissingKeyHandler and the post-processors applied as for a missing key. It used to return the sentence key 'obj (en)' returned an object instead of string., which ended up in the UI. The key is not saved as missing, since it exists. The debug warning stays; returnedObjectHandler still decides what is returned when it is set, so it can bring a sentence back.
  • addResources() keeps every value. It added only strings and arrays and silently dropped numbers, booleans and nested objects.
  • dir() without a language returns 'ltr' (since 26.7.0).

createInstance(options)

createInstance(options) merges the defaults right away, without initializing the instance. Before init(), instance.options has the defaults (options.interpolation, overloadTranslationOptionHandler, ...), getFixedT() no longer throws, and with debug: true the warnings before init() are shown; t() returns '' until init(). A keySeparator or nsSeparator passed to createInstance() now counts as set by you, as if passed to init(). loadNamespaces() before init() does nothing and calls back. You still call init() as before; with a callback, createInstance(options, callback) initializes right away, as in v26.

keySeparator: false

With keySeparator: false keys are flat: a lookup no longer splits a key at the text false (t('nestedfalsec') returned the value of nested.c), and a selector path is joined with . like a key prefix. Selectors work on the flat keys: $ => $['a.b'] and $ => $.a.b look up the flat key 'a.b', and $ => $.nested.c no longer resolves nested, then c.

Removed

Removed Use instead
getResource('en.translation.key'), addResource('en.translation.key', value), addResourceBundle('en.translation', resources) getResource('en', 'translation', 'key'), addResource('en', 'translation', 'key', value), addResourceBundle('en', 'translation', resources). The dotted form throws a TypeError in addResource(), addResources() and addResourceBundle(). A language code with a . (de_DE.UTF-8) is now a language like any other.
interpolation.prefixEscaped, suffixEscaped, nestingPrefixEscaped, nestingSuffixEscaped (inserted into the RegExp unescaped) the literal delimiters as prefix, suffix, nestingPrefix, nestingSuffix
compatibilityJSON (types) nothing, remove it
locizeLastUsed and the body of ReactOptions (types) locize-lastused 5.1.0, react-i18next 17.1.13 or 18
the root index.js (a CommonJS shim the exports map made unreachable) require('i18next')

For plugin authors

  • Declare the calling convention of a backend or an async language detector with callbackApi (since 26.7.0, see Plugins). Plugins without it still work in v27: i18next guesses from the number of parameters, which default and rest parameters change, and logs a deprecation warning in debug mode. The guessing is planned to go in a later major.
  • Reload through services.utils.reloadResources() instead of calling backendConnector.read() and loaded() yourself (since 26.7.0).
  • SSR integrations put server-rendered resources into the store with hydrate() (since 26.7.0). Writing isInitialized, initializedStoreOnce and initializedLanguageOnce from outside still works in v27, so react-i18next 17's useSSR keeps working, but it is deprecated.
  • Use isSafeLanguageCode() to check a language code from a request, a URL or storage with the same rule as core.
  • services.backendConnector.backend, services.languageUtils, services.resourceStore, services.interpolator and the cloneInstance() semantics are unchanged.

Support

  • Node.js 22 or later (engines).
  • The build targets ES2020 (Chrome and Edge 80, Firefox 80, Safari 14, Opera 67, Samsung Internet 13). It used browserslist defaults, which changed with every update of the browser data; the output is the same as 26.7's.
  • TypeScript ^5 || ^6 || ^7, unchanged.

Changes from 26.x patches, in one place

  • 26.4.7: a clone no longer inherits preload.
  • 26.5.5: with skipOnVariables (the default), a {{placeholder}} inside a value inserted with {{- var}} stays literal.
  • 26.5.10: the best match no longer picks a supported code that only starts with the same letters.
  • 26.5.11 and 26.5.12: a nested 0, false or '' wins over a flat dotted key; a nested null falls back to it.

Selector API

Both APIs are supported, in the types too: string keys stay first-class, and the selector API (enableSelector) stays opt-in. The old note that v26 would turn on selectors by default and v27 would deprecate string-key types is withdrawn. Selectors help with very large namespaces and go-to-definition; see Selector API.

Companion releases

  • react-i18next: on i18next 27, use react-i18next 17.1.13 or later, or 18; 17.x keeps working.
  • i18next-cli: update to the release published with i18next 27.
  • next-i18next: its peers already allow both majors.

Preparing for v27 (v26.7.0)

v26.7.0 is the last 26.x minor. With debug: true it logs a warning wherever a setup relies on something v27 changes, and most v27 defaults can be tried now with existing options:

javascript
i18next.init({
  debug: true, // prints the warnings
  // v27: fallbackLng defaults to false, set your source language (no implicit
  // 'dev')
  fallbackLng: 'en',
  // v27 default: an empty string falls back like a missing key
  returnEmptyString: false,
  // close to v27, which canonicalizes codes without a region ('EN' becomes 'en')
  cleanCode: true,
});
typescript
declare module 'i18next' {
  interface CustomTypeOptions {
    strictInterpolationChecks: true; // v27 default
    returnEmptyString: false; // v27 default
  }
}

With strictInterpolationChecks, type the key of a wrapper around t() with ParseKeysWithoutVariables and declare variables from interpolation.defaultVariables with the defaultVariables type option (see TypeScript). Plugin authors: declare callbackApi on backends and async detectors (see Plugins).

v25.x.x to v26.0.0

This is a major breaking release:

  • Removed legacy interpolation.format function: the old monolithic format function (interpolation: { format: (value, format, lng) => ... }) is no longer supported. The built-in Formatter is now always used. For custom formatting, use i18next.services.formatter.add() or .addCached(), or provide a custom Formatter module via .use(). See the formatting docs.
  • Removed deprecated initImmediate option: use initAsync instead (renamed in v24).
  • Removed console support notice: the showSupportNotice option and all related internal suppression logic (globalThis.__i18next_supportNoticeShown, I18NEXT_NO_SUPPORT_NOTICE env var) have been removed.
  • i18next.format now always uses the Formatter: i18next.format still works, but it is backed by the built-in Formatter (or a custom Formatter module). It no longer accepts a user-supplied function via interpolation.format.

Migration steps

  1. If you were using interpolation: { format: function(value, format, lng) { ... } }, migrate your custom logic to the Formatter API:
javascript
// before (v25 and earlier)
i18next.init({
  interpolation: {
    format: function(value, format, lng) {
      if (format === 'uppercase') return value.toUpperCase();
      return value;
    }
  }
});

// after (v26)
i18next.init({ /* ... */ });
i18next.services.formatter.add('uppercase', (value, lng, options) => {
  return value.toUpperCase();
});
  1. Replace any usage of initImmediate with initAsync.
  2. Remove any usage of showSupportNotice from your init options.

v25.3.x to v25.4.0

  • add new selector API to improve TypeScript IDE performance 2322
    • To enable it, set enableSelector: true in your configuration options
    • With enableSelector: "optimize", i18next can now handle translation dictionaries of any size, without affecting IDE performance or build times
    • To assist with the migration, we've published the following packages:

v24.x.x to v25.0.0

This is a potentially breaking release:

  • fix multiple changeLanguage calls that may have resulted in the wrong order in previous versions 1605 2298
  • adapt changeLanguage to always (string or array) use getBestMatchFromCodes 2299
  • getBestMatchFromCodes now tries to fall back to a language code with the same script 2299

v23.x.x to v24.0.0

  • remove support for older environments
  • remove old i18next JSON formats
  • remove support for compatibility to the very first v1 API (old docs)
  • Intl API is mandatory now and will not fall back anymore; use a polyfill (Intl.PluralRules and Intl.getCanonicalLocales) if your environment does not support it.
    • those who really need the old behavior need to create a compatibility layer similar to this.
  • renamed initImmediate to initAsync
  • fallback to dev language if plural rule not found
  • Dropped support for Node.js < v14

TypeScript

  • Now only typescript >5 versions are supported. v4 types are now removed from the codebase.
  • jsonFormat option has been removed. When a new JSON version is released, you can use the compatibilityJSON option, which now only accepts v4 as value.

v22.x.x to v23.0.0

Redesigned TypeScript types

This PR redesigned the types to be less complex, faster and easier to maintain.\

The redesign endeavors to enhance the approach to parsing and inferring keys for the t function. Instead of performing a recursive examination of each key-value pair in resources associated with specific namespace(s) each time the t function is invoked, we generate a comprehensive set of keys from all namespaces just once.

Make sure your tsconfig compilerOptions has the strict flag or the strictNullChecks set to true.

Also use TypeScript v5.

Codemods

To assist with the upgrade from i18next v22.x.x to v23.0.0, Codemod provides open-source codemods that automatically transform your code to many of the new APIs and patterns.

The following codemods are available (see the notes in the "More information" section below):

More information: Features and Breaking changes

Features

  • When loading multiple namespaces (react-i18next), t function will infer and accept the keys for the first namespace. So this pattern will be accepted now:

Screenshot 2023-02-12 at 9 40 06 PM

  • t function will now infer and accept the keys for the main namespace (i18next):

Screenshot 2023-02-12 at 9 48 31 PM

  • We're introducing a new type (returnObjects) that will infer fewer keys if set to false, and all keys and sub-keys if set to true. If the option returnObjects from t function is set to true, it'll work the same way:

Screenshot 2023-02-12 at 9 52 07 PM

Screenshot 2023-02-12 at 9 57 43 PM

Screenshot 2023-02-12 at 10 03 12 PM

  • t function will now infer interpolation values, but it'll only work if the translation files (resources) are placed in a ts file and using as const (like this) or an interface in a d.ts file, as i18next-cli types generates it; JSON files don't support as const to convert objects to be type literals (yet).

Breaking changes

All breaking changes described below are minor ones:

  1. Projects with the option returnObjects set as true by default will also have to set the same option in the CustomTypeOptions type. Otherwise, only complete keys will be allowed (key1.key2.key3...).
 // i18next.d.ts
 import 'i18next';
 declare module 'i18next' {
    interface CustomTypeOptions {
     returnObjects: true
      ...
  1. Renaming StringMap to $Dictionary, and we'll no longer export it. $Dictionary is an internal helper, and there is no reason to expose it. If needed, you can create a copy and reuse it in your project.
  2. ns property from InterpolationOptions type will be constrained to Namespace rather than string or readonly string[].

Codemod for this change:

bash
npx codemod i18next/23/add-namespace-type-annotation
  1. Renaming KeysWithSeparator type to JoinKeys, and it will no longer be exposed.

Codemod for this change:

bash
npx codemod i18next/23/replace-keys
  1. Renaming TFuncKey type to ParseKeys.
  2. Removing NormalizeByTypeOptions type.
  3. Renaming DefaultTFuncReturnWithObject type to DefaultTReturn. It will accept TOptions as generic constraint and will no longer be exposed.

Codemod for this change:

bash
npx codemod i18next/23/remove-options
  1. Removing DefaultTFuncReturn type in favor of DefaultTReturn.
  2. Removing NormalizeReturn type.

If you encounter any issues, please report them to the Codemod team with npx codemod feedback

Removed setDebug function in internal logger

Based on this discussion we decided to remove the setDebug function.

Changed default value for returnNull option to false

To improve the usage for TypeScript users (in combination with React.js) we decided to set the returnNull value to false by default.
More information can be found here.

Dropped support for old browsers and Node.js < v12

To have smaller builds and faster loads, we now transpile only for modern browsers and runtimes.
More information can be found here.

Prefixed ordinal plural keys

To help translators, ordinal plural keys are now prefixed with _ordinal.

Older versions

Migrations from versions before v22 are on Older migrations.