提交 c178e98a 编写于 作者: I Isaac Hinman 提交者: Tim Neutkens

Move with-i18next example to external repo (#5743)

上级 e3c7d3fd
# with-i18next example
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/segmentio/create-next-app) with [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) or [npx](https://github.com/zkat/npx#readme) to bootstrap the example:
```bash
npx create-next-app --example with-i18next with-i18next-app
# or
yarn create next-app --example with-i18next with-i18next-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-i18next
cd with-i18next
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
alternatively
```bash
yarn && yarn dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
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/)
The `with-i18next` example is maintained within the `react-i18next` repository and [can be found here](https://github.com/i18next/react-i18next/tree/master/example/nextjs).
import React from 'react'
import { translate } from 'react-i18next'
class Post extends React.Component {
render () {
const { t } = this.props
return (
<div>
{t('namespace1:greatMorning')}
</div>
)
}
}
export default translate(['namespace1'])(Post)
import { translate } from 'react-i18next'
export default translate(['common'])((props) => (<h1>{props.t('hello')}, {props.t('morning')}</h1>))
{
"name": "with-i18next",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"i18next": "^10.0.7",
"isomorphic-unfetch": "^2.0.0",
"next": "latest",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-i18next": "^6.1.0"
},
"license": "ISC"
}
import React, { Component } from 'react'
import { I18nextProvider } from 'react-i18next'
import startI18n from '../tools/startI18n'
import { getTranslation } from '../tools/translationHelpers'
import Title from '../components/Title'
import Post from '../components/Post'
// get language from query parameter or url path
const lang = 'id'
export default class Homepage extends Component {
static async getInitialProps () {
const translations = await getTranslation(
lang,
['common', 'namespace1'],
'http://localhost:3000/static/locales/'
)
return { translations }
}
constructor (props) {
super(props)
this.i18n = startI18n(props.translations, lang)
}
render (props) {
return (
<I18nextProvider i18n={this.i18n}>
<div>
<Title />
<Post />
</div>
</I18nextProvider>
)
}
}
{
"hello": "halo"
}
\ No newline at end of file
{
"hello": "e ae tche",
"morning": "manha"
}
\ No newline at end of file
import i18n from 'i18next'
/**
* Initialize a i18next instance.
* @function startI18n
* @param {object} files - Translation files.
* @param {string} lang - Active language.
*/
const startI18n = (files, lang) => i18n.init({
lng: lang, // active language http://i18next.com/translate/
fallbackLng: 'pt',
resources: files,
ns: ['common'],
defaultNS: 'common',
debug: false
})
export default startI18n
/* global fetch */
import 'isomorphic-unfetch'
/**
* Fetch translation file(s).
* @function getTranslation
* @param {string} lang - Language to fetch.
* @param {array} files - Translation files to fetch.
* @param {string} baseUrl - Locale location.
* @return {object} Fetched translation files.
*/
export async function getTranslation (lang, files, baseUrl) {
let translation = {}
for (let file of files) {
const response = await fetch(`${baseUrl}${lang}/${file}.json`)
translation[file] = await response.json()
}
return { [lang]: translation }
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册