next-i18next-serverless
The easiest way to translate your NextJs apps.
Last updated 17 days ago by gurkerl83 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install next-i18next-serverless 
SYNC missed versions from official npm registry.

next-i18next

npm version CircleCI dependencies Status Package Quality

The easiest way to translate your NextJs apps.

What is this?

next-i18next is a plugin for Next.js projects that allows you to get translations up and running quickly and easily, while fully supporting SSR, multiple namespaces with codesplitting, etc.

While next-i18next uses i18next and react-i18next under the hood, users of next-i18next simply need to include their translation content as JSON files and don't have to worry about much else.

A live demo is available here. Please be aware this is hosted on a free Heroku dyno and therefore may go to sleep during periods of inactivity. This demo app is the simple example - nothing more, nothing less.

Setup

1. Installation

yarn add next-i18next

You need to also have react and next installed.

2. Translation content

By default, next-i18next expects your translations to be organised as such:

.
└── static
    └── locales
        ├── en
        |   └── common.json
        └── de
            └── common.json

This structure can also be seen in the simple example.

If you want to structure your translations/namespaces in a custom way, you will need to pass modified localePath and localeStructure values into the initialisation config.

3. Project setup

The default export of next-i18next is a class constructor, into which you pass your config options. The resulting class has all the methods you will need to translate your app:

import NextI18Next from 'next-i18next'

const NextI18NextInstance = new NextI18Next({
  defaultLanguage: 'en',
  otherLanguages: ['de']
})

export default NextI18NextInstance

/* Optionally, export class methods as named exports */
export const {
  appWithTranslation,
  withTranslation,
} = NextI18NextInstance

A full list of options can be seen here.

It's recommended to export this NextI18Next instance from a single file in your project, where you can continually import it from to use the class methods as needed. You can see this approach in the examples/simple/i18n.js file.

After creating and exporting your NextI18Next instance, you need to take the following steps to get things working:

  1. Create an _app.js file inside your pages directory, and wrap it with the NextI18Next.appWithTranslation higher order component (HOC). You can see this approach in the examples/simple/pages/_app.js.
  2. Create a server.js file inside your root directory, initialise an express server, and use the nextI18NextMiddleware middleware with your nextI18Next instance passed in. You can see this approach in the examples/simple/server.js. For more info, see the NextJs section on custom servers.

That's it! Your app is ready to go. You can now use the NextI18Next.withTranslation HOC to make your components or pages translatable, based on namespaces:

import React from 'react'

// This is our initialised `NextI18Next` instance
import { withTranslation } from '../i18n'

class Footer extends React.Component {
  render() {
    return (
      <footer>{this.props.t('description')}</footer>
    )
  }
}

export default withTranslation('footer')(Footer)

4. Declaring namespace dependencies

The withTranslation HOC is responsible for passing the t function to your component. It enables all the translation functionality provided by i18next. Further, it asserts your component gets re-rendered on language change or changes to the translation catalog itself (loaded translations). More info can be found here.

By default, next-i18next will send all your namespaces down to the client on each initial request. This can be an appropriate approach for smaller apps with less content, but a lot of apps will benefit from splitting namespaces based on route.

To do that, you need to return a namespacesRequired array via getInitialProps on your page-level component. You can see this approach in examples/simple/pages/index.js.

Note: withTranslation provides namespaces to the component that it wraps. However, namespacesRequired provides the total available namespaces to the entire React tree and belongs on the page level. Both are required (although you can use Trans instead of withTranslation if desired).

5. Locale subpaths

One of the main features of this package, besides translation itself, are locale subpaths. It's easiest to explain by example:

myapp.com         ---> Homepage in default lang
myapp.com/de     ---> Homepage in German

This functionality is not enabled by default, and must be passed as an option into the NextI18Next constructor as a config option:

new NextI18Next({
  localeSubpaths: {
    de: 'de'
  }
})

The localeSubpaths option is a key/value mapping, where keys are the locale itself (case sensitive) and values are the subpath without slashes.

Now, all your page routes will be duplicated across all your locale subpaths. Here's an example:

----- Config -----
new NextI18Next({
  localeSubpaths: {
    fr: 'fr',
    de: 'german',
    en: 'eng',
  }
})

----- Output -----
myapp.com/fr
myapp.com/german
myapp.com/eng

When using the localeSubpaths option, our middleware may redirect without calling any subsequent middleware. Therefore, if there are any critical middleware that must run before this redirect, ensure that you place it before the nextI18NextMiddleware middleware.

The main "gotcha" with locale subpaths is routing. We want to be able to route to "naked" routes, and not have to worry about the locale subpath part of the route:

<Link href='/some-page'>

With this link, we would expect someone whose language is set to French to automatically be directed to /fr/some-page.

To do that, we must import Link from your NextI18Next instance, not next/router:

import React from 'react'

// This is our initialised `NextI18Next` instance
import { Link } from '../i18n'

const SomeLink = () => (
  <Link href='/some-page'>
    This will magically prepend locale subpaths
  </Link>
)

We can also navigate imperatively with locale subpaths by importing Router from your NextI18Next instance. The exported Router shares the same API as the native Next Router. The push, replace, and prefetch functions will automatically prepend locale subpaths.

import React from 'react'

// This is our initialised `NextI18Next` instance
import { Router } from '../i18n'

const SomeButton = () => (
  <button
    onClick={() => Router.push('/some-page')}
  >
    This will magically prepend locale subpaths
  </button>
)

Custom Routing

Custom routing can be achieved via the app.render method:

/* First, use middleware */
server.use(nextI18NextMiddleware(nextI18next))

/* Second, declare custom routes */
server.get('/products/:id', (req, res) => {
  const { query, params } = req

  return app.render(req, res, '/product-page', { ...query, id: params.id })
})

/* Third, add catch-all GET for non-custom routes */
server.get('*', (req, res) => handle(req, res))

Accessing the Current Language

In many cases, you'll need to know the currently active language. Most of the time, to accomplish this, you should use the withTranslation HOC, which will pass an i18n prop to the wrapped component and further asserts your component will get re-rendered on language change or changes to the translation catalog itself (loaded translations). More info can be found here.

If for some reason you need to access the current language inside getInitialProps, you'll need to switch over server and client contexts. For example:

// This is our initialised `NextI18Next` instance
import { i18n } from '../i18n'

MyPage.getInitialProps = async({ req }) => {
  const currentLanguage = req ? req.language : i18n.language
}

Options

Key Default value
browserLanguageDetection true
defaultNS 'common'
defaultLanguage 'en'
ignoreRoutes ['/_next/', '/static/']
otherLanguages (required) []
localeExtension 'json'
localePath 'static/locales'
localeStructure '{{lng}}/{{ns}}'
localeSubpaths {}
serverLanguageDetection true
strictMode true
use (for plugins) []
customDetectors []

This table contains options which are specific to next-i18next. All other i18next options can be passed in as well.

Notes

Contributors

Thanks goes to these wonderful people (emoji key):

Rob Capellini
Rob Capellini

???? ⚠️
Alexander Kachkaev
Alexander Kachkaev

???? ???? ???? ???? ⚠️
Mathias Wøbbe
Mathias Wøbbe

???? ???? ⚠️
Lucas Feliciano
Lucas Feliciano

???? ????
Ryan Leung
Ryan Leung

????

This project follows the all-contributors specification. Contributions of any kind welcome!

Supported by BrowserStack

Thanks to BrowserStack for their support of this open-source project.

Current Tags

  • 1.1.179                                ...           canary (a month ago)
  • 1.1.180                                ...           latest (17 days ago)

160 Versions

  • 1.1.180                                ...           17 days ago
  • 1.1.179                                ...           a month ago
  • 1.1.178                                ...           2 months ago
  • 1.1.177                                ...           2 months ago
  • 1.1.176                                ...           2 months ago
  • 1.1.175                                ...           2 months ago
  • 1.1.174                                ...           2 months ago
  • 1.1.173                                ...           2 months ago
  • 1.1.172                                ...           2 months ago
  • 1.1.171                                ...           2 months ago
  • 1.1.170                                ...           2 months ago
  • 1.1.169                                ...           2 months ago
  • 1.1.168                                ...           2 months ago
  • 1.1.167                                ...           2 months ago
  • 1.1.165                                ...           2 months ago
  • 1.1.164                                ...           2 months ago
  • 1.1.163                                ...           2 months ago
  • 1.1.162                                ...           2 months ago
  • 1.1.161                                ...           2 months ago
  • 1.1.160                                ...           2 months ago
  • 1.1.159                                ...           2 months ago
  • 1.1.158                                ...           2 months ago
  • 1.1.157                                ...           2 months ago
  • 1.1.156                                ...           2 months ago
  • 1.1.155                                ...           2 months ago
  • 1.1.154                                ...           2 months ago
  • 1.1.153                                ...           2 months ago
  • 1.1.152                                ...           2 months ago
  • 1.1.151                                ...           2 months ago
  • 1.1.150                                ...           2 months ago
  • 1.1.149                                ...           2 months ago
  • 1.1.148                                ...           2 months ago
  • 1.1.147                                ...           2 months ago
  • 1.1.146                                ...           2 months ago
  • 1.1.145                                ...           2 months ago
  • 1.1.144                                ...           2 months ago
  • 1.1.143                                ...           2 months ago
  • 1.1.141                                ...           2 months ago
  • 1.1.140                                ...           2 months ago
  • 1.1.139                                ...           2 months ago
  • 1.1.138                                ...           2 months ago
  • 1.1.137                                ...           2 months ago
  • 1.1.136                                ...           2 months ago
  • 1.1.135                                ...           2 months ago
  • 1.1.134                                ...           2 months ago
  • 1.1.133                                ...           2 months ago
  • 1.1.132                                ...           2 months ago
  • 1.1.131                                ...           2 months ago
  • 1.1.130                                ...           2 months ago
  • 1.1.129                                ...           2 months ago
  • 1.1.128                                ...           2 months ago
  • 1.1.127                                ...           2 months ago
  • 1.1.126                                ...           2 months ago
  • 1.1.125                                ...           2 months ago
  • 1.1.124                                ...           2 months ago
  • 1.1.123                                ...           2 months ago
  • 1.1.122                                ...           2 months ago
  • 1.1.121                                ...           2 months ago
  • 1.1.120                                ...           2 months ago
  • 1.1.119                                ...           2 months ago
  • 1.1.118                                ...           2 months ago
  • 1.1.117                                ...           2 months ago
  • 1.1.116                                ...           2 months ago
  • 1.1.115                                ...           2 months ago
  • 1.1.114                                ...           2 months ago
  • 1.1.113                                ...           2 months ago
  • 1.1.112                                ...           2 months ago
  • 1.1.111                                ...           2 months ago
  • 1.1.109                                ...           2 months ago
  • 1.1.108                                ...           2 months ago
  • 1.1.107                                ...           2 months ago
  • 1.1.106                                ...           2 months ago
  • 1.1.105                                ...           2 months ago
  • 1.1.104                                ...           2 months ago
  • 1.1.103                                ...           2 months ago
  • 1.1.102                                ...           2 months ago
  • 1.1.101                                ...           2 months ago
  • 1.1.100                                ...           3 months ago
  • 3.0.15                                ...           3 months ago
  • 3.0.14                                ...           3 months ago
  • 3.0.13                                ...           3 months ago
  • 3.0.12                                ...           3 months ago
  • 3.0.11                                ...           3 months ago
  • 3.0.10                                ...           3 months ago
  • 3.0.9                                ...           3 months ago
  • 3.0.8                                ...           3 months ago
  • 3.0.7                                ...           3 months ago
  • 3.0.6                                ...           3 months ago
  • 3.0.4                                ...           3 months ago
  • 3.0.3                                ...           3 months ago
  • 3.0.2                                ...           3 months ago
  • 3.0.1                                ...           3 months ago
  • 3.0.0                                ...           3 months ago
  • 1.1.72                                ...           4 months ago
  • 1.1.71                                ...           4 months ago
  • 1.1.69                                ...           4 months ago
  • 1.1.68                                ...           4 months ago
  • 1.1.67                                ...           4 months ago
  • 1.1.65                                ...           4 months ago
  • 1.1.64                                ...           4 months ago
  • 1.1.63                                ...           4 months ago
  • 1.1.62                                ...           4 months ago
  • 1.1.61                                ...           4 months ago
  • 1.1.60                                ...           4 months ago
  • 1.1.59                                ...           4 months ago
  • 1.1.58                                ...           4 months ago
  • 1.1.57                                ...           4 months ago
  • 1.1.56                                ...           4 months ago
  • 1.1.55                                ...           4 months ago
  • 1.1.53                                ...           4 months ago
  • 1.1.52                                ...           4 months ago
  • 1.1.51                                ...           4 months ago
  • 1.1.50                                ...           4 months ago
  • 1.1.49                                ...           4 months ago
  • 1.1.48                                ...           4 months ago
  • 1.1.47                                ...           4 months ago
  • 1.1.46                                ...           4 months ago
  • 1.1.45                                ...           4 months ago
  • 1.1.44                                ...           4 months ago
  • 1.1.43                                ...           6 months ago
  • 1.1.42                                ...           6 months ago
  • 1.1.41                                ...           6 months ago
  • 1.1.40                                ...           6 months ago
  • 1.1.39                                ...           6 months ago
  • 1.1.38                                ...           6 months ago
  • 1.1.37                                ...           6 months ago
  • 1.1.36                                ...           6 months ago
  • 1.1.35                                ...           6 months ago
  • 1.1.34                                ...           6 months ago
  • 1.1.33                                ...           7 months ago
  • 1.1.32                                ...           7 months ago
  • 1.1.31                                ...           7 months ago
  • 1.1.30                                ...           7 months ago
  • 1.1.29                                ...           7 months ago
  • 1.1.28                                ...           7 months ago
  • 1.1.27                                ...           7 months ago
  • 1.1.26                                ...           7 months ago
  • 1.1.25                                ...           7 months ago
  • 1.1.24                                ...           7 months ago
  • 1.1.22                                ...           7 months ago
  • 1.1.21                                ...           7 months ago
  • 1.1.20                                ...           7 months ago
  • 1.1.19                                ...           7 months ago
  • 1.1.18                                ...           7 months ago
  • 1.1.17                                ...           7 months ago
  • 1.1.16                                ...           7 months ago
  • 1.1.15                                ...           7 months ago
  • 1.1.14                                ...           7 months ago
  • 1.1.13                                ...           7 months ago
  • 1.1.12                                ...           7 months ago
  • 1.1.11                                ...           7 months ago
  • 1.1.10                                ...           7 months ago
  • 1.1.9                                ...           7 months ago
  • 1.1.8                                ...           7 months ago
  • 1.1.7                                ...           7 months ago
  • 1.1.6                                ...           7 months ago
  • 1.1.5                                ...           7 months ago
  • 1.1.4                                ...           7 months ago
  • 1.1.3                                ...           7 months ago
  • 1.1.2                                ...           7 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 331
Dependencies (16)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |