Browse Source

Ajout du switch des langues

Lou 3 years ago
parent
commit
63a6cc6ef6
6 changed files with 74 additions and 65 deletions
  1. 28
    14
      src/App.jsx
  2. 9
    1
      src/components/Navbar/index.jsx
  3. 2
    15
      src/main.jsx
  4. 10
    10
      src/redux/action/index.js
  5. 12
    12
      src/redux/reducer/index.js
  6. 13
    13
      src/redux/store/index.js

+ 28
- 14
src/App.jsx View File

1
-import React from "react";
1
+import React, { useState } from "react";
2
 import {
2
 import {
3
 	BrowserRouter as Router,
3
 	BrowserRouter as Router,
4
 	Route,
4
 	Route,
6
 	Redirect
6
 	Redirect
7
 } from "react-router-dom";
7
 } from "react-router-dom";
8
 import { useSelector } from "react-redux";
8
 import { useSelector } from "react-redux";
9
+import { IntlProvider } from "react-intl";
9
 
10
 
10
 import Navbar from "./components/Navbar/";
11
 import Navbar from "./components/Navbar/";
11
 import Home from "./pages/Home/";
12
 import Home from "./pages/Home/";
13
 import LoginPage from "./pages/Login/";
14
 import LoginPage from "./pages/Login/";
14
 import ProfilePage from "./pages/Profile";
15
 import ProfilePage from "./pages/Profile";
15
 
16
 
17
+import fr_FR from "./translations/fr_FR";
18
+import en_default from "./translations/en.json";
19
+
16
 const App = () => {
20
 const App = () => {
21
+	const [language, setLanguage] = useState(navigator.language);
17
 	const currentUser = useSelector((state) => state.currentUser);
22
 	const currentUser = useSelector((state) => state.currentUser);
18
 
23
 
24
+	let lang = null;
25
+	if (language !== "fr") {
26
+		lang = en_default;
27
+	} else {
28
+		lang = fr_FR;
29
+	}
30
+
19
 	const UnAuthRoute = ({ component: Component, ...rest }) => (
31
 	const UnAuthRoute = ({ component: Component, ...rest }) => (
20
 		<Route
32
 		<Route
21
 			{...rest}
33
 			{...rest}
44
 
56
 
45
 	return (
57
 	return (
46
 		<div className="App">
58
 		<div className="App">
47
-			<Router>
48
-				<Navbar />
49
-				<section className="pages">
50
-					<Switch>
51
-						<Route exact path="/">
52
-							<Home />
53
-						</Route>
54
-						<UnAuthRoute path="/login" component={LoginPage} />
55
-						<UnAuthRoute path="/signup" component={SignupPage} />
56
-						<AuthRoute path="/me" component={ProfilePage} />
57
-					</Switch>
58
-				</section>
59
-			</Router>
59
+			<IntlProvider locale={language} messages={lang}>
60
+				<Router>
61
+					<Navbar setLanguage={setLanguage} />
62
+					<section className="pages">
63
+						<Switch>
64
+							<Route exact path="/">
65
+								<Home />
66
+							</Route>
67
+							<UnAuthRoute path="/login" component={LoginPage} />
68
+							<UnAuthRoute path="/signup" component={SignupPage} />
69
+							<AuthRoute path="/me" component={ProfilePage} />
70
+						</Switch>
71
+					</section>
72
+				</Router>
73
+			</IntlProvider>
60
 		</div>
74
 		</div>
61
 	);
75
 	);
62
 };
76
 };

+ 9
- 1
src/components/Navbar/index.jsx View File

6
 import Star from "../../assets/img/star.ico";
6
 import Star from "../../assets/img/star.ico";
7
 import LogoutButton from "../buttons/LogoutButton";
7
 import LogoutButton from "../buttons/LogoutButton";
8
 
8
 
9
-const Navbar = () => {
9
+const Navbar = ({ setLanguage }) => {
10
 	const currentUser = useSelector((state) => state.currentUser);
10
 	const currentUser = useSelector((state) => state.currentUser);
11
 
11
 
12
 	return (
12
 	return (
24
 				</Link>
24
 				</Link>
25
 			</div>
25
 			</div>
26
 			<div className="navbar__RightSide">
26
 			<div className="navbar__RightSide">
27
+				<div className="localeLinks">
28
+					<button id="setFr" onClick={() => setLanguage("fr")}>
29
+						Fr
30
+					</button>
31
+					<button id="setEn" onClick={() => setLanguage("en")}>
32
+						En
33
+					</button>
34
+				</div>
27
 				{!currentUser && (
35
 				{!currentUser && (
28
 					<>
36
 					<>
29
 						<Link to="/signup">
37
 						<Link to="/signup">

+ 2
- 15
src/main.jsx View File

1
 import React from "react";
1
 import React from "react";
2
 import ReactDOM from "react-dom";
2
 import ReactDOM from "react-dom";
3
-import { IntlProvider } from "react-intl";
3
+// import { IntlProvider } from "react-intl";
4
 import { Provider } from "react-redux";
4
 import { Provider } from "react-redux";
5
 import { PersistGate } from "redux-persist/integration/react";
5
 import { PersistGate } from "redux-persist/integration/react";
6
 import { store, persistor } from "./redux/store/index";
6
 import { store, persistor } from "./redux/store/index";
8
 import App from "./App.jsx";
8
 import App from "./App.jsx";
9
 import "./main.scss";
9
 import "./main.scss";
10
 
10
 
11
-import fr_FR from "./translations/fr_FR.json";
12
-import en_default from "./translations/en.json";
13
-
14
-const locale = navigator.language;
15
-let lang = "";
16
-if (locale !== "fr") {
17
-	lang = en_default;
18
-} else {
19
-	lang = fr_FR;
20
-}
21
-
22
 ReactDOM.render(
11
 ReactDOM.render(
23
 	<React.StrictMode>
12
 	<React.StrictMode>
24
 		<Provider store={store}>
13
 		<Provider store={store}>
25
 			<PersistGate loading={null} persistor={persistor}>
14
 			<PersistGate loading={null} persistor={persistor}>
26
-				<IntlProvider locale={locale} messages={lang}>
27
-					<App />
28
-				</IntlProvider>
15
+				<App />
29
 			</PersistGate>
16
 			</PersistGate>
30
 		</Provider>
17
 		</Provider>
31
 	</React.StrictMode>,
18
 	</React.StrictMode>,

+ 10
- 10
src/redux/action/index.js View File

1
 const setCurrentUser = (currentUser) => {
1
 const setCurrentUser = (currentUser) => {
2
-  return {
3
-    type: "SET_CURRENT_USER",
4
-    payload: currentUser
5
-  }
6
-}
2
+	return {
3
+		type: "SET_CURRENT_USER",
4
+		payload: currentUser
5
+	};
6
+};
7
 
7
 
8
 const removeCurrentUser = () => {
8
 const removeCurrentUser = () => {
9
-  return {
10
-    type: "REMOVE_CURRENT_USER"
11
-  }
12
-}
9
+	return {
10
+		type: "REMOVE_CURRENT_USER"
11
+	};
12
+};
13
 
13
 
14
-export { setCurrentUser, removeCurrentUser }
14
+export { setCurrentUser, removeCurrentUser };

+ 12
- 12
src/redux/reducer/index.js View File

1
-const initialState = { currentUser: null }
1
+const initialState = { currentUser: null };
2
 
2
 
3
-const reducer = ( state = initialState, action ) => {
4
-  switch (action.type){
5
-    case "SET_CURRENT_USER":
6
-      return { currentUser: action.payload };
7
-    case "REMOVE_CURRENT_USER":
8
-      return initialState;
9
-    default:
10
-      return { ...state }
11
-  }
12
-}
3
+const reducer = (state = initialState, action) => {
4
+	switch (action.type) {
5
+		case "SET_CURRENT_USER":
6
+			return { currentUser: action.payload };
7
+		case "REMOVE_CURRENT_USER":
8
+			return initialState;
9
+		default:
10
+			return { ...state };
11
+	}
12
+};
13
 
13
 
14
-export default reducer;
14
+export default reducer;

+ 13
- 13
src/redux/store/index.js View File

1
-import { createStore, compose, applyMiddleware } from 'redux';
2
-import thunk from 'redux-thunk';
3
-import { persistStore, persistReducer } from 'redux-persist'
4
-import storage from 'redux-persist/lib/storage'
5
-import reducer from "../reducer/index"
1
+import { createStore, compose, applyMiddleware } from "redux";
2
+import thunk from "redux-thunk";
3
+import { persistStore, persistReducer } from "redux-persist";
4
+import storage from "redux-persist/lib/storage";
5
+import reducer from "../reducer/index";
6
 
6
 
7
 const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
7
 const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
8
 
8
 
9
 const persistConfig = {
9
 const persistConfig = {
10
-  key: 'root',
11
-  storage,
12
-}
10
+	key: "root",
11
+	storage
12
+};
13
 
13
 
14
-const persistedReducer = persistReducer(persistConfig, reducer)
14
+const persistedReducer = persistReducer(persistConfig, reducer);
15
 
15
 
16
 const store = createStore(
16
 const store = createStore(
17
-  persistedReducer,
18
-  composeEnhancer(applyMiddleware(thunk))
19
-  );
17
+	persistedReducer,
18
+	composeEnhancer(applyMiddleware(thunk))
19
+);
20
 
20
 
21
 const persistor = persistStore(store);
21
 const persistor = persistStore(store);
22
 
22
 
23
-export { store, persistor };
23
+export { store, persistor };

Loading…
Cancel
Save