Skip to content

Commit cffb934

Browse files
committed
refactor: merge translations
1 parent 88a8f1c commit cffb934

26 files changed

Lines changed: 213 additions & 171 deletions

web/src/.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VITE_APP_GRAPHQL_API_ENDPOINT_DEV_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTQxMTY2ODYsIm9yaWdfaWF0IjoxNzU0MDMwMjg2LCJ1c2VyX2lkIjoiMTk1MDY2NDYzMjQ4NjA3MjMyMCJ9.z7FrTMkSDXRN8f6ryFB1HVoCMuhNvyQb4ch6gzxdKa0
2-
VITE_APP_BACKEND_BASE_HOST=https://tainan.anyshake.org
2+
VITE_APP_BACKEND_BASE_HOST=http://127.0.0.1:8073
33

44
VITE_APP_RESTFUL_API_BASE_PATH=/api
55
VITE_APP_WEBSOCKET_API_ENDPOINT=/api/socket

web/src/src/Entry.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ export const Entry = ({ currentLocale, locales, onSwitchLocale }: IEntry) => {
4646
}, [getSoftwareVersionData, getSoftwareVersionLoading]);
4747

4848
const { pathname } = useLocation();
49-
const [currentTitle, setCurrentTitle] = useState(globalConfig.name[currentLocale]);
49+
const [currentTitle, setCurrentTitle] = useState(t(globalConfig.name));
5050
useEffect(() => {
5151
for (const key in routerConfig.routes) {
5252
const { uri } = routerConfig.routes[key];
5353
if (pathname === uri) {
54-
setCurrentTitle(routerConfig.routes[key].title[currentLocale]);
54+
setCurrentTitle(t(routerConfig.routes[key].title));
5555
return;
5656
}
5757
}
58-
setCurrentTitle(routerConfig.routes.default.title[currentLocale]);
59-
}, [pathname, currentLocale]);
58+
setCurrentTitle(t(routerConfig.routes.default.title));
59+
}, [pathname, currentLocale, t]);
6060

6161
const { clearCredential } = useCredentialStore();
6262
const handleLogoutSubmit = () => {
@@ -127,17 +127,13 @@ export const Entry = ({ currentLocale, locales, onSwitchLocale }: IEntry) => {
127127
return (
128128
<div className="animate-fade animate-duration-500 animate-delay-300">
129129
<Header
130-
title={globalConfig.name[currentLocale]}
130+
title={t(globalConfig.name)}
131131
onLogout={handleLogoutSubmit}
132132
currentLocale={currentLocale}
133133
onSwitchLocale={onSwitchLocale}
134134
locales={locales}
135135
/>
136-
<AsideMenu
137-
title={globalConfig.name[currentLocale]}
138-
menu={menuConfig}
139-
currentLocale={currentLocale}
140-
/>
136+
<AsideMenu title={t(globalConfig.name)} menu={menuConfig} />
141137

142138
<div className="ml-10 flex min-h-screen flex-col space-y-4 p-20 px-4">
143139
<BreadCrumb
@@ -146,8 +142,7 @@ export const Entry = ({ currentLocale, locales, onSwitchLocale }: IEntry) => {
146142
title={currentTitle}
147143
/>
148144
<RouterView
149-
routerProps={{ currentLocale }}
150-
currentLocale={currentLocale}
145+
routerProps={{ currentLocale, onSwitchLocale, locales }}
151146
appName={globalConfig.name}
152147
routes={routerConfig.routes}
153148
suspense={<Skeleton />}
@@ -157,8 +152,7 @@ export const Entry = ({ currentLocale, locales, onSwitchLocale }: IEntry) => {
157152
<Footer
158153
copyright={globalConfig.copyright}
159154
repository={globalConfig.repository}
160-
currentLocale={currentLocale}
161-
text={globalConfig.footer}
155+
text={t(globalConfig.footer)}
162156
homepage={globalConfig.homepage}
163157
/>
164158
<Scroller threshold={100} />

web/src/src/Login.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ interface ILogin {
2727
}
2828

2929
export const Login = ({ currentLocale, locales, onSwitchLocale }: ILogin) => {
30+
const { t } = useTranslation();
3031
useEffect(() => {
31-
document.title = globalConfig.name[currentLocale];
32-
}, [currentLocale]);
32+
document.title = t(globalConfig.name);
33+
}, [currentLocale, t]);
3334

3435
// State for pre-authentication data (captcha, encrypt key, etc.)
3536
const [preAuthTTL, setPreAuthTTL] = useState(0);
@@ -39,7 +40,6 @@ export const Login = ({ currentLocale, locales, onSwitchLocale }: ILogin) => {
3940
captcha_img: '',
4041
error: false
4142
});
42-
const { t } = useTranslation();
4343
const getPreAuthData = useCallback(
4444
async (notify: boolean) => {
4545
setPreAuthData({ public_key: '', captcha_id: '', captcha_img: '', error: false });

web/src/src/components/base/AsideMenu.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ import { mdiArrowExpandAll } from '@mdi/js';
22
import Icon from '@mdi/react';
33
import { useState } from 'react';
44
import { Link, useLocation } from 'react-router-dom';
5-
6-
import { localeConfig } from '../../config/locale';
5+
import { useTranslation } from 'react-i18next';
76
import { IMenuItem } from '../../config/menu';
87

98
interface IAsideMenu {
109
readonly title: string;
1110
readonly menu: IMenuItem[];
12-
readonly currentLocale: keyof typeof localeConfig.resources;
1311
}
1412

15-
export const AsideMenu = ({ title, menu, currentLocale }: IAsideMenu) => {
16-
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
13+
export const AsideMenu = ({ title, menu }: IAsideMenu) => {
1714
const { hash, pathname } = useLocation();
15+
const [t] = useTranslation();
16+
const [isSidebarOpen, setIsSidebarOpen] = useState(false);
1817

1918
return (
2019
<aside
@@ -57,7 +56,7 @@ export const AsideMenu = ({ title, menu, currentLocale }: IAsideMenu) => {
5756
key={url}
5857
>
5958
<Icon className="flex-shrink-0" path={icon} size={0.8} />
60-
<span className="ml-4">{label[currentLocale]}</span>
59+
<span className="ml-4">{t(label)}</span>
6160
</Link>
6261
))}
6362
</div>

web/src/src/components/base/Footer.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import { mdiGithub } from '@mdi/js';
22
import Icon from '@mdi/react';
3-
import { useState } from 'react';
3+
import { useMemo } from 'react';
44
import { Link } from 'react-router-dom';
5-
6-
import { localeConfig } from '../../config/locale';
5+
import { useTranslation } from 'react-i18next';
76

87
interface IFooter {
98
readonly copyright: string;
109
readonly homepage: string;
1110
readonly repository: string;
12-
readonly currentLocale: keyof typeof localeConfig.resources;
13-
readonly text: Record<keyof typeof localeConfig.resources, string>;
11+
readonly text: string;
1412
}
1513

16-
export const Footer = ({ text, homepage, currentLocale, copyright, repository }: IFooter) => {
17-
const [currentYear] = useState(new Date().getFullYear());
14+
export const Footer = ({ text, homepage, copyright, repository }: IFooter) => {
15+
const [t] = useTranslation();
16+
const currentYear = useMemo(() => new Date().getFullYear(), []);
1817

1918
return (
2019
<footer className="bg-base-300 flex w-full flex-col justify-between px-8 py-2 text-gray-500 sm:flex-row">
21-
<span className="self-center text-center text-xs italic sm:ml-12">
22-
{text[currentLocale]}
23-
</span>
20+
<span className="self-center text-center text-xs italic sm:ml-12">{t(text)}</span>
2421
<div className="flex items-center justify-center text-center">
2522
<Link
2623
className="self-center text-sm hover:underline"

web/src/src/components/ui/ErrorPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mdiBugPause } from '@mdi/js';
22
import Icon from '@mdi/react';
3-
import { useEffect, useState } from 'react';
3+
import { useEffect, useMemo, useState } from 'react';
44
import { useTranslation } from 'react-i18next';
55

66
import { hideLoaderAnimation } from '../../helpers/app/hideLoaderAnimation';
@@ -20,7 +20,7 @@ interface IErrorPage {
2020
export const ErrorPage = ({ code, heading, content, action, debug }: IErrorPage) => {
2121
const { t } = useTranslation();
2222

23-
const [currentTime] = useState(Date.now());
23+
const currentTime = useMemo(() => Date.now(), []);
2424
const [isDebug, setIsDebug] = useState(false);
2525

2626
useEffect(() => {

web/src/src/components/ui/RouterView.tsx

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
import { ReactNode, Suspense, useEffect } from 'react';
22
import { Route, Routes, useLocation } from 'react-router-dom';
33

4-
import { localeConfig, Translation } from '../../config/locale';
54
import { IRoute, IRouterComponent } from '../../config/router';
5+
import { useTranslation } from 'react-i18next';
66

77
interface IRouterView {
8-
readonly currentLocale: keyof typeof localeConfig.resources;
98
readonly routes: Record<string, IRoute>;
109
readonly routerProps: IRouterComponent;
11-
readonly appName: Translation;
10+
readonly appName: string;
1211
readonly suspense: ReactNode;
1312
}
1413

15-
export const RouterView = ({
16-
currentLocale,
17-
routes,
18-
suspense,
19-
appName,
20-
routerProps
21-
}: IRouterView) => {
14+
export const RouterView = ({ routes, suspense, appName, routerProps }: IRouterView) => {
2215
const { pathname } = useLocation();
16+
const { t } = useTranslation();
2317

2418
// Set the document title based on the current route
2519
useEffect(() => {
2620
const routeTitle = Object.values(routes).find(({ uri }) => pathname === uri)?.title;
27-
const title = routeTitle?.[currentLocale] ?? routes.default.title?.[currentLocale];
28-
document.title = `${title} - ${appName[currentLocale]}`;
29-
}, [routes, appName, pathname, currentLocale]);
21+
const title = routeTitle ?? routes.default.title;
22+
document.title = `${t(title)} - ${t(appName)}`;
23+
}, [routes, appName, pathname, t]);
3024

3125
return (
3226
<Suspense key={pathname} fallback={suspense}>
3327
<Routes>
3428
{Object.values(routes).map(({ uri, element: Element }, index) => (
35-
<Route key={index} element={<Element {...routerProps} />} path={`${uri}`} />
29+
<Route key={index} element={<Element {...routerProps} />} path={uri} />
3630
))}
3731
</Routes>
3832
</Suspense>

web/src/src/config/constraints.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ export const HistoryConstraints = {
4848
}
4949
};
5050

51-
export const DownloadConstraints = {};
51+
export const DownloadConstraints = {
52+
pollInterval: 5000
53+
};
5254

53-
export const SettingsConstraints = {};
55+
export const SettingsConstraints = {
56+
pollInterval: 5000
57+
};

web/src/src/config/global.tsx

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,19 @@
11
import logo from '/anyshake.svg';
22

3-
import { Translation } from './locale';
4-
53
interface IGlobalConfig {
64
readonly repository: string;
75
readonly logo: string;
8-
readonly name: Translation;
6+
readonly name: string;
97
readonly copyright: string;
108
readonly homepage: string;
119
readonly update: number;
12-
readonly footer: Translation;
10+
readonly footer: string;
1311
}
1412

1513
export const globalConfig: IGlobalConfig = {
1614
logo,
17-
name: {
18-
'de-DE': 'AnyShake Observer',
19-
'en-US': 'AnyShake Observer',
20-
'fr-FR': 'AnyShake Observer',
21-
'ja-JP': 'AnyShake Observer',
22-
'zh-TW': 'AnyShake Observer',
23-
'pt-PT': 'AnyShake Observer',
24-
'ru-RU': 'AnyShake Observer',
25-
'id-ID': 'AnyShake Observer',
26-
'tr-TR': 'AnyShake Observer',
27-
'pl-PL': 'AnyShake Observer'
28-
},
29-
footer: {
30-
'de-DE': '"Hoher Geist der Erde."',
31-
'en-US': '"Listen to the whispering earth."',
32-
'fr-FR': '"Ecouter le murmure de la terre."',
33-
'ja-JP': '「地球のささやき」',
34-
'zh-TW': '「聽見地球」',
35-
'pt-PT': '"Ouça a terra sussurrar."',
36-
'ru-RU': '"Прислушайтесь к шепоту Земли."',
37-
'id-ID': '"Dengarkan bisikan bumi."',
38-
'tr-TR': '"Dünyanın fısıltısını dinleyin."',
39-
'pl-PL': '"Wsłuchaj się w szept Ziemi."'
40-
},
15+
name: 'config.global.name',
16+
footer: 'config.global.footer',
4117
update: 10 * 60 * 1000,
4218
copyright: 'SensePlex Limited',
4319
homepage: 'https://anyshake.org',

web/src/src/config/locale.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ export const localeConfig: ILocaleConfig = {
3535
}
3636
};
3737

38-
export type Translation = Record<keyof typeof localeConfig.resources, string>;
39-
4038
const i18n = createI18n(localeConfig.fallback, localeConfig.key, localeConfig.resources);
4139

4240
export default i18n;

0 commit comments

Comments
 (0)