Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18next-xhr-backend

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18next-xhr-backend

backend layer for i18next using browsers xhr

  • 3.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is i18next-xhr-backend?

The i18next-xhr-backend package is a backend layer for the i18next internationalization framework, which allows you to load translations from a server using XMLHttpRequest (XHR). This is particularly useful for dynamic loading of translations in web applications.

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

Loading translations from a server

This feature allows you to load translation files from a server. The `loadPath` option specifies the path to the translation files, which can include placeholders for the language (`{{lng}}`) and namespace (`{{ns}}`).

const i18next = require('i18next');
const XHR = require('i18next-xhr-backend');

i18next
  .use(XHR)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json'
    }
  }, function(err, t) {
    if (err) return console.error(err);
    console.log('i18next is ready...');
  });

Customizing XHR settings

This feature allows you to customize the XHR settings, such as using the Fetch API instead of the default XMLHttpRequest. The `ajax` function can be overridden to provide custom behavior for loading translation files.

const i18next = require('i18next');
const XHR = require('i18next-xhr-backend');

i18next
  .use(XHR)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json',
      ajax: function (url, options, callback, data) {
        // Custom AJAX request
        fetch(url, options)
          .then(response => response.json())
          .then(data => callback(data, { status: '200' }))
          .catch(error => callback(null, { status: '500' }));
      }
    }
  });

Caching translations

This feature allows you to cache translations in localStorage to reduce the number of requests to the server. The `cache` option can be configured to enable caching and set a key prefix for the cached translations.

const i18next = require('i18next');
const XHR = require('i18next-xhr-backend');
const Cache = require('i18next-localstorage-cache');

i18next
  .use(XHR)
  .use(Cache)
  .init({
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json'
    },
    cache: {
      enabled: true,
      prefix: 'i18next_res_' // Key prefix for localStorage
    }
  });

Other packages similar to i18next-xhr-backend

Keywords

FAQs

Package last updated on 01 Nov 2019

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