Commit 8acdae66 authored by whphhg's avatar whphhg Committed by Tim Neutkens

Add support for fetching multiple translation files (#2743)

* Add support for fetching multiple translation files

* Cleanup
parent 7a5a6bce
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)
...@@ -3,13 +3,18 @@ import { I18nextProvider } from 'react-i18next' ...@@ -3,13 +3,18 @@ import { I18nextProvider } from 'react-i18next'
import startI18n from '../tools/startI18n' import startI18n from '../tools/startI18n'
import { getTranslation } from '../tools/translationHelpers' import { getTranslation } from '../tools/translationHelpers'
import Title from '../components/Title' import Title from '../components/Title'
import Post from '../components/Post'
// get language from query parameter or url path // get language from query parameter or url path
const lang = 'id' const lang = 'id'
export default class Homepage extends Component { export default class Homepage extends Component {
static async getInitialProps () { 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 } return { translations }
} }
...@@ -23,7 +28,10 @@ export default class Homepage extends Component { ...@@ -23,7 +28,10 @@ export default class Homepage extends Component {
render (props) { render (props) {
return ( return (
<I18nextProvider i18n={this.i18n}> <I18nextProvider i18n={this.i18n}>
<Title /> <div>
<Title />
<Post />
</div>
</I18nextProvider> </I18nextProvider>
) )
} }
......
{
"greatMorning": "Pagi yang indah!"
}
{
"greatMorning": "Maravilhosa manhã!"
}
import i18n from 'i18next' 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/ lng: lang, // active language http://i18next.com/translate/
fallbackLng: 'pt', fallbackLng: 'pt',
resources: file, resources: files,
ns: ['common'], ns: ['common'],
defaultNS: 'common', defaultNS: 'common',
debug: false debug: false
......
/* global fetch */ /* global fetch */
import 'isomorphic-fetch' import 'isomorphic-fetch'
export async function getTranslation (lang, file, baseUrl) { /**
const response = await fetch(`${baseUrl}${lang}/${file}.json`) * Fetch translation file(s).
const json = await response.json() * @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 { for (let file of files) {
[lang]: { const response = await fetch(`${baseUrl}${lang}/${file}.json`)
[file]: json translation[file] = await response.json()
}
} }
return { [lang]: translation }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment