Skip to content

Commit 8acdae6

Browse files
whphhgtimneutkens
authored andcommitted
Add support for fetching multiple translation files (vercel#2743)
* Add support for fetching multiple translation files * Cleanup
1 parent 7a5a6bc commit 8acdae6

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react'
2+
import { translate } from 'react-i18next'
3+
4+
class Post extends React.Component {
5+
constructor (props) {
6+
super(props)
7+
this.t = props.t
8+
}
9+
10+
render () {
11+
return (
12+
<div>
13+
{this.t('namespace1:greatMorning')}
14+
</div>
15+
)
16+
}
17+
}
18+
19+
export default translate(['namespace1'])(Post)

examples/with-i18next/pages/index.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import { I18nextProvider } from 'react-i18next'
33
import startI18n from '../tools/startI18n'
44
import { getTranslation } from '../tools/translationHelpers'
55
import Title from '../components/Title'
6+
import Post from '../components/Post'
67

78
// get language from query parameter or url path
89
const lang = 'id'
910

1011
export default class Homepage extends Component {
1112
static async getInitialProps () {
12-
const translations = await getTranslation(lang, 'common', 'http://localhost:3000/static/locales/')
13+
const translations = await getTranslation(
14+
lang,
15+
['common', 'namespace1'],
16+
'http://localhost:3000/static/locales/'
17+
)
1318

1419
return { translations }
1520
}
@@ -23,7 +28,10 @@ export default class Homepage extends Component {
2328
render (props) {
2429
return (
2530
<I18nextProvider i18n={this.i18n}>
26-
<Title />
31+
<div>
32+
<Title />
33+
<Post />
34+
</div>
2735
</I18nextProvider>
2836
)
2937
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"greatMorning": "Pagi yang indah!"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"greatMorning": "Maravilhosa manhã!"
3+
}

examples/with-i18next/tools/startI18n.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import i18n from 'i18next'
22

3-
const startI18n = (file, lang) => i18n.init({
3+
/**
4+
* Initialize a i18next instance.
5+
* @function startI18n
6+
* @param {object} files - Translation files.
7+
* @param {string} lang - Active language.
8+
*/
9+
const startI18n = (files, lang) => i18n.init({
410
lng: lang, // active language http://i18next.com/translate/
511
fallbackLng: 'pt',
6-
resources: file,
12+
resources: files,
713
ns: ['common'],
814
defaultNS: 'common',
915
debug: false
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
/* global fetch */
22
import 'isomorphic-fetch'
33

4-
export async function getTranslation (lang, file, baseUrl) {
5-
const response = await fetch(`${baseUrl}${lang}/${file}.json`)
6-
const json = await response.json()
4+
/**
5+
* Fetch translation file(s).
6+
* @function getTranslation
7+
* @param {string} lang - Language to fetch.
8+
* @param {array} files - Translation files to fetch.
9+
* @param {string} baseUrl - Locale location.
10+
* @return {object} Fetched translation files.
11+
*/
12+
export async function getTranslation (lang, files, baseUrl) {
13+
let translation = {}
714

8-
return {
9-
[lang]: {
10-
[file]: json
11-
}
15+
for (let file of files) {
16+
const response = await fetch(`${baseUrl}${lang}/${file}.json`)
17+
translation[file] = await response.json()
1218
}
19+
20+
return { [lang]: translation }
1321
}

0 commit comments

Comments
 (0)