From 3d4dbac93d79b9e4b86f269a29d94643f80a6b19 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Thu, 29 Mar 2018 14:47:52 +0200 Subject: [PATCH] :alien: Synchroniser la langue initiale --- source/containers/Layout.js | 17 ----------------- source/entry.dev.js | 6 +++++- source/entry.prod.js | 6 +++++- source/i18n.js | 23 +++++++++++++++++++++++ source/reducers.js | 4 ++-- 5 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 source/i18n.js diff --git a/source/containers/Layout.js b/source/containers/Layout.js index a20220064..ea38ec460 100644 --- a/source/containers/Layout.js +++ b/source/containers/Layout.js @@ -21,23 +21,6 @@ import ReactPiwik from 'Components/Tracker' import createHistory from 'history/createBrowserHistory' import { Header, Footer } from 'Components/pages/Header' import { getIframeOption } from '../utils' -import queryString from 'query-string' -import enTranslations from '../locales/en.yaml' - -let lang = getIframeOption('lang') || queryString.parse(location.search)['lang'] || sessionStorage['lang'] -if (lang) sessionStorage['lang'] = lang - -i18next - .init({ - debug: true, - lng: lang, - resources: { - en: { - translation: enTranslations - }} - }, (err, t) => { - console.log("Error from i18n load",err,t) - }) const piwik = new ReactPiwik({ url: 'stats.data.gouv.fr', diff --git a/source/entry.dev.js b/source/entry.dev.js index 4b3f03b57..6a2e1150d 100644 --- a/source/entry.dev.js +++ b/source/entry.dev.js @@ -8,6 +8,9 @@ import debounceFormChangeActions from './debounceFormChangeActions' import computeThemeColours from './components/themeColours' import { getIframeOption, getUrl } from './utils' +import { rules, rulesFr } from 'Engine/rules' +import lang from './i18n' + import App from './containers/App.dev' let initialStore = { @@ -20,7 +23,8 @@ let enhancer = compose( DevTools.instrument() ) -let store = createStore(reducers, initialStore, enhancer) +let initialRules = lang == 'en' ? rules : rulesFr +let store = createStore(reducers(initialRules), initialStore, enhancer) let anchor = document.querySelector('#js') render(, anchor) diff --git a/source/entry.prod.js b/source/entry.prod.js index abd42a38a..f97d43c4a 100644 --- a/source/entry.prod.js +++ b/source/entry.prod.js @@ -8,6 +8,9 @@ import { getIframeOption, getUrl } from './utils' import { Provider } from 'react-redux' import Layout from './containers/Layout' +import { rules, rulesFr } from 'Engine/rules' +import lang from './i18n' + let initialStore = { iframe: getUrl().includes('iframe'), themeColours: computeThemeColours(getIframeOption('couleur')) @@ -15,7 +18,8 @@ let initialStore = { let enhancer = compose(applyMiddleware(debounceFormChangeActions())) -let store = createStore(reducers, initialStore, enhancer) +let initialRules = lang == 'en' ? rules : rulesFr +let store = createStore(reducers(initialRules), initialStore, enhancer) let anchor = document.querySelector('#js') render( diff --git a/source/i18n.js b/source/i18n.js new file mode 100644 index 000000000..38f4573d0 --- /dev/null +++ b/source/i18n.js @@ -0,0 +1,23 @@ +import { I18nextProvider } from 'react-i18next'; +import i18next from 'i18next' +import queryString from 'query-string' + +import { getIframeOption } from './utils' +import enTranslations from './locales/en.yaml' + +let lang = getIframeOption('lang') || queryString.parse(location.search)['lang'] || sessionStorage['lang'] +if (lang) sessionStorage['lang'] = lang + +i18next + .init({ + debug: true, + lng: lang, + resources: { + en: { + translation: enTranslations + }} + }, (err, t) => { + console.log("Error from i18n load",err,t) + }) + +export default lang diff --git a/source/reducers.js b/source/reducers.js index 9342acce7..7cea8029a 100644 --- a/source/reducers.js +++ b/source/reducers.js @@ -202,7 +202,7 @@ function currentExample(state = null, { type, situation, name }) { } } -export default reduceReducers( +export default (initialRules) => reduceReducers( combineReducers({ sessionId: (id = Math.floor(Math.random() * 1000000000000) + '') => id, // this is handled by redux-form, pas touche ! @@ -233,5 +233,5 @@ export default reduceReducers( currentExample }), // cross-cutting concerns because here `state` is the whole state tree - reduceSteps(ReactPiwik, rulesFr, formatInputs(rulesFr, formValueSelector)) + reduceSteps(ReactPiwik, initialRules, formatInputs(initialRules, formValueSelector)) )