There are a few options to load translations to your application instrumented by i18next. The most common approach to this adding a so called backend plugin to i18next. The range of backends is large from loading translations in the browser using xhr request to loading translations from databases or filesystem in nodejs.
You can add the translations on init
import i18next from 'i18next';
i18next
.init({
resources: {
en: {
namespace1: {
key: 'hello from namespace 1'
},
namespace2: {
key: 'hello from namespace 2'
}
},
de: {
namespace1: {
key: 'hallo von namespace 1'
},
namespace2: {
key: 'hallo von namespace 2'
}
}
}
});
You can add the translations after init
import i18next from 'i18next';
i18next.init({ resources: {} });
i18next.addResourceBundle('en', 'namespace1', {
key: 'hello from namespace 1'
});
There are more options to adding, removing translations...learn more about resource handling.
{% hint style="info" %} Please make sure to at least pass in an empty resources object on init. Else i18next will try to load translations and give you a warning that you are not using a backend. {% endhint %}
Each plugin comes with a set of on configuration settings like path to load resources from. Those settings are documented on the individual readme file of each repository.
Here is a sample using the i18next-http-backend to load resources from the server.
import i18next from 'i18next';
import Backend from 'i18next-http-backend';
i18next
.use(Backend)
.init({
backend: {
// for all available options read the backend's repository readme file
loadPath: '/locales/{{lng}}/{{ns}}.json'
}
});
{% hint style="danger" %}
Having a combination of defining resources + having a backend will not implicitly take one or the other source as fallback resources.
If you need some fallback behaviour you may use the i18next-chained-backend. A short example can be found here.
With i18next-chained-backend you can also create some caching behaviour.
{% endhint %}
{% hint style="info" %} What's a plugin? {% endhint %}
Load using a smart backend plugin serving directly from a CDN
Just use the i18next-locize-backend to load resources from the CDN.
import i18next from 'i18next';
import Backend from 'i18next-locize-backend';
i18next
.use(Backend)
.init({
backend: {
projectId: '[PROJECT_ID]',
apiKey: '[API_KEY]',
referenceLng: '[LNG]'
}
});
Here you can find a step by step guide with a React.js app, which will unleash the full power of i18next in combination with locize.
See how your developer experience with this localization workflow could look like.
There's also the possibility to have an even more focused developer experience, with the help of the auto-machinetranslation workflow and the use of the save missing keys functionality, new keys not only gets added to locize automatically, while developing the app, but are also automatically translated into the target languages using machine translation (like Google Translate).
{% embed url="https://youtu.be/osScyaGMVqo" %}
{% embed url="https://youtu.be/VfxBpSXarlU" %}