File tree 6 files changed +58
-11
lines changed
6 files changed +58
-11
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change @@ -3,13 +3,18 @@ import { I18nextProvider } from 'react-i18next'
3
3
import startI18n from '../tools/startI18n'
4
4
import { getTranslation } from '../tools/translationHelpers'
5
5
import Title from '../components/Title'
6
+ import Post from '../components/Post'
6
7
7
8
// get language from query parameter or url path
8
9
const lang = 'id'
9
10
10
11
export default class Homepage extends Component {
11
12
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
+ )
13
18
14
19
return { translations }
15
20
}
@@ -23,7 +28,10 @@ export default class Homepage extends Component {
23
28
render ( props ) {
24
29
return (
25
30
< I18nextProvider i18n = { this . i18n } >
26
- < Title />
31
+ < div >
32
+ < Title />
33
+ < Post />
34
+ </ div >
27
35
</ I18nextProvider >
28
36
)
29
37
}
Original file line number Diff line number Diff line change
1
+ {
2
+ "greatMorning" : " Pagi yang indah!"
3
+ }
Original file line number Diff line number Diff line change
1
+ {
2
+ "greatMorning" : " Maravilhosa manhã!"
3
+ }
Original file line number Diff line number Diff line change 1
1
import i18n from 'i18next'
2
2
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 ( {
4
10
lng : lang , // active language http://i18next.com/translate/
5
11
fallbackLng : 'pt' ,
6
- resources : file ,
12
+ resources : files ,
7
13
ns : [ 'common' ] ,
8
14
defaultNS : 'common' ,
9
15
debug : false
Original file line number Diff line number Diff line change 1
1
/* global fetch */
2
2
import 'isomorphic-fetch'
3
3
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 = { }
7
14
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 ( )
12
18
}
19
+
20
+ return { [ lang ] : translation }
13
21
}
You can’t perform that action at this time.
0 commit comments