You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* refactor: Use rewrites instead of custom server (#689)
* chore: Add .e2e to npmignore
* v5.0.0-beta.0
* chore: Add scripts to npmignore
* v5.0.0-beta.1
* 5.0.0-beta.2
* refactor: Require absolute localePath, and refactor out usage of eval
* 5.0.0-beta.3
* refactor: Clean up example dir, remove class components
* chore: Update core-js to v3
* docs: Update README
* v5.0.0-beta.4
* Clean up release
Copy file name to clipboardExpand all lines: README.md
+22-55Lines changed: 22 additions & 55 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ If you are using next-i18next in production, please consider [sponsoring the pac
13
13
14
14
While `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
15
16
-
A live demo is [available here](http://next-i18next.com/). 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](./examples/simple/) - nothing more, nothing less.
16
+
A live demo is [available here](http://next-i18next.com/). This demo app is the [simple example](./examples/simple/) - nothing more, nothing less.
17
17
18
18
## Setup
19
19
@@ -43,59 +43,49 @@ This structure can also be seen in the [simple example](./examples/simple).
43
43
44
44
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.
45
45
46
-
If translations are not found in `config.localePath` or `public/static/locales` an attempt will be made to find the locales in `static/locales`, if found a deprecation warning will be logged.
47
-
48
46
### 3. Project setup
49
47
50
48
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:
Note that `localePath` is required, and must be an absolute path.
63
+
61
64
[A full list of options can be seen here](#options).
62
65
63
66
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](./examples/simple/i18n.js) file.
64
67
65
68
After creating and exporting your `NextI18Next` instance, you need to take the following steps to get things working:
66
69
67
70
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](./examples/simple/pages/_app.js).
68
-
Your app component must either extend `App` if it's a class component or define a `getInitialProps` if it's a function component [(explanation here)](https://github.com/isaachinman/next-i18next/issues/615#issuecomment-575578375).
69
-
2. Create a `server.js` file inside your root directory, initialise an [express](https://www.npmjs.com/package/express) server, and use the `nextI18NextMiddleware` middleware with your `nextI18Next` instance passed in. You can see this approach in the [examples/simple/server.js](./examples/simple/server.js).
70
-
3. Update the scripts in `package.json` to:
71
-
```
72
-
{
73
-
"scripts": {
74
-
"dev": "node server.js",
75
-
"build": "next build",
76
-
"start": "NODE_ENV=production node server.js"
77
-
}
78
-
}
79
-
```
80
-
For more info, see [the NextJs section on custom servers](https://github.com/zeit/next.js#custom-server-and-routing).
71
+
Your 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).
72
+
2. 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).
81
73
82
74
Note: You can pass `shallowRender: true` into config options to avoid triggering getInitialProps when `changeLanguage` method is invoked.
83
75
84
76
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:
85
77
86
78
```jsx
87
-
importReactfrom'react'
88
-
89
79
// This is our initialised `NextI18Next` instance
90
80
import { withTranslation } from'../i18n'
91
81
92
-
classFooterextendsReact.Component {
93
-
render() {
94
-
return (
95
-
<footer>{this.props.t('description')}</footer>
96
-
)
97
-
}
98
-
}
82
+
constFooter= ({ t }) => (
83
+
<footer>
84
+
<p>
85
+
{t('description')}
86
+
</p>
87
+
</footer>
88
+
)
99
89
100
90
exportdefaultwithTranslation('footer')(Footer)
101
91
```
@@ -129,6 +119,8 @@ new NextI18Next({
129
119
})
130
120
```
131
121
122
+
The `localeSubpaths` object must also be passed into `next.config.js`, via the `nextI18NextRewrites` util, which you can import from `next-i18next/rewrites`.
123
+
132
124
The `localeSubpaths` option is a key/value mapping, where keys are the locale itself (case sensitive) and values are the subpath without slashes.
133
125
134
126
Now, all your page routes will be duplicated across all your locale subpaths. Here's an example:
@@ -149,7 +141,7 @@ myapp.com/german
149
141
myapp.com/eng
150
142
```
151
143
152
-
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.
144
+
When 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.
153
145
154
146
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:
155
147
@@ -162,8 +154,6 @@ With this link, we would expect someone whose language is set to French to autom
162
154
To do that, we must import `Link` from your `NextI18Next` instance, **not next/router**:
163
155
164
156
```jsx
165
-
importReactfrom'react'
166
-
167
157
// This is our initialised `NextI18Next` instance
168
158
import { Link } from'../i18n'
169
159
@@ -174,12 +164,9 @@ const SomeLink = () => (
174
164
)
175
165
```
176
166
177
-
We can also navigate imperatively with locale subpaths by importing `Router` from your `NextI18Next` instance.
178
-
The exported Router shares the same API as the native Next Router. The push, replace, and prefetch functions will automatically prepend locale subpaths.
167
+
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.
179
168
180
169
```jsx
181
-
importReactfrom'react'
182
-
183
170
// This is our initialised `NextI18Next` instance
184
171
import { Router } from'../i18n'
185
172
@@ -192,25 +179,6 @@ const SomeButton = () => (
192
179
)
193
180
```
194
181
195
-
## Custom Routing
196
-
197
-
Custom routing can be achieved via the `app.render` method:
/* Third, add catch-all GET for non-custom routes */
211
-
server.get('*', (req, res) =>handle(req, res))
212
-
```
213
-
214
182
## Accessing the Current Language
215
183
216
184
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](https://react.i18next.com/latest/withtranslation-hoc).
@@ -250,7 +218,6 @@ _This table contains options which are specific to next-i18next. All other [i18n
250
218
## Notes
251
219
252
220
-[`next export` will result in a _client-side only_ React application.](https://github.com/isaachinman/next-i18next/issues/10)
253
-
-[Serverless (e.g. Now 2.0) is not currently supported](https://github.com/isaachinman/next-i18next/issues/274).
254
221
-[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)
255
222
-[Localising `next/head` requires special consideration due to NextJs internals](https://github.com/isaachinman/next-i18next/issues/251#issuecomment-479421852).
0 commit comments