Skip to content

Commit c9d0670

Browse files
Átila Fassinatimneutkens
Átila Fassina
authored andcommitted
with-i18next example (vercel#1496)
* examples/with-i18next: create folder and initial settings * examples/with-i18next:abstract url and object keys - finish readme * examples/with-i18next: next@beta is not actually necessary * examples/with-it18next: fix standardjs eslint warnings * examples/with-i18next: review updates
1 parent 1f642dc commit c9d0670

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

examples/with-i18next/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
# with-i18next example
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js):
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-i18next
10+
cd with-i18next
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
alternatively
21+
```bash
22+
yarn && yarn dev
23+
```
24+
25+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
26+
27+
```bash
28+
now
29+
```
30+
31+
## The idea behind the example
32+
33+
This example shows how to add internationalisation through [i18next](https://github.com/i18next/i18next) to your NextJS app. The possibilities and features are documented in the [i18next project](http://i18next.com/translate/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { translate } from 'react-i18next'
2+
export default translate(['common'])((props) => (<h1>{props.t('hello')}</h1>))

examples/with-i18next/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "with-i18next",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"i18next": "^7.1.3",
11+
"isomorphic-fetch": "^2.2.1",
12+
"next": "*",
13+
"react": "^15.4.2",
14+
"react-dom": "^15.4.2",
15+
"react-i18next": "^2.2.1"
16+
},
17+
"author": "",
18+
"license": "ISC"
19+
}

examples/with-i18next/pages/index.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { Component } from 'react'
2+
import { I18nextProvider } from 'react-i18next'
3+
import startI18n from '../tools/startI18n'
4+
import { getTranslation } from '../tools/translationHelpers'
5+
import Title from '../components/Title'
6+
7+
export default class Homepage extends Component {
8+
static async getInitialProps () {
9+
const translations = await getTranslation('pt', 'common', 'http://localhost:3000/static/locales/')
10+
11+
return { translations }
12+
}
13+
14+
constructor (props) {
15+
super(props)
16+
17+
this.i18n = startI18n(props.translations)
18+
}
19+
20+
render (props) {
21+
return (
22+
<I18nextProvider i18n={this.i18n}>
23+
<Title />
24+
</ I18nextProvider>
25+
)
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "e ae tche"
3+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import i18n from 'i18next'
2+
3+
const startI18n = file => i18n.init({
4+
fallbackLng: 'pt',
5+
resources: file,
6+
ns: ['common'],
7+
defaultNS: 'common',
8+
debug: false
9+
})
10+
11+
export default startI18n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* global fetch */
2+
import 'isomorphic-fetch'
3+
4+
export async function getTranslation (lang, file, baseUrl) {
5+
const response = await fetch(`${baseUrl}${lang}/${file}.json`)
6+
const json = await response.json()
7+
8+
return {
9+
[lang]: {
10+
[file]: json
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)