Skip to content

Commit befb72a

Browse files
committed
made React Router support localization
1 parent 8d9047a commit befb72a

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

web/src/apps/main/entry/app.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import "./style.scss";
22

3+
import { allLanguages, LanguageEntity } from "@dzcode.io/models/dist/language";
34
import { ErrorBoundary } from "@dzcode.io/ui/dist/error-boundary";
45
import Container from "@material-ui/core/Container";
56
import { ComponentType, FC, lazy, Suspense, useEffect } from "react";
6-
import { Route, RouteProps, Switch, useLocation } from "react-router-dom";
7+
import { useDispatch, useSelector } from "react-redux";
8+
import { Route, RouteProps, Switch, useLocation, useRouteMatch } from "react-router-dom";
79
import { Footer } from "src/apps/main/components/footer";
810
import { Navbar } from "src/apps/main/components/navbar";
911
import { Theme } from "src/apps/main/components/theme";
1012
import { getEnv } from "src/common/utils";
13+
import { urlLanguageRegEx } from "src/common/utils/language";
1114
import { Loading } from "src/components/loading";
1215

16+
import { Dispatch, StateInterface } from "../redux";
17+
import { SettingsState } from "../redux/reducers/settings";
18+
1319
interface RouteInterface extends RouteProps {
1420
import: Promise<{ default: ComponentType }>;
1521
}
@@ -50,17 +56,29 @@ const routes: RouteInterface[] = [
5056
];
5157

5258
export const App: FC = () => {
53-
if (getEnv() !== "development") {
54-
const location = useLocation();
59+
const location = useLocation();
60+
const match = useRouteMatch<{ lang?: LanguageEntity["code"] }>(urlLanguageRegEx);
61+
const { language } = useSelector<StateInterface, SettingsState>((state) => state.settings);
62+
const dispatch = useDispatch<Dispatch<SettingsState>>();
5563

56-
useEffect(() => {
64+
useEffect(() => {
65+
if (getEnv() !== "development") {
5766
if (window.ga) {
5867
window.ga("set", "page", location.pathname);
5968
window.ga("send", "pageview");
6069
window.fbq("track", "PageView");
6170
}
62-
}, [location]);
63-
}
71+
}
72+
73+
const urlLanguage =
74+
allLanguages.find(({ code }) => code === match?.params.lang) || allLanguages[0];
75+
if (urlLanguage.code !== language.code) {
76+
dispatch({
77+
type: "UPDATE_SETTINGS",
78+
payload: { language: urlLanguage },
79+
});
80+
}
81+
}, [location]);
6482

6583
return (
6684
<Theme>
@@ -76,8 +94,13 @@ export const App: FC = () => {
7694
<Container maxWidth="lg" style={{ paddingTop: "130px" }}>
7795
<Suspense fallback={<Loading />}>
7896
<Switch>
79-
{routes.map((route, index) => (
80-
<Route {...route} key={`route-${index}`} component={lazy(() => route.import)} />
97+
{routes.map(({ import: im, path, ...route }, index) => (
98+
<Route
99+
{...route}
100+
path={path ? `${urlLanguageRegEx}${path}` : undefined}
101+
key={`route-${index}`}
102+
component={lazy(() => im)}
103+
/>
81104
))}
82105
</Switch>
83106
</Suspense>

web/src/apps/main/entry/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import * as Sentry from "@sentry/browser";
22
import { Integrations } from "@sentry/tracing";
33
import { render } from "react-dom";
44
import { Provider } from "react-redux";
5-
import { BrowserRouter as Router } from "react-router-dom";
5+
import { Router } from "react-router-dom";
66
import { mainStore } from "src/apps/main/redux";
77
import { getEnv } from "src/common/utils";
8+
import { history } from "src/common/utils/history";
89

910
import { App } from "./app";
1011

@@ -23,7 +24,7 @@ if (env !== "development") {
2324

2425
render(
2526
<Provider store={mainStore}>
26-
<Router>
27+
<Router history={history}>
2728
<App />
2829
</Router>
2930
</Provider>,

web/src/apps/main/pages/articles/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export const ArticlesPage: FC = () => {
2424
dispatch(fetchArticlesList());
2525
}, []);
2626

27-
const { path } = useRouteMatch();
27+
const { path: pathRegex } = useRouteMatch();
28+
const path = "/Articles";
2829
const loadedCurrentArticle = isLoaded(currentArticle);
2930

3031
return (
@@ -53,10 +54,13 @@ export const ArticlesPage: FC = () => {
5354
<Grid item xs md={7}>
5455
<Route
5556
exact
56-
path={`${path}`}
57+
path={pathRegex}
5758
render={() => <Landing onShowSidebar={() => setOpen(true)} />}
5859
/>
59-
<Route path={`${path}/:articleSlug`} render={() => <Content key={location.pathname} />} />
60+
<Route
61+
path={`${pathRegex}/:articleSlug`}
62+
render={() => <Content key={location.pathname} />}
63+
/>
6064
</Grid>
6165
</Grid>
6266
</ErrorBoundary>

web/src/apps/main/pages/learn/index.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export const LearnPage: FC = () => {
2525
dispatch(fetchDocumentationList());
2626
}, []);
2727

28-
const { path } = useRouteMatch();
28+
const { path: pathRegex } = useRouteMatch();
29+
const path = "/Learn";
30+
2931
const loadedCurrentDocument = isLoaded(currentDocument);
3032

3133
return (
@@ -54,11 +56,11 @@ export const LearnPage: FC = () => {
5456
<Grid item xs md={7}>
5557
<Route
5658
exact
57-
path={`${path}`}
59+
path={pathRegex}
5860
render={() => <Landing onShowSidebar={() => setOpen(true)} />}
5961
/>
6062
<Route
61-
path={`${path}/:documentSlug`}
63+
path={`${pathRegex}/:documentSlug`}
6264
render={() => <Content key={location.pathname} />}
6365
/>
6466
</Grid>

0 commit comments

Comments
 (0)