瀏覽代碼

Ajout du switch des langues

Lou 3 年之前
父節點
當前提交
63a6cc6ef6
共有 6 個檔案被更改,包括 74 行新增65 行删除
  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 查看文件

@@ -1,4 +1,4 @@
1
-import React from "react";
1
+import React, { useState } from "react";
2 2
 import {
3 3
 	BrowserRouter as Router,
4 4
 	Route,
@@ -6,6 +6,7 @@ import {
6 6
 	Redirect
7 7
 } from "react-router-dom";
8 8
 import { useSelector } from "react-redux";
9
+import { IntlProvider } from "react-intl";
9 10
 
10 11
 import Navbar from "./components/Navbar/";
11 12
 import Home from "./pages/Home/";
@@ -13,9 +14,20 @@ import SignupPage from "./pages/Signup";
13 14
 import LoginPage from "./pages/Login/";
14 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 20
 const App = () => {
21
+	const [language, setLanguage] = useState(navigator.language);
17 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 31
 	const UnAuthRoute = ({ component: Component, ...rest }) => (
20 32
 		<Route
21 33
 			{...rest}
@@ -44,19 +56,21 @@ const App = () => {
44 56
 
45 57
 	return (
46 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 74
 		</div>
61 75
 	);
62 76
 };

+ 9
- 1
src/components/Navbar/index.jsx 查看文件

@@ -6,7 +6,7 @@ import "./index.scss";
6 6
 import Star from "../../assets/img/star.ico";
7 7
 import LogoutButton from "../buttons/LogoutButton";
8 8
 
9
-const Navbar = () => {
9
+const Navbar = ({ setLanguage }) => {
10 10
 	const currentUser = useSelector((state) => state.currentUser);
11 11
 
12 12
 	return (
@@ -24,6 +24,14 @@ const Navbar = () => {
24 24
 				</Link>
25 25
 			</div>
26 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 35
 				{!currentUser && (
28 36
 					<>
29 37
 						<Link to="/signup">

+ 2
- 15
src/main.jsx 查看文件

@@ -1,6 +1,6 @@
1 1
 import React from "react";
2 2
 import ReactDOM from "react-dom";
3
-import { IntlProvider } from "react-intl";
3
+// import { IntlProvider } from "react-intl";
4 4
 import { Provider } from "react-redux";
5 5
 import { PersistGate } from "redux-persist/integration/react";
6 6
 import { store, persistor } from "./redux/store/index";
@@ -8,24 +8,11 @@ import { store, persistor } from "./redux/store/index";
8 8
 import App from "./App.jsx";
9 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 11
 ReactDOM.render(
23 12
 	<React.StrictMode>
24 13
 		<Provider store={store}>
25 14
 			<PersistGate loading={null} persistor={persistor}>
26
-				<IntlProvider locale={locale} messages={lang}>
27
-					<App />
28
-				</IntlProvider>
15
+				<App />
29 16
 			</PersistGate>
30 17
 		</Provider>
31 18
 	</React.StrictMode>,

+ 10
- 10
src/redux/action/index.js 查看文件

@@ -1,14 +1,14 @@
1 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 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 查看文件

@@ -1,14 +1,14 @@
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 查看文件

@@ -1,23 +1,23 @@
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 7
 const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
8 8
 
9 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 16
 const store = createStore(
17
-  persistedReducer,
18
-  composeEnhancer(applyMiddleware(thunk))
19
-  );
17
+	persistedReducer,
18
+	composeEnhancer(applyMiddleware(thunk))
19
+);
20 20
 
21 21
 const persistor = persistStore(store);
22 22
 
23
-export { store, persistor };
23
+export { store, persistor };

Loading…
取消
儲存