项目作者: dotcore64

项目描述 :
Fetch i18next translations with the fetch API
高级语言: JavaScript
项目地址: git://github.com/dotcore64/i18next-fetch-backend.git
创建时间: 2017-01-23T00:17:37Z
项目社区:https://github.com/dotcore64/i18next-fetch-backend

开源协议:MIT License

下载


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.

  1. # npm package
  2. $ npm install --save i18next-fetch-backend

Wiring up:

  1. import i18next from 'i18next';
  2. import Fetch from 'i18next-fetch-backend';
  3. i18next
  4. .use(Fetch)
  5. .init(i18nextOptions);
  • As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.

Backend Options

  1. {
  2. // path where resources get loaded from, or a function
  3. // returning a path:
  4. // function(lngs, namespaces) { return customPath; }
  5. // the returned path will interpolate lng, ns if provided like giving a static path
  6. loadPath: '/locales/{{lng}}/{{ns}}.json',
  7. // parse data after it has been fetched
  8. // in example use https://www.npmjs.com/package/json5
  9. // here it removes the letter a from the json (bad idea)
  10. parse: function(data) { return data.replace(/a/g, ''); },
  11. // path to post missing resources
  12. addPath: 'locales/add/{{lng}}/{{ns}}',
  13. // define how to stringify the data when adding missing resources
  14. stringify: JSON.stringify,
  15. // your backend server supports multiloading
  16. // /locales/resources.json?lng=de+en&ns=ns1+ns2
  17. allowMultiLoading: false, // set loadPath: '/locales/resources.json?lng={{lng}}&ns={{ns}}' to adapt to multiLoading
  18. multiSeparator: '+',
  19. // init option for fetch, for example
  20. requestOptions: {
  21. mode: 'cors',
  22. credentials: 'same-origin',
  23. cache: 'default',
  24. },
  25. // define a custom fetch function
  26. fetch: function (url, options, callback) {},
  27. }

Options can be passed in:

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

  1. import i18next from 'i18next';
  2. import Fetch from 'i18next-fetch-backend';
  3. i18next
  4. .use(Fetch)
  5. .init({
  6. backend: options
  7. });

on construction:

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

via calling init:

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

Service Worker example

  1. import i18next from 'i18next';
  2. import Fetch from 'i18next-fetch-backend';
  3. let t = null;
  4. self.addEventListener('activate', (event) => {
  5. event.waitUntil(new Promise((resolve, reject) => {
  6. i18next
  7. .use(Fetch)
  8. .init({
  9. fallbackLng: ['ja', 'en', 'zh'],
  10. preload: ['ja', 'en', 'zh'],
  11. ns: 'translation',
  12. defaultNS: 'translation',
  13. keySeparator: false, // Allow usage of dots in keys
  14. nsSeparator: false,
  15. backend: {
  16. loadPath: '/locales/{{lng}}/{{ns}}.json',
  17. },
  18. }, (err, _t) => {
  19. if (err) {
  20. reject(err);
  21. return;
  22. }
  23. t = _t;
  24. resolve();
  25. });
  26. }));
  27. });