Installations
npm install i18next-http-backend
Score
57.6
Supply Chain
99
Quality
93.5
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Developer
i18next
Module System
ESM
Statistics
453 Stars
271 Commits
70 Forks
7 Watching
1 Branches
29 Contributors
Updated on 22 Nov 2024
Bundle Size
19.60 kB
Minified
5.99 kB
Minified + Gzipped
Languages
JavaScript (70.13%)
HTML (14.67%)
TypeScript (9.36%)
Vue (4.31%)
CSS (1.45%)
Less (0.08%)
Total Downloads
Cumulative downloads
Total Downloads
114,784,616
Last day
16.9%
219,738
Compared to previous day
Last week
7.8%
1,098,762
Compared to previous week
Last month
0.1%
4,564,628
Compared to previous month
Last year
35.8%
47,004,798
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
24
Introduction
This is a simple i18next backend to be used in Node.js, in the browser and for Deno. It will load resources from a backend server using the XMLHttpRequest or the fetch API.
Get a first idea on how it is used in this i18next crash course video.
It's based on the deprecated i18next-xhr-backend and can mostly be used as a drop-in replacement.
Why i18next-xhr-backend was deprecated?
Advice:
If you don't like to manage your translation files manually or are simply looking for a better management solution, take a look at i18next-locize-backend. The i18next backed plugin for 🌐 locize ☁️.
To see i18next-locize-backend in a working app example, check out:
- this react-tutorial starting from Step 2
- this guide starting from the step of replacing i18next-http-backend with i18next-locize-backend
- this Angular blog post introducing i18next-locize-backend
- the code integration part in this YouTube video
Troubleshooting
Make sure you set the debug
option of i18next to true
. This will maybe log more information in the developer console.
Seeing failed http requests, like 404?
Are you using a language detector plugin that detects region specific languages you are not providing? i.e. you provide 'en'
translations but you see a 'en-US'
request first?
This is because of the default load
option set to 'all'
.
Try to set the load
option to 'languageOnly'
1i18next.init({ 2 load: 'languageOnly', 3 // other options 4})
Getting started
Source can be loaded via npm or downloaded from this repo.
There's also the possibility to directly import it via a CDN like jsdelivr or unpkg or similar.
1# npm package 2$ npm install i18next-http-backend
Wiring up:
1import i18next from 'i18next'; 2import HttpApi from 'i18next-http-backend'; 3 4i18next.use(HttpApi).init(i18nextOptions);
for Deno:
1import i18next from 'https://deno.land/x/i18next/index.js' 2import Backend from 'https://deno.land/x/i18next_http_backend/index.js' 3 4i18next.use(Backend).init(i18nextOptions);
for plain browser:
1<script src="https://cdn.jsdelivr.net/npm/i18next-http-backend@1.3.1/i18nextHttpBackend.min.js"></script> 2<!-- an example can be found in example/jquery/index.html -->
1i18next.use(i18nextHttpBackend).init(i18nextOptions);
- As with all modules you can either pass the constructor function (class) to the i18next.use or a concrete instance.
- If you don't use a module loader it will be added to
window.i18nextHttpBackend
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 // the function might return a promise 7 // returning falsy will abort the download 8 // 9 // If not used with i18next-multiload-backend-adapter, lngs and namespaces will have only one element each, 10 // If used with i18next-multiload-backend-adapter, lngs and namespaces can have multiple elements 11 // and also your server needs to support multiloading 12 // /locales/resources.json?lng=de+en&ns=ns1+ns2 13 // Adapter is needed to enable MultiLoading https://github.com/i18next/i18next-multiload-backend-adapter 14 // Returned JSON structure in this case is 15 // { 16 // lang : { 17 // namespaceA: {}, 18 // namespaceB: {}, 19 // ...etc 20 // } 21 // } 22 loadPath: '/locales/{{lng}}/{{ns}}.json', 23 24 // path to post missing resources, or a function 25 // function(lng, namespace) { return customPath; } 26 // the returned path will interpolate lng, ns if provided like giving a static path 27 // 28 // note that this only works when initialized with { saveMissing: true } 29 // (see https://www.i18next.com/overview/configuration-options) 30 addPath: '/locales/add/{{lng}}/{{ns}}', 31 32 // parse data after it has been fetched 33 // in example use https://www.npmjs.com/package/json5 or https://www.npmjs.com/package/jsonc-parser 34 // here it removes the letter a from the json (bad idea) 35 parse: function(data) { return data.replace(/a/g, ''); }, 36 37 // parse data before it has been sent by addPath 38 parsePayload: function(namespace, key, fallbackValue) { return { key: fallbackValue || "" } }, 39 40 // parse data before it has been sent by loadPath 41 // if value returned it will send a POST request 42 parseLoadPayload: function(languages, namespaces) { return undefined }, 43 44 // allow cross domain requests 45 crossDomain: false, 46 47 // allow credentials on cross domain requests 48 withCredentials: false, 49 50 // overrideMimeType sets request.overrideMimeType("application/json") 51 overrideMimeType: false, 52 53 // custom request headers sets request.setRequestHeader(key, value) 54 customHeaders: { 55 authorization: 'foo', 56 // ... 57 }, 58 // can also be a function, that returns the headers 59 customHeaders: () => ({ 60 authorization: 'foo', 61 // ... 62 }), 63 64 requestOptions: { // used for fetch, can also be a function (payload) => ({ method: 'GET' }) 65 mode: 'cors', 66 credentials: 'same-origin', 67 cache: 'default' 68 }, 69 70 // define a custom request function 71 // can be used to support XDomainRequest in IE 8 and 9 72 // 73 // 'options' will be this entire options object 74 // 'url' will be passed the value of 'loadPath' 75 // 'payload' will be a key:value object used when saving missing translations 76 // 'callback' is a function that takes two parameters, 'err' and 'res'. 77 // 'err' should be an error 78 // 'res' should be an object with a 'status' property and a 'data' property containing a stringified object instance beeing the key:value translation pairs for the 79 // requested language and namespace, or null in case of an error. 80 request: function (options, url, payload, callback) {}, 81 82 // adds parameters to resource URL. 'example.com' -> 'example.com?v=1.3.5' 83 queryStringParams: { v: '1.3.5' }, 84 85 reloadInterval: false // can be used to reload resources in a specific interval (milliseconds) (useful in server environments) 86}
Options can be passed in:
preferred - by setting options.backend in i18next.init:
1import i18next from 'i18next'; 2import HttpApi from 'i18next-http-backend'; 3 4i18next.use(HttpApi).init({ 5 backend: options, 6});
on construction:
1import HttpApi from 'i18next-http-backend'; 2const HttpApi = new HttpApi(null, options);
via calling init:
1import HttpApi from 'i18next-http-backend'; 2const HttpApi = new HttpApi(); 3HttpApi.init(null, options);
TypeScript
To properly type the backend options, you can import the HttpBackendOptions
interface and use it as a generic type parameter to the i18next's init
method, e.g.:
1import i18n from 'i18next' 2import HttpBackend, { HttpBackendOptions } from 'i18next-http-backend' 3 4i18n 5 .use(HttpBackend) 6 .init<HttpBackendOptions>({ 7 backend: { 8 // http backend options 9 }, 10 11 // other i18next options 12 })
Gold Sponsors
From the creators of i18next: localization as a service - locize.com
A translation management system built around the i18next ecosystem - locize.com.
With using locize you directly support the future of i18next.
No vulnerabilities found.
Reason
18 commit(s) and 9 issue activity found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: licence:0
- Info: FSF or OSI recognized license: MIT License: licence:0
Reason
Found 6/27 approved changesets -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/deno.yml:1
- Warn: no topLevel permission defined: .github/workflows/node.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/deno.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/i18next/i18next-http-backend/deno.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/deno.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/i18next/i18next-http-backend/deno.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/i18next/i18next-http-backend/node.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/i18next/i18next-http-backend/node.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/node.yml:24
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 9 are checked with a SAST tool
Score
4.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to i18next-http-backend
i18next-http-middleware
i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.
i18next-http-event
i18next-http-backend is a backend layer for i18next using in Node.js, in the browser and for Deno.
@skyra/i18next-backend
A fast and modern filesystem-based i18next backend for Node.js.
@skyra/http-framework-i18n
An internationalization layer powered by i18next and @skyra/i18next-backend for @skyra/http-framework