i18next-http-backend is a backend layer for the i18next internationalization framework that loads translation resources from HTTP endpoints. It provides a modern replacement for the deprecated i18next-xhr-backend, supporting Node.js, browser, and Deno environments with both XMLHttpRequest and fetch API implementations.
npm install i18next-http-backendimport HttpBackend from "i18next-http-backend";For CommonJS:
const HttpBackend = require("i18next-http-backend");The package supports dual module exports (ESM/CommonJS) with the following structure:
./esm/index.js./esm/index.d.mts./cjs/index.js./cjs/index.d.tscross-fetchNote: Utility functions are internal implementation details and not exposed through the main package exports. They should be accessed through the Backend class methods rather than directly imported.
import i18next from "i18next";
import HttpBackend from "i18next-http-backend";
// Basic setup
i18next
.use(HttpBackend)
.init({
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json',
addPath: '/locales/add/{{lng}}/{{ns}}'
},
lng: 'en',
fallbackLng: 'en',
ns: ['common'],
defaultNS: 'common'
});
// With custom options
i18next
.use(HttpBackend)
.init({
backend: {
loadPath: 'https://api.example.com/translations/{{lng}}/{{ns}}',
customHeaders: {
'Authorization': 'Bearer token'
},
requestOptions: {
mode: 'cors',
credentials: 'include'
}
}
});i18next-http-backend is built around several key components:
Core backend class that implements the i18next BackendModule interface for loading and saving translation resources via HTTP.
export default class HttpBackend implements BackendModule<HttpBackendOptions> {
static type: "backend";
type: "backend";
services: any;
options: HttpBackendOptions;
constructor(services?: any, options?: HttpBackendOptions, allOptions?: any);
init(services?: any, options?: HttpBackendOptions, allOptions?: any): void;
read(language: string, namespace: string, callback: ReadCallback): void;
readMulti?(languages: string[], namespaces: string[], callback: MultiReadCallback): void;
loadUrl(url: string, callback: ReadCallback, languages?: string | string[], namespaces?: string | string[]): void;
create?(languages: string[], namespace: string, key: string, fallbackValue: string, callback?: (dataArray: any[], resArray: any[]) => void): void;
reload(): void;
}
type ReadCallback = (error: any, data?: any) => void;
type MultiReadCallback = (error: any, data?: any) => void;Comprehensive configuration system for customizing HTTP requests, paths, parsing, and behavior.
interface HttpBackendOptions {
loadPath?: LoadPathOption;
addPath?: AddPathOption;
parse?(data: string, languages?: string | string[], namespaces?: string | string[]): { [key: string]: any };
parsePayload?(namespace: string, key: string, fallbackValue?: string): { [key: string]: any };
parseLoadPayload?(languages: string[], namespaces: string[]): { [key: string]: any } | undefined;
crossDomain?: boolean;
withCredentials?: boolean;
request?(options: HttpBackendOptions, url: string, payload: {} | string, callback: RequestCallback): void;
queryStringParams?: { [key: string]: string };
customHeaders?: { [key: string]: string } | (() => { [key: string]: string });
reloadInterval?: false | number;
requestOptions?: RequestInit | ((payload: {} | string) => RequestInit);
alternateFetch?: FetchFunction;
}
type LoadPathOption = string | ((lngs: string[], namespaces: string[]) => string) | ((lngs: string[], namespaces: string[]) => Promise<string>);
type AddPathOption = string | ((lng: string, namespace: string) => string);
type FetchFunction = (input: string, init: RequestInit) => Promise<Response> | void;Adaptive HTTP request system supporting both fetch API and XMLHttpRequest with automatic environment detection.
function request(options: HttpBackendOptions, url: string, payload?: any, callback?: RequestCallback): void;
type RequestCallback = (error: any | undefined | null, response: RequestResponse | undefined | null) => void;
interface RequestResponse {
status: number;
data: any;
}Helper functions for environment detection, promise handling, and object manipulation.
function defaults(obj: object, ...sources: object[]): object;
function hasXMLHttpRequest(): boolean;
function makePromise(maybePromise: any): Promise<any>;