All Projects → dotcore64 → i18next-fetch-backend

dotcore64 / i18next-fetch-backend

Licence: MIT license
Fetch i18next translations with the fetch API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to i18next-fetch-backend

i18next-http-backend
i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
Stars: ✭ 270 (+592.31%)
Mutual labels:  i18next
react-i18next-phraseapp
Library support to use react-i18next with the Phrase In-Context Editor - DEPRECATED
Stars: ✭ 14 (-64.1%)
Mutual labels:  i18next
website
The Algorithms website providing GitHub's largest open-source algorithm library.
Stars: ✭ 616 (+1479.49%)
Mutual labels:  i18next
ad localize
ADLocalize is a simple way to manage your localization files. Supported wording sources : CSVs and Google Sheets. Localization file generation available for iOS, Android, JSON (i18next), YAML and Java properties
Stars: ✭ 22 (-43.59%)
Mutual labels:  i18next
nextjs-movie-browser
A NextJS implementation of the themoviedb.org website
Stars: ✭ 45 (+15.38%)
Mutual labels:  i18next
i18next-fs-backend
i18next-fs-backend is a backend layer for i18next using in Node.js and for Deno to load translations from the filesystem.
Stars: ✭ 67 (+71.79%)
Mutual labels:  i18next
Next I18next
The easiest way to translate your NextJs apps.
Stars: ✭ 2,818 (+7125.64%)
Mutual labels:  i18next
I18n Ally
🌍 All in one i18n extension for VS Code
Stars: ✭ 1,931 (+4851.28%)
Mutual labels:  i18next
React I18next
Internationalization for react done right. Using the i18next i18n ecosystem.
Stars: ✭ 6,942 (+17700%)
Mutual labels:  i18next
i18next-scanner-typescript
Typescript transform for i18next-scanner
Stars: ✭ 21 (-46.15%)
Mutual labels:  i18next
movies
Real world isomorphic application for movies search, based on Webpack 5 / Express / React 17 + Redux-Saga / Bootstrap 4.6 + CSS Modules / i18next / SSR
Stars: ✭ 20 (-48.72%)
Mutual labels:  i18next
web-starter-kit
An opinionated starter kit with styled-system, graphql-hooks, mobx and nextjs (PWA)
Stars: ✭ 17 (-56.41%)
Mutual labels:  i18next
react-native-boilerplate-starter-app
📱🚀A POWERFUL React Native starter kit to bootstrap the start of your mobile app development
Stars: ✭ 202 (+417.95%)
Mutual labels:  i18next
react-sendbird-messenger
ReactJS (React-router-dom v6 + Antdesign + Firebase + Sendbird + Sentry) codebase containing real world examples (CRUD, auth, advanced patterns, etc).
Stars: ✭ 39 (+0%)
Mutual labels:  i18next
marko-i18next
Components to use i18next in Marko templates.
Stars: ✭ 13 (-66.67%)
Mutual labels:  i18next
gatsby-i18n
Gatsby plugin that provides i18n support
Stars: ✭ 25 (-35.9%)
Mutual labels:  i18next
i18next-json-sync
Keep i18next JSON resource files in sync
Stars: ✭ 44 (+12.82%)
Mutual labels:  i18next

Introduction

Build Status npm package Coverage Status

This is a simple i18next backend to be used in the browser. It will load resources from a backend server using the fetch API.

Getting started

This backend is most useful when XMLHttpRequest is not available, such as with Service Worker contexts. It is also useful when support for older browsers is not a concern, and newer APIs are a priority. Source can be loaded via npm.

# npm package
$ npm install --save i18next-fetch-backend

Wiring up:

import i18next from 'i18next';
import Fetch from 'i18next-fetch-backend';

i18next
  .use(Fetch)
  .init(i18nextOptions);
  • As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.

Backend Options

{
  // path where resources get loaded from, or a function
  // returning a path:
  // function(lngs, namespaces) { return customPath; }
  // the returned path will interpolate lng, ns if provided like giving a static path
  loadPath: '/locales/{{lng}}/{{ns}}.json',

  // parse data after it has been fetched
  // in example use https://www.npmjs.com/package/json5
  // here it removes the letter a from the json (bad idea)
  parse: function(data) { return data.replace(/a/g, ''); },

  // path to post missing resources
  addPath: 'locales/add/{{lng}}/{{ns}}',

  // define how to stringify the data when adding missing resources
  stringify: JSON.stringify,

  // your backend server supports multiloading
  // /locales/resources.json?lng=de+en&ns=ns1+ns2
  allowMultiLoading: false, // set loadPath: '/locales/resources.json?lng={{lng}}&ns={{ns}}' to adapt to multiLoading

  multiSeparator: '+',

  // init option for fetch, for example
  requestOptions: {
    mode: 'cors',
    credentials: 'same-origin',
    cache: 'default',
  },

  // define a custom fetch function
  fetch: function (url, options, callback) {},
}

Options can be passed in:

preferred - by setting options.backend in i18next.init:

import i18next from 'i18next';
import Fetch from 'i18next-fetch-backend';

i18next
  .use(Fetch)
  .init({
    backend: options
  });

on construction:

  import Fetch from 'i18next-fetch-backend';
  const fetch = new Fetch(null, options);

via calling init:

  import Fetch from 'i18next-fetch-backend';
  const fetch = new Fetch();
  fetch.init(options);

Service Worker example

import i18next from 'i18next';
import Fetch from 'i18next-fetch-backend';

let t = null;

self.addEventListener('activate', (event) => {
  event.waitUntil(new Promise((resolve, reject) => {
    i18next
      .use(Fetch)
      .init({
        fallbackLng: ['ja', 'en', 'zh'],
        preload: ['ja', 'en', 'zh'],
        ns: 'translation',
        defaultNS: 'translation',
        keySeparator: false, // Allow usage of dots in keys
        nsSeparator: false,
        backend: {
          loadPath: '/locales/{{lng}}/{{ns}}.json',
        },
      }, (err, _t) => {
        if (err) {
          reject(err);
          return;
        }

        t = _t;
        resolve();
      });
  }));
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].