i18next documentation
🏠 i18next🌐 localization as a serviceπŸŽ“ i18next crash courseπŸ’Ύ GitHub Repository
  • Introduction
  • Overview
    • Getting started
    • Comparison to others
    • API
    • Configuration Options
    • Supported Frameworks
    • Plugins and Utils
    • For Enterprises
    • First setup help
    • TypeScript
  • Translation Function
    • Essentials
    • Interpolation
    • Formatting
    • Plurals
    • Nesting
    • Context
    • Objects and Arrays
  • Principles
    • Best Practices
    • Translation Resolution
    • Namespaces
    • Fallback
    • Plugins
  • How to
    • Add or Load Translations
    • Extracting translations
    • Caching
    • Backend Fallback
    • FAQ
  • Misc
    • JSON Format
    • Creating own Plugins
    • Migration Guide
    • The history of i18next
    • Testimonials
  • 🌐localization as a service
  • πŸŽ“i18next crash course
  • πŸ’ΎGitHub Repository
Powered by GitBook
On this page
  • Installation
  • Load in Deno
  • Load from CDN
  • Important Caveat
  • Basic sample
  • Extended sample
  1. Overview

Getting started

Last updated 1 year ago

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 may 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 in

i18next can be imported like this:

import i18next from 'https://deno.land/x/i18next/index.js'
// or import i18next from 'https://raw.githubusercontent.com/i18next/i18next/master/src/index.js'
// or import i18next from 'https://cdn.jsdelivr.net/gh/i18next/i18next/src/index.js'

In you can check out how this works.

Load from CDN

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

unpkg.com

esm or cjs:

cdnjs.com

jsdelivr.com

Important Caveat

The downside of this is that your keys must not be in natural language β€” the names of the keys are not used as fallback content and the key names must not include reserved characters : and . since those are used by i18next.

Basic sample

import i18next from 'i18next';

i18next.init({
  lng: 'en', // if you're using a language detector, do not define the lng option
  debug: true,
  resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  }
});
// initialized and ready to go!
// i18next is already initialized, because the translation resources where passed via init function
document.getElementById('output').innerHTML = i18next.t('key');

Or using callback init signature:

import i18next from 'i18next';

i18next.init({
  lng: 'en', // if you're using a language detector, do not define the lng option
  debug: true,
  resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  }
}, function(err, t) {
  // initialized and ready to go!
  document.getElementById('output').innerHTML = i18next.t('key');
});

Or using Promises:

i18next.init({
  lng: 'en', // if you're using a language detector, do not define the lng option
  debug: true,
  resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  }
}).then(function(t) {
  // initialized and ready to go!
  document.getElementById('output').innerHTML = i18next.t('key');
});

Or using async/await:

await i18next.init({
  lng: 'en', // if you're using a language detector, do not define the lng option
  debug: true,
  resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  }
});
// 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… let's extend this to have buttons to change language from English to German:

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

Extended sample

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

Make sure to use a fixed version in production like as passing no version will redirect to latest version which might contain breaking changes in future.

Before we dive into the first sample, please note the following: By default, i18next uses a key-based notation to look up translations, which comes with the benefit of for your translation files.

If you prefer using natural language in keys, please read the .

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

if you are , you may need to wait for i18next to have finished to initialize.

Do you quickly want to translate your resources to other languages? Try:

The extended sample adds the language detector for our browser and the http-backend to load translation files from this documentation's .

The translation functions like , and .

Find an , eg. react-i18next, jquery-i18next and others. Using those wrappers around i18next makes using i18next a lot simpler in your project. Most of such modules come with extended examples.

Find out more about the .

Add a to detect the preferred language of your user

Add a to load the translations from the server or filesystem

Connect i18next with a smart , like in

https://unpkg.com/i18next/dist/umd/i18next.js
https://unpkg.com/i18next/dist/umd/i18next.min.js
https://unpkg.com/i18next/dist/esm/i18next.js
https://unpkg.com/i18next/dist/cjs/i18next.js
https://unpkg.com/i18next@17.0.0/dist/umd/i18next.js
https://cdnjs.com/libraries/i18next
https://www.jsdelivr.com/package/npm/i18next
additional structure
fallback guide
framework integrations
lazy loading the translation resources
source code
https://translate.i18next.com
i18next-gitbook repo
source code
interpolation
formatting
plurals
extension for your project
configuration options
language detector
backend plugin
Translation Management System
this React.js example
Deno
this tutorial blog post