UNPKG

13.2 kBMarkdownView Raw
1# next-i18next
2[![npm version](https://badge.fury.io/js/next-i18next.svg)](https://badge.fury.io/js/next-i18next)
3[![CircleCI](https://circleci.com/gh/isaachinman/next-i18next.svg?style=shield)](https://circleci.com/gh/isaachinman/next-i18next)
4[![Package Quality](https://npm.packagequality.com/shield/next-i18next.svg)](https://packagequality.com/#?package=next-i18next)
5
6**The easiest way to translate your NextJs apps.**
7
8If you are using next-i18next in production, please consider [sponsoring the package](https://github.com/sponsors/isaachinman) with any amount you think appropriate.
9
10## What is this?
11
12`next-i18next` is a plugin for [Next.js](https://nextjs.org/) projects that allows you to get translations up and running quickly and easily, while fully supporting SSR, multiple [namespaces](https://www.i18next.com/principles/namespaces) with codesplitting, etc.
13
14While `next-i18next` uses [i18next](https://www.i18next.com/) and [react-i18next](https://github.com/i18next/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.
15
16A live demo is [available here](http://next-i18next.com/). This demo app is the [simple example](./examples/simple/) - nothing more, nothing less.
17
18## Setup
19
20### 1. Installation
21
22```jsx
23yarn add next-i18next
24```
25
26You need to also have `react` and `next` installed.
27
28### 2. Translation content
29
30By default, `next-i18next` expects your translations to be organised as such:
31```
32.
33└── public
34 └── static
35 └── locales
36 ├── en
37 | └── common.json
38 └── de
39 └── common.json
40```
41
42This structure can also be seen in the [simple example](./examples/simple).
43
44If 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.
45
46### 3. Project setup
47
48The 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:
49
50```jsx
51const NextI18Next = require('next-i18next').default
52const { localeSubpaths } = require('next/config').default().publicRuntimeConfig
53const path = require('path')
54
55module.exports = new NextI18Next({
56 otherLanguages: ['de'],
57 localeSubpaths,
58 localePath: path.resolve('./public/static/locales')
59})
60```
61
62Note that `localePath` is required, and must be an absolute path.
63
64[A full list of options can be seen here](#options).
65
66It'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](./examples/simple/i18n.js) file.
67
68After creating and exporting your `NextI18Next` instance, you need to take the following steps to get things working:
69
701. 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](./examples/simple/pages/_app.js).
71Your app component must either extend `App` if it's a class component or define a `getInitialProps` if it's a functional component [(explanation here)](https://github.com/isaachinman/next-i18next/issues/615#issuecomment-575578375).
722. Create a `next.config.js` file inside your root directory if you want to use locale subpaths. You can see this approach in the [examples/simple/next.config.js](./examples/simple/next.config.js).
73
74Note: You can pass `shallowRender: true` into config options to avoid triggering getInitialProps when `changeLanguage` method is invoked.
75
76That'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:
77
78```jsx
79// This is our initialised `NextI18Next` instance
80import { withTranslation } from '../i18n'
81
82const Footer = ({ t }) => (
83 <footer>
84 <p>
85 {t('description')}
86 </p>
87 </footer>
88)
89
90export default withTranslation('footer')(Footer)
91```
92
93### 4. Declaring namespace dependencies
94
95The `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](https://react.i18next.com/latest/withtranslation-hoc).
96
97By 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.
98
99To 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](./examples/simple/pages/index.js).
100
101Note: `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).
102
103### 5. Locale subpaths
104
105One of the main features of this package, besides translation itself, are locale subpaths. It's easiest to explain by example:
106
107```
108myapp.com ---> Homepage in default lang
109myapp.com/de ---> Homepage in German
110```
111
112This functionality is not enabled by default, and must be passed as an option into the `NextI18Next` constructor as a config option:
113
114```jsx
115new NextI18Next({
116 localeSubpaths: {
117 de: 'de'
118 }
119})
120```
121
122The `localeSubpaths` object must also be passed into `next.config.js`, via the `nextI18NextRewrites` util, which you can import from `next-i18next/rewrites`.
123
124The `localeSubpaths` option is a key/value mapping, where keys are the locale itself (case sensitive) and values are the subpath without slashes.
125
126Now, all your page routes will be duplicated across all your locale subpaths. Here's an example:
127
128```jsx
129----- Config -----
130new NextI18Next({
131 localeSubpaths: {
132 fr: 'fr',
133 de: 'german',
134 en: 'eng',
135 }
136})
137
138----- Output -----
139myapp.com/fr
140myapp.com/german
141myapp.com/eng
142```
143
144When using the localeSubpaths option, our middleware will redirect as needed in the wrapped `getInitialProps` one level above your `_app`, so none of your code will be called.
145
146The 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:
147
148```jsx
149<Link href='/some-page'>
150```
151
152With this link, we would expect someone whose language is set to French to automatically be directed to `/fr/some-page`.
153
154To do that, we must import `Link` from your `NextI18Next` instance, **not next/router**:
155
156```jsx
157// This is our initialised `NextI18Next` instance
158import { Link } from '../i18n'
159
160const SomeLink = () => (
161 <Link href='/some-page'>
162 This will magically prepend locale subpaths
163 </Link>
164)
165```
166
167We 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.
168
169```jsx
170// This is our initialised `NextI18Next` instance
171import { Router } from '../i18n'
172
173const SomeButton = () => (
174 <button
175 onClick={() => Router.push('/some-page')}
176 >
177 This will magically prepend locale subpaths
178 </button>
179)
180```
181
182## Accessing the Current Language
183
184In 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](https://react.i18next.com/latest/withtranslation-hoc).
185
186If for some reason you need to access the current language and `withTranslation` doesn't suit your needs, you can use the `I18nContext`:
187
188```jsx
189import { I18nContext } from 'next-i18next'
190
191const { i18n: { language } } = useContext(I18nContext)
192```
193
194## Options
195
196| Key | Default value |
197| ------------- | ------------- |
198| `browserLanguageDetection` | `true` |
199| `defaultNS` | `'common'` |
200| `defaultLanguage` | `'en'` |
201| `ignoreRoutes` | `['/_next/', '/static/', '/public/', '/api/']` |
202| `otherLanguages` (required) | `[]` |
203| `localeExtension` | `'json'` |
204| `localePath` (required) | `'/public/static/locales'` |
205| `localeStructure` | `'{{lng}}/{{ns}}'` |
206| `localeSubpaths` | `{}` |
207| `serverLanguageDetection` | `true` |
208| `strictMode` | `true` |
209| `use` (for plugins) | `[]` |
210| `customDetectors` | `[]` |
211| `shallowRender` | `false` |
212
213_This table contains options which are specific to next-i18next. All other [i18next options](https://www.i18next.com/overview/configuration-options) can be passed in as well._
214
215## Notes
216
217- [`next export` is not supported.](https://github.com/isaachinman/next-i18next/issues/780)
218- [To add a `lang` attribute to your top-level html DOM node, you must create a `_document.js` file.](https://github.com/isaachinman/next-i18next/issues/20#issuecomment-443461652)
219- [Localising `next/head` requires special consideration due to NextJs internals](https://github.com/isaachinman/next-i18next/issues/251#issuecomment-479421852).
220
221## Contributors
222
223Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
224
225<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
226<!-- prettier-ignore -->
227<table cellspacing="0" cellpadding="1"><tr><td><a href="https://github.com/capellini"><img src="https://avatars3.githubusercontent.com/u/75311?v=4" width="100px;" height="100px;" alt="Rob Capellini"/><br /><sub><b>Rob Capellini</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=capellini" title="Tests">⚠️</a></td><td><a href="https://en.kachkaev.ru"><img src="https://avatars3.githubusercontent.com/u/608862?v=4" width="100px;" height="100px;" alt="Alexander Kachkaev"/><br /><sub><b>Alexander Kachkaev</b></sub></a><br /><a href="#talk-kachkaev" title="Talks">📢</a> <a href="#question-kachkaev" title="Answering Questions">💬</a> <a href="#ideas-kachkaev" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=kachkaev" title="Tests">⚠️</a></td><td><a href="https://kandelborg.dk"><img src="https://avatars1.githubusercontent.com/u/33042011?v=4" width="100px;" height="100px;" alt="Mathias Wøbbe"/><br /><sub><b>Mathias Wøbbe</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Code">💻</a> <a href="#ideas-MathiasKandelborg" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=MathiasKandelborg" title="Tests">⚠️</a></td><td><a href="http://lucasfeliciano.com"><img src="https://avatars3.githubusercontent.com/u/968014?v=4" width="100px;" height="100px;" alt="Lucas Feliciano"/><br /><sub><b>Lucas Feliciano</b></sub></a><br /><a href="#ideas-lucasfeliciano" title="Ideas, Planning, & Feedback">🤔</a> <a href="#review-lucasfeliciano" title="Reviewed Pull Requests">👀</a></td><td><a href="http://www.fifteenprospects.com"><img src="https://avatars2.githubusercontent.com/u/6932550?v=4" width="100px;" height="100px;" alt="Ryan Leung"/><br /><sub><b>Ryan Leung</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=minocys" title="Code">💻</a></td><td><a href="http://nathanfriemel.com"><img src="https://avatars3.githubusercontent.com/u/1325835?v=4" width="100px;" height="100px;" alt="Nathan Friemel"/><br /><sub><b>Nathan Friemel</b></sub></a><br /><a href="https://github.com/isaachinman/next-i18next/commits?author=nathanfriemel" title="Code">💻</a> <a href="https://github.com/isaachinman/next-i18next/commits?author=nathanfriemel" title="Documentation">📖</a> <a href="#example-nathanfriemel" title="Examples">💡</a> <a href="#ideas-nathanfriemel" title="Ideas, Planning, & Feedback">🤔</a></td></tr></table>
228<!-- ALL-CONTRIBUTORS-LIST:END -->
229
230This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
231
232## Supported by BrowserStack
233Thanks to [BrowserStack](https://browserstack.com/) for their support of this open-source project.
234
235<img src="https://3fxtqy18kygf3on3bu39kh93-wpengine.netdna-ssl.com/wp-content/themes/browserstack/img/browserstack-logo.svg" width="150">