Pārlūkot izejas kodu

Changement du fonctionnement de la localisation - le changement de langue est maintenant peristant au rechargement de la page

Lou 3 gadus atpakaļ
vecāks
revīzija
b7638fa6dc

+ 34
- 27
src/App.jsx Parādīt failu

@@ -8,25 +8,28 @@ import {
8 8
 import { useSelector } from "react-redux";
9 9
 import { IntlProvider } from "react-intl";
10 10
 
11
+import { LOCALES } from "./i18n/locales";
12
+import { messages } from "./i18n/messages";
13
+
11 14
 import Navbar from "./components/Navbar/";
12 15
 import Home from "./pages/Home/";
13 16
 import SignupPage from "./pages/Signup";
14 17
 import LoginPage from "./pages/Login/";
15 18
 import ProfilePage from "./pages/Profile";
16 19
 
17
-import fr_FR from "./translations/fr_FR";
18
-import en_default from "./translations/en.json";
19
-
20 20
 const App = () => {
21
-	const [language, setLanguage] = useState(navigator.language);
21
+	const getInitialLocal = () => {
22
+		const savedLocale = localStorage.getItem("locale");
23
+		return savedLocale || LOCALES.FRENCH;
24
+	};
25
+
26
+	const [currentLocale, setCurrentLocale] = useState(getInitialLocal());
22 27
 	const currentUser = useSelector((state) => state.currentUser);
23 28
 
24
-	let lang = null;
25
-	if (language !== "fr") {
26
-		lang = en_default;
27
-	} else {
28
-		lang = fr_FR;
29
-	}
29
+	const handleLocaleChange = (e) => {
30
+		setCurrentLocale(e.target.value);
31
+		localStorage.setItem("locale", e.target.value);
32
+	};
30 33
 
31 34
 	const UnAuthRoute = ({ component: Component, ...rest }) => (
32 35
 		<Route
@@ -55,23 +58,27 @@ const App = () => {
55 58
 	);
56 59
 
57 60
 	return (
58
-		<div className="App">
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>
74
-		</div>
61
+		<IntlProvider
62
+			messages={messages[currentLocale]}
63
+			locale={currentLocale}
64
+			defaultLocale={LOCALES.FRENCH}>
65
+			<Router>
66
+				<Navbar
67
+					currentLocale={currentLocale}
68
+					handleLocaleChange={handleLocaleChange}
69
+				/>
70
+				<section className="app">
71
+					<Switch>
72
+						<Route exact path="/">
73
+							<Home />
74
+						</Route>
75
+						<UnAuthRoute path="/login" component={LoginPage} />
76
+						<UnAuthRoute path="/signup" component={SignupPage} />
77
+						<AuthRoute path="/me" component={ProfilePage} />
78
+					</Switch>
79
+				</section>
80
+			</Router>
81
+		</IntlProvider>
75 82
 	);
76 83
 };
77 84
 

+ 15
- 8
src/components/Navbar/index.jsx Parādīt failu

@@ -5,10 +5,15 @@ import { useSelector } from "react-redux";
5 5
 import "./index.scss";
6 6
 import Star from "../../assets/img/star.ico";
7 7
 import LogoutButton from "../buttons/LogoutButton";
8
+import { LOCALES } from "../../i18n/locales";
8 9
 
9
-const Navbar = ({ setLanguage }) => {
10
+const Navbar = ({ handleLocaleChange, currentLocale }) => {
10 11
 	const currentUser = useSelector((state) => state.currentUser);
11 12
 
13
+	const languages = [
14
+		{ name: "French", code: LOCALES.FRENCH },
15
+		{ name: "English", code: LOCALES.ENGLISH }
16
+	];
12 17
 	return (
13 18
 		<div className="navbar">
14 19
 			<div className="navbar__LeftSide">
@@ -24,13 +29,15 @@ const Navbar = ({ setLanguage }) => {
24 29
 				</Link>
25 30
 			</div>
26 31
 			<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>
32
+				<div className="languageSwitcher">
33
+					Languages{""}
34
+					<select onChange={handleLocaleChange} value={currentLocale}>
35
+						{languages.map(({ name, code }) => (
36
+							<option key={code} value={code}>
37
+								{name}
38
+							</option>
39
+						))}
40
+					</select>
34 41
 				</div>
35 42
 				{!currentUser && (
36 43
 					<>

+ 4
- 0
src/i18n/locales.js Parādīt failu

@@ -0,0 +1,4 @@
1
+export const LOCALES = {
2
+	ENGLISH: "en",
3
+	FRENCH: "fr"
4
+};

+ 15
- 0
src/i18n/messages.js Parādīt failu

@@ -0,0 +1,15 @@
1
+import { LOCALES } from "./locales";
2
+
3
+import { en } from "./translations/en";
4
+import { fr } from "./translations/fr";
5
+
6
+export const messages = {
7
+	[LOCALES.ENGLISH]: {
8
+		home_connected: "User is connected",
9
+		home_notConnected: "User is not connected"
10
+	},
11
+	[LOCALES.FRENCH]: {
12
+		home_connected: "Utilisateurice connecté·e - lol",
13
+		home_notConnected: "Utilisateurice non connecté·e - lol"
14
+	}
15
+};

+ 4
- 0
src/i18n/translations/en.js Parādīt failu

@@ -0,0 +1,4 @@
1
+export const en = {
2
+	home_connected: "User is connected",
3
+	home_notConnected: "User is not connected"
4
+};

+ 4
- 0
src/i18n/translations/fr.js Parādīt failu

@@ -0,0 +1,4 @@
1
+export const fr = {
2
+	home_connected: "Utilisateurice connecté·e - lol",
3
+	home_notConnected: "Utilisateurice non connecté·e - lol"
4
+};

+ 6
- 8
src/pages/Home/index.jsx Parādīt failu

@@ -1,9 +1,7 @@
1 1
 import React, { useEffect } from "react";
2 2
 import { useLocation } from "react-router";
3 3
 import { useSelector } from "react-redux";
4
-import { FormattedMessage } from "react-intl";
5
-
6
-import Cookies from "js-cookie";
4
+import { FormattedMessage, FormattedDate } from "react-intl";
7 5
 
8 6
 const Home = () => {
9 7
 	const currentUser = useSelector((state) => state.currentUser);
@@ -17,27 +15,27 @@ const Home = () => {
17 15
 				<div>
18 16
 					<p>
19 17
 						<FormattedMessage
20
-							id="home.connected"
18
+							id="home_connected"
21 19
 							defaultMessage="Utilisateurice connecté·e - {what}"
22 20
 							description="verification header - connected"
23
-							values={{ what: "react-intl" }}
21
+							values={{ what: "default msg" }}
24 22
 						/>
25 23
 					</p>
26 24
 				</div>
27 25
 			)}
28
-
29 26
 			{!currentUser && (
30 27
 				<div>
31 28
 					<p>
32 29
 						<FormattedMessage
33
-							id="home.notConnected"
30
+							id="home_notConnected"
34 31
 							defaultMessage="Utilisateurice non connecté·e - {what}"
35 32
 							description="verification header - not connected"
36
-							values={{ what: "react-intl" }}
33
+							values={{ what: "default msg" }}
37 34
 						/>
38 35
 					</p>
39 36
 				</div>
40 37
 			)}
38
+			<FormattedDate value={Date.now()} /> <br />
41 39
 			<p>{location.state && location.state.status}</p>
42 40
 		</div>
43 41
 	);

+ 0
- 4
src/translations/en.json Parādīt failu

@@ -1,4 +0,0 @@
1
-{
2
-	"home.connected": "User is connected",
3
-	"home.notConnected": "User is not connected"
4
-}

+ 0
- 4
src/translations/fr_FR.json Parādīt failu

@@ -1,4 +0,0 @@
1
-{
2
-	"home.connected": "Utilisateurice connecté·e - lol",
3
-	"home.notConnected": "Utilisateurice non connecté·e - lol"
4
-}

Notiek ielāde…
Atcelt
Saglabāt