Socket
Socket
Sign inDemoInstall

i18next-http-backend

Package Overview
Dependencies
Maintainers
2
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next-http-backend

i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.


Version published
Weekly downloads
1.1M
increased by5.82%
Maintainers
2
Weekly downloads
 
Created

What is i18next-http-backend?

The i18next-http-backend package is a backend layer for the i18next internationalization framework, allowing you to load translations from a remote server via HTTP(S). It is particularly useful for applications that need to fetch translations dynamically from a server, making it easier to manage and update translations without redeploying the application.

What are i18next-http-backend's main functionalities?

Loading Translations from a Remote Server

This feature allows you to load translation files from a remote server. The `loadPath` option specifies the URL pattern to fetch the translation files, where `{{lng}}` and `{{ns}}` are placeholders for the language and namespace, respectively.

const i18next = require('i18next');
const HttpBackend = require('i18next-http-backend');

i18next
  .use(HttpBackend)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json'
    }
  });

Customizing HTTP Requests

This feature allows you to customize the HTTP requests used to fetch the translation files. The `requestOptions` object can include any options that are valid for the Fetch API, such as `mode`, `credentials`, and `cache`.

const i18next = require('i18next');
const HttpBackend = require('i18next-http-backend');

i18next
  .use(HttpBackend)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json',
      requestOptions: {
        mode: 'cors',
        credentials: 'same-origin',
        cache: 'default'
      }
    }
  });

Handling Load Errors

This feature allows you to handle errors that occur during the loading of translation files. The `request` function can be customized to handle different types of errors and provide appropriate responses.

const i18next = require('i18next');
const HttpBackend = require('i18next-http-backend');

i18next
  .use(HttpBackend)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json',
      request: (options, url, payload, callback) => {
        fetch(url, options)
          .then(response => {
            if (!response.ok) {
              throw new Error('Network response was not ok');
            }
            return response.json();
          })
          .then(data => callback(null, { status: '200', data }))
          .catch(error => callback(error, { status: '500' }));
      }
    }
  });

Other packages similar to i18next-http-backend

Keywords

FAQs

Package last updated on 15 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc