Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
8acdae66
Commit
8acdae66
authored
Aug 13, 2017
by
whphhg
Committed by
Tim Neutkens
Aug 13, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for fetching multiple translation files (#2743)
* Add support for fetching multiple translation files * Cleanup
parent
7a5a6bce
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
58 additions
and
11 deletions
+58
-11
Post.js
examples/with-i18next/components/Post.js
+19
-0
index.js
examples/with-i18next/pages/index.js
+10
-2
namespace1.json
examples/with-i18next/static/locales/id/namespace1.json
+3
-0
namespace1.json
examples/with-i18next/static/locales/pt/namespace1.json
+3
-0
startI18n.js
examples/with-i18next/tools/startI18n.js
+8
-2
translationHelpers.js
examples/with-i18next/tools/translationHelpers.js
+15
-7
No files found.
examples/with-i18next/components/Post.js
0 → 100644
View file @
8acdae66
import
React
from
'react'
import
{
translate
}
from
'react-i18next'
class
Post
extends
React
.
Component
{
constructor
(
props
)
{
super
(
props
)
this
.
t
=
props
.
t
}
render
()
{
return
(
<
div
>
{
this
.
t
(
'namespace1:greatMorning'
)}
<
/div
>
)
}
}
export
default
translate
([
'namespace1'
])(
Post
)
examples/with-i18next/pages/index.js
View file @
8acdae66
...
...
@@ -3,13 +3,18 @@ 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'
,
'http://localhost:3000/static/locales/'
)
const
translations
=
await
getTranslation
(
lang
,
[
'common'
,
'namespace1'
],
'http://localhost:3000/static/locales/'
)
return
{
translations
}
}
...
...
@@ -23,7 +28,10 @@ export default class Homepage extends Component {
render
(
props
)
{
return
(
<
I18nextProvider
i18n
=
{
this
.
i18n
}
>
<
Title
/>
<
div
>
<
Title
/>
<
Post
/>
<
/div
>
<
/I18nextProvider
>
)
}
...
...
examples/with-i18next/static/locales/id/namespace1.json
0 → 100644
View file @
8acdae66
{
"greatMorning"
:
"Pagi yang indah!"
}
examples/with-i18next/static/locales/pt/namespace1.json
0 → 100644
View file @
8acdae66
{
"greatMorning"
:
"Maravilhosa manhã!"
}
examples/with-i18next/tools/startI18n.js
View file @
8acdae66
import
i18n
from
'i18next'
const
startI18n
=
(
file
,
lang
)
=>
i18n
.
init
({
/**
* 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
:
file
,
resources
:
file
s
,
ns
:
[
'common'
],
defaultNS
:
'common'
,
debug
:
false
...
...
examples/with-i18next/tools/translationHelpers.js
View file @
8acdae66
/* global fetch */
import
'isomorphic-fetch'
export
async
function
getTranslation
(
lang
,
file
,
baseUrl
)
{
const
response
=
await
fetch
(
`
${
baseUrl
}${
lang
}
/
${
file
}
.json`
)
const
json
=
await
response
.
json
()
/**
* 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
=
{}
return
{
[
lang
]:
{
[
file
]:
json
}
for
(
let
file
of
files
)
{
const
response
=
await
fetch
(
`
${
baseUrl
}${
lang
}
/
${
file
}
.json`
)
translation
[
file
]
=
await
response
.
json
()
}
return
{
[
lang
]:
translation
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment