Skip to content

Commit 3bde0d6

Browse files
support i18n cookie
1 parent 1a317d5 commit 3bde0d6

File tree

10 files changed

+40
-21
lines changed

10 files changed

+40
-21
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"@loadable/webpack-plugin": "^5.15.2",
6565
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.4",
6666
"@types/compression": "^1.7.0",
67+
"@types/cookie-parser": "^1.4.2",
6768
"@types/cors": "^2.8.10",
6869
"@types/express": "^4.17.11",
6970
"@types/express-session": "^1.17.3",

src/components/Layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { Outlet } from "react-router";
22

33
import { Header } from "./Header";
44
import { Footer } from "./Footer";
5+
import type { PreLoadComponentType } from "types/components";
56

67
import style from "./index.module.scss";
78

8-
export const Layout = () => {
9+
export const Layout: PreLoadComponentType = () => {
910
return (
1011
<div className={style.container}>
1112
<Header />

src/components/LoadingBar/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useEffect, useState } from "react";
2-
import { useChangeLoadingWithoutRedux, useLoadingBar } from "hooks/useLoadingBar";
2+
import { useLoadingBar, useLoadingBarState } from "hooks/useLoadingBar";
33
import { Bar } from "./LoadingBar";
44
import type { LoadingBarWrapperType } from "types/components";
55

66
export const LoadingBar: LoadingBarWrapperType = () => {
77
const [loading, setLoading] = useState(false);
8-
const loadingState = useChangeLoadingWithoutRedux((s) => s.loading);
8+
const loadingState = useLoadingBarState((s) => s.loading);
99
useEffect(() => {
1010
let id: NodeJS.Timeout | null = null;
1111
if (loadingState) {

src/hooks/useLang.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { apiName } from "config/api";
2-
import shallow from "zustand/shallow";
32
import { useCallback, useRef } from "react";
43
import { useDispatch, useSelector } from "react-redux";
4+
import cookie from "js-cookie";
55
import { useChangeLoadingWithoutRedux } from "./useLoadingBar";
66
import { getDataAction_Server } from "store/reducer/server/share/action";
77
import type { StoreState } from "types/store";
88

99
export const useLang = () => {
1010
const lang = useSelector<StoreState, string>((state) => state.client.currentLang.data);
11-
const { start, end } = useChangeLoadingWithoutRedux(
12-
useCallback((s) => ({ start: s.start, end: s.end }), []),
13-
shallow
14-
);
11+
const { start, end } = useChangeLoadingWithoutRedux();
1512
const langRef = useRef(lang);
1613
const dispatch = useDispatch();
1714
langRef.current = lang;
1815
const changeLang = useCallback(
1916
(newLang: string) => {
2017
if (langRef.current !== newLang) {
2118
Promise.resolve(start())
22-
.then(() => dispatch(getDataAction_Server({ name: apiName.lang, lang: newLang })))
19+
.then(() => {
20+
cookie.set("site_lang", newLang);
21+
return dispatch(getDataAction_Server({ name: apiName.lang, lang: newLang }));
22+
})
2323
.then(end)
2424
.catch(end);
2525
}

src/hooks/useLoadingBar.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useDispatch } from "react-redux";
44
import { actionName } from "config/action";
55
import { setDataSuccess_client } from "store/reducer/client/share/action";
66
import { delay, cancel } from "utils/delay";
7+
import shallow from "zustand/shallow";
78

89
type LoadingBarProps = {
910
height?: number;
@@ -59,7 +60,7 @@ const useLoadingBar = (props: LoadingBarProps = {}) => {
5960
return { ref };
6061
};
6162

62-
const useChangeLoadingWithoutRedux = create<{ loading: boolean; start: () => void; end: () => void }>((set) => ({
63+
const useLoadingBarState = create<{ loading: boolean; start: () => void; end: () => void }>((set) => ({
6364
loading: false,
6465
start: () => set({ loading: true }),
6566
end: () => set({ loading: false }),
@@ -73,4 +74,11 @@ const useChangeLoading = () => {
7374
return { start, end };
7475
};
7576

76-
export { useLoadingBar, useChangeLoading, useChangeLoadingWithoutRedux };
77+
const useChangeLoadingWithoutRedux = () => {
78+
return useLoadingBarState(
79+
useCallback((s) => ({ start: s.start, end: s.end }), []),
80+
shallow
81+
);
82+
};
83+
84+
export { useLoadingBar, useChangeLoading, useLoadingBarState, useChangeLoadingWithoutRedux };

src/hooks/usePreLoad.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { useCallback, useEffect, useRef, useState } from "react";
1+
import { useEffect, useRef, useState } from "react";
22
import { useStore } from "react-redux";
33
import cookie from "js-cookie";
4-
import shallow from "zustand/shallow";
54
import { useLocation, useNavigate } from "react-router";
65
import { log } from "utils/log";
76
import { useChangeLoadingWithoutRedux } from "./useLoadingBar";
@@ -13,10 +12,7 @@ const usePreLoad: UsePreLoadType = ({ routes, preLoad }) => {
1312
const store = useStore();
1413
const location = useLocation();
1514
const navigate = useNavigate();
16-
const { start, end } = useChangeLoadingWithoutRedux(
17-
useCallback((s) => ({ start: s.start, end: s.end }), []),
18-
shallow
19-
);
15+
const { start, end } = useChangeLoadingWithoutRedux();
2016
const firstLoad = useRef(true);
2117
const loadedPath = useRef<string | null>("");
2218
const loadingPath = useRef<string | null>("");

src/server/middleware/renderPage/middleware/initLang.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { determineUserLang } from "utils/i18n";
22
import { Middleware } from "../compose";
33

44
export const initLang: Middleware = (next) => async (args) => {
5-
const { req } = args;
6-
const lang = determineUserLang(req.acceptsLanguages(), req.path);
5+
const { req, res } = args;
6+
const cookieLang = req.cookies?.site_lang;
7+
const lang = cookieLang || determineUserLang(req.acceptsLanguages(), req.path);
8+
9+
res.cookie("site_lang", lang);
710

811
args.lang = lang;
912
args.env && (args.env["LANG"] = lang);

src/server/setup/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import express, { Express } from "express";
2+
import cookieParser from "cookie-parser";
23
import cors from "cors";
34
import session from "express-session";
45

@@ -9,6 +10,8 @@ const setUp = (expressApp: Express) => {
910

1011
expressApp.use(express.static(`${process.cwd()}/dist`));
1112

13+
expressApp.use(cookieParser())
14+
1215
expressApp.use(express.json({ limit: "5mb" }));
1316

1417
expressApp.use(express.urlencoded({ extended: true }));

src/store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type CreateStoreProps = {
1313
};
1414

1515
const devTools =
16-
__CLIENT__ && typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === "function" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ actionsBlacklist: [] });
16+
__CLIENT__ && typeof window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ === "function" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({ actionsDenylist: [] });
1717

1818
const composeEnhancers = devTools || compose;
1919

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,13 @@
21582158
dependencies:
21592159
"@types/node" "*"
21602160

2161+
"@types/cookie-parser@^1.4.2":
2162+
version "1.4.2"
2163+
resolved "https://registry.npmmirror.com/@types/cookie-parser/download/@types/cookie-parser-1.4.2.tgz#e4d5c5ffda82b80672a88a4281aaceefb1bd9df5"
2164+
integrity sha1-5NXF/9qCuAZyqIpCgarO77G9nfU=
2165+
dependencies:
2166+
"@types/express" "*"
2167+
21612168
"@types/cors@^2.8.10":
21622169
version "2.8.12"
21632170
resolved "https://registry.npmmirror.com/@types/cors/download/@types/cors-2.8.12.tgz#6b2c510a7ad7039e98e7b8d3d6598f4359e5c080"
@@ -3624,7 +3631,7 @@ convert-source-map@^1.1.0, convert-source-map@^1.5.0, convert-source-map@^1.7.0:
36243631
cookie-parser@^1.4.6:
36253632
version "1.4.6"
36263633
resolved "https://registry.npmmirror.com/cookie-parser/download/cookie-parser-1.4.6.tgz#3ac3a7d35a7a03bbc7e365073a26074824214594"
3627-
integrity sha1-OsOn01p6A7vH42UHOiYHSCQhRZQ=
3634+
integrity sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==
36283635
dependencies:
36293636
cookie "0.4.1"
36303637
cookie-signature "1.0.6"

0 commit comments

Comments
 (0)