Skip to content

Commit 0ec0d5e

Browse files
committed
updated URL when switching languages
1 parent befb72a commit 0ec0d5e

File tree

1 file changed

+24
-0
lines changed
  • web/src/apps/main/redux/reducers/settings

1 file changed

+24
-0
lines changed

web/src/apps/main/redux/reducers/settings/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { allLanguages, LanguageEntity } from "@dzcode.io/models/dist/language";
2+
import { matchPath } from "react-router-dom";
3+
import { history } from "src/common/utils/history";
4+
import { urlLanguageRegEx } from "src/common/utils/language";
25

36
export interface SettingsState {
47
darkMode: boolean;
@@ -29,6 +32,27 @@ export const settings = (
2932
if (action.payload.language) {
3033
localStorage.setItem("languageCode", action.payload.language.code);
3134
document.body.setAttribute("dir", action.payload.language.code === "ar" ? "rtl" : "ltr");
35+
const match = matchPath<{ lang?: LanguageEntity["code"] }>(history.location.pathname, {
36+
path: urlLanguageRegEx,
37+
exact: false,
38+
strict: false,
39+
});
40+
console.log({
41+
payload: action.payload.language.code,
42+
match: match?.params.lang,
43+
});
44+
45+
const langPrefix =
46+
action.payload.language.code === "en" ? "" : `/${action.payload.language.code}`;
47+
48+
if (match?.params.lang || langPrefix) {
49+
history.push({
50+
...history.location,
51+
pathname: match?.params.lang
52+
? history.location.pathname.replace(`/${match.params.lang}`, langPrefix)
53+
: `${langPrefix}${history.location.pathname}`,
54+
});
55+
}
3256
}
3357
return { ...state, ...action.payload };
3458
default:

0 commit comments

Comments
 (0)