File size: 4,148 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# Introduction
[](https://github.com/i18next/i18next-resources-to-backend/actions?query=workflow%3Anode)
[](https://github.com/i18next/i18next-resources-to-backend/actions?query=workflow%3Adeno)
[](https://www.npmjs.com/package/i18next-resources-to-backend)
This package helps to transform resources to an i18next backend. To be used in Node.js, in the browser and for Deno.
# Getting started
Source can be loaded via [npm](https://www.npmjs.com/package/i18next-resources-to-backend).
```bash
# npm package
$ npm install i18next-resources-to-backend
```
Wiring up:
```js
import i18next from 'i18next'
import ChainedBackend from 'i18next-chained-backend'
import resourcesToBackend from 'i18next-resources-to-backend'
import HttpBackend from 'i18next-http-backend'
i18next.use(ChainedBackend).init({
backend: {
backends: [
HttpBackend, // if a namespace can't be loaded via normal http-backend loadPath, then the inMemoryLocalBackend will try to return the correct resources
resourcesToBackend({
en: {
translations: {
sayHi: 'hello world'
}
}
})
],
backendOptions: [{
loadPath: 'http://localhost:8080/locales/{{lng}}/{{ns}}.json'
}]
}
})
```
for Deno:
```js
import i18next from 'https://deno.land/x/i18next/index.js'
import ChainedBackend from 'https://deno.land/x/i18next_chained_backend/index.js'
import resourcesToBackend from 'https://deno.land/x/i18next_resources_to_backend/index.js'
import HttpBackend from 'https://deno.land/x/i18next_http_backend/index.js'
i18next.use(ChainedBackend).init({
backend: {
backends: [
HttpBackend, // if a namespace can't be loaded via normal http-backend loadPath, then the inMemoryLocalBackend will try to return the correct resources
resourcesToBackend({
en: {
translations: {
sayHi: 'hello world'
}
}
})
],
backendOptions: [{
loadPath: 'http://localhost:8080/locales/{{lng}}/{{ns}}.json'
}]
}
})
```
## you can also lazy load the in memory translations, i.e. when using webpack:
```js
i18next.use(ChainedBackend).init({
backend: {
backends: [
HttpBackend, // if a namespace can't be loaded via normal http-backend loadPath, then the inMemoryLocalBackend will try to return the correct resources
// with dynamic import, you have to use the "default" key of the module ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#importing_defaults )
resourcesToBackend((language, namespace) => import(`./locales/${language}/${namespace}.json`))
// resourcesToBackend((language, namespace, callback) => {
// import(`./locales/${language}/${namespace}.json`)
// .then(({ default: resources }) => {
// callback(null, resources)
// })
// .catch((error) => {
// callback(error, null)
// })
// })
],
backendOptions: [{
loadPath: 'http://localhost:8080/locales/{{lng}}/{{ns}}.json'
}]
}
})
```
---
<h3 align="center">Gold Sponsors</h3>
<p align="center">
<a href="https://locize.com/" target="_blank">
<img src="https://raw.githubusercontent.com/i18next/i18next/master/assets/locize_sponsor_240.gif" width="240px">
</a>
</p>
---
**From the creators of i18next: localization as a service - locize.com**
A translation management system built around the i18next ecosystem - [locize.com](https://locize.com).

With using [locize](http://locize.com/?utm_source=react_i18next_readme&utm_medium=github) you directly support the future of i18next.
---
|