👽 Traduction avec i18next

pull/188/head
Laurent Bossavit 2018-03-13 14:23:53 +01:00 committed by Mael
parent 95e03eb438
commit 310959a02b
5 changed files with 96 additions and 32 deletions

3
locales/en.json Normal file
View File

@ -0,0 +1,3 @@
{
"news":"Welcome to the english-language version of the salary cost estimator"
}

View File

@ -15,7 +15,6 @@
"not ie < 11"
],
"dependencies": {
"live-server": "^1.2.0",
"@babel/polyfill": "^7.0.0-beta.34",
"classnames": "^2.2.5",
"dedent-js": "^1.0.1",
@ -24,8 +23,11 @@
"fuse.js": "^3.2.0",
"global": "^4.3.2",
"google-translate-api": "^2.3.0",
"i18next": "^10.0.7",
"i18next-xhr-backend": "^1.5.1",
"ignore-loader": "^0.1.2",
"js-yaml": "^3.9.1",
"live-server": "^1.2.0",
"marked": "^0.3.9",
"mocha": "^5.0.0",
"mocha-webpack": "^1.0.1",
@ -38,6 +40,7 @@
"react-dom": "^16.2.0",
"react-helmet": "^5.2.0",
"react-highlight-words": "^0.10.0",
"react-i18next": "^7.5.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",

View File

@ -1,4 +1,5 @@
import React, { Component } from 'react'
import { Trans } from 'react-i18next'
import './Pages.css'
import './Home.css'
import TargetSelection from '../TargetSelection'
@ -28,8 +29,10 @@ export default class Home extends Component {
}}
/>
<p style={opacityStyle}>
<Trans i18nKey="news">
Cette nouvelle version du site vous permet de simuler un CDD ou CDI,
aux taux de 2018.
</Trans>
</p>
</div>
<div id="content">

View File

@ -1,3 +1,7 @@
import { I18nextProvider } from 'react-i18next';
import i18next from 'i18next'
import XHR from 'i18next-xhr-backend';
import React, { Component } from 'react'
import './Layout.css'
import './reset.css'
@ -19,6 +23,29 @@ import createHistory from 'history/createBrowserHistory'
import { Header, Footer } from 'Components/pages/Header'
import { getIframeOption } from '../utils'
function loadLocales(url, options, callback, data) {
try {
let translations = require('../../locales/'+url+'.json');
callback(translations, {status: '200'});
} catch (e) {
callback(null, {status: '404'});
}
}
i18next
.use(XHR)
.init({
debug: true,
lng: 'en',
backend: {
loadPath: '{{lng}}',
parse: (data) => data,
ajax: loadLocales
}
}, (err, t) => {
// ...
});
const piwik = new ReactPiwik({
url: 'stats.data.gouv.fr',
siteId: 39,
@ -39,36 +66,38 @@ export default class Layout extends Component {
// track the initial pageview
ReactPiwik.push(['trackPageView'])
return (
<Router history={piwik.connectToHistory(this.history)}>
<>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/contact" component={Contact} />
<Route path="/règle/:name" component={RulePage} />
{/* Redirect to be removed in March (Google should have understood...)*/}
<Route
path="/regle/:name"
render={({ match }) => (
<Redirect to={`/règle/${match.params.name}`} />
)}
/>
<Route path="/règles" component={RulesList} />
<Route path="/mecanismes" component={Mecanisms} />
<Redirect from="/simu/surcoût-CDD/intro" to="/" />
<Redirect from="/simu/surcoût-CDD" to="/" />
<Route path="/simu/:targets" component={Simulateur} />
<Route path="/à-propos" component={About} />
<Route path="/intégrer" component={Integration} />
<Route path="/contribuer" component={Contribution} />
<Redirect from="/simu/" to="/" />
<Redirect from="/simulateur" to="/" />
<Redirect from="/couleur.html" to="/" />
<Route component={Route404} />
</Switch>
<Footer />
</>
</Router>
<I18nextProvider i18n={ i18next }>
<Router history={piwik.connectToHistory(this.history)}>
<>
<Header />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/contact" component={Contact} />
<Route path="/règle/:name" component={RulePage} />
{/* Redirect to be removed in March (Google should have understood...)*/}
<Route
path="/regle/:name"
render={({ match }) => (
<Redirect to={`/règle/${match.params.name}`} />
)}
/>
<Route path="/règles" component={RulesList} />
<Route path="/mecanismes" component={Mecanisms} />
<Redirect from="/simu/surcoût-CDD/intro" to="/" />
<Redirect from="/simu/surcoût-CDD" to="/" />
<Route path="/simu/:targets" component={Simulateur} />
<Route path="/à-propos" component={About} />
<Route path="/intégrer" component={Integration} />
<Route path="/contribuer" component={Contribution} />
<Redirect from="/simu/" to="/" />
<Redirect from="/simulateur" to="/" />
<Redirect from="/couleur.html" to="/" />
<Route component={Route404} />
</Switch>
<Footer />
</>
</Router>
</I18nextProvider>
)
}
}

View File

@ -3197,7 +3197,7 @@ hoist-non-react-statics@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-1.2.0.tgz#aa448cf0986d55cc40773b17174b7dd066cb7cfb"
hoist-non-react-statics@^2.3.0, hoist-non-react-statics@^2.5.0:
hoist-non-react-statics@^2.3.0, hoist-non-react-statics@^2.3.1, hoist-non-react-statics@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.0.tgz#d2ca2dfc19c5a91c5a6615ce8e564ef0347e2a40"
@ -3251,6 +3251,12 @@ html-minifier@^3.5.8:
relateurl "0.2.x"
uglify-js "3.3.x"
html-parse-stringify2@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/html-parse-stringify2/-/html-parse-stringify2-2.0.1.tgz#dc5670b7292ca158b7bc916c9a6735ac8872834a"
dependencies:
void-elements "^2.0.1"
http-auth@3.1.x:
version "3.1.3"
resolved "https://registry.yarnpkg.com/http-auth/-/http-auth-3.1.3.tgz#945cfadd66521eaf8f7c84913d377d7b15f24e31"
@ -3313,6 +3319,14 @@ https-browserify@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/https-browserify/-/https-browserify-1.0.0.tgz#ec06c10e0a34c0f2faf199f7fd7fc78fffd03c73"
i18next-xhr-backend@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/i18next-xhr-backend/-/i18next-xhr-backend-1.5.1.tgz#50282610780c6a696d880dfa7f4ac1d01e8c3ad5"
i18next@^10.0.7:
version "10.5.0"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-10.5.0.tgz#a2a90e67774fa85b8ff9cd0063e697e482440de7"
iconv-lite@0.4.19, iconv-lite@^0.4.17, iconv-lite@~0.4.13:
version "0.4.19"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b"
@ -5385,6 +5399,14 @@ react-hot-loader@^3.1.3:
redbox-react "^1.3.6"
source-map "^0.6.1"
react-i18next@^7.5.0:
version "7.5.0"
resolved "https://registry.yarnpkg.com/react-i18next/-/react-i18next-7.5.0.tgz#96a92ba7492edb746975eae316534b42423362fe"
dependencies:
hoist-non-react-statics "^2.3.1"
html-parse-stringify2 "2.0.1"
prop-types "^15.6.0"
react-input-autosize@^2.1.2:
version "2.2.1"
resolved "https://registry.yarnpkg.com/react-input-autosize/-/react-input-autosize-2.2.1.tgz#ec428fa15b1592994fb5f9aa15bb1eb6baf420f8"
@ -6869,6 +6891,10 @@ vm-browserify@0.0.4:
dependencies:
indexof "0.0.1"
void-elements@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-2.0.1.tgz#c066afb582bb1cb4128d60ea92392e94d5e9dbec"
w3c-hr-time@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045"