Getting started

Last updated 4 months ago

Getting started

Installation

i18next can be added to your project using npm or yarn:

# npm
$ npm install i18next --save
# yarn
$ yarn add i18next

The default export is UMD compatible (commonjs, requirejs, global).

In the /dist folder you find additional builds for commonjs, es6 modules. Optimized to load i18next in webpack, rollup, ... or node.js. The correct entry points are already configured in the package.json so there should be no extra setup to get the best build option.

Load from CDN

You can also directly add a script tag loading i18next from one of the CDNs providing it:

unpkg.com

cdnjs.com

Important Read

Before we dive into the first sample please note. i18next by default uses a key based notation to lookup translations, which comes with the benefit of additional structure of your translation files.

The downside of this is your keys can not be in natural language, do not act as a fallback and can not by default include the chars : and . as those are used by i18next.

If you prefer using natural language please read the fallback guide.

Basic sample

Please be aware these samples are just showing basic usage of the core functionality. For production usage please consider using one of our framework integrations to get better and simpler integrations (Setting innerHTML is just done to show how it works).

import i18next from 'i18next';
i18next.init({
lng: 'en',
debug: true,
resources: {
en: {
translation: {
"key": "hello world"
}
}
}
}, function(err, t) {
// initialized and ready to go!
document.getElementById('output').innerHTML = i18next.t('key');
});

As you might see this basic sample provides only one language directly added on init...lets extend this to have buttons to change language from english to german:

source code

This is a working sample showing translated text. To learn more have a look at the following extended sample.

Extended sample

The extended sample adds the language detector for browser and the xhr-backend to load translation files from this documentation's i18next-gitbook repo.

source code

You now have an idea about the basic setup. It's time to learn more about: