Skip to content

Commit

Permalink
Feat: Make all text into i18next
Browse files Browse the repository at this point in the history
  • Loading branch information
whats2000 committed Jul 3, 2024
1 parent 614d9b3 commit 02fe08d
Show file tree
Hide file tree
Showing 8 changed files with 1,096 additions and 33 deletions.
13 changes: 10 additions & 3 deletions nsysu_selector_helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,28 @@
"@ant-design/icons": "^5.3.7",
"@swc/plugin-styled-components": "^2.0.9",
"antd": "^5.19.0",
"i18next": "^23.11.5",
"i18next-browser-languagedetector": "^8.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-i18next": "^14.1.2",
"styled-components": "^6.1.11"
},
"devDependencies": {
"@types/i18next-browser-languagedetector": "^3.0.0",
"@types/node": "^20.14.9",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/styled-components": "^5.1.34",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-react": "^7.34.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"prettier": "^3.3.2",
"typescript": "^5.2.2",
"vite": "^5.3.1"
Expand Down
4 changes: 2 additions & 2 deletions nsysu_selector_helper/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NSYSUCourseAPI } from '@/api/NSYSUCourseAPI.ts';
import SectionHeader from '#/SectionHeader.tsx';

const App: FC = () => {
const [themeConfig, _setThemeConfig] = useThemeConfig();
const [themeConfig] = useThemeConfig();

const [isLoading, setIsLoading] = useState(true);

Expand All @@ -17,7 +17,7 @@ const App: FC = () => {
history: {},
});
const [selectedSemester, setSelectedSemester] = useState('');
const [courses, setCourses] = useState<Course[]>([]);
const [, setCourses] = useState<Course[]>([]);

useEffect(() => {
NSYSUCourseAPI.getAvailableSemesters().then((availableSemesters) => {
Expand Down
44 changes: 32 additions & 12 deletions nsysu_selector_helper/src/components/SectionHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Select,
SelectProps,
} from 'antd';
import { useTranslation } from 'react-i18next';
import {
BookFilled,
BookOutlined,
Expand All @@ -17,9 +18,10 @@ import {
NotificationOutlined,
MenuOutlined,
} from '@ant-design/icons';
import banner from '@/assets/banner.svg';
import styled from 'styled-components';

import { AcademicYear } from '@/types';
import banner from '@/assets/banner.svg';

const HeaderContainer = styled(Flex)<{
$primaryColor: string;
Expand Down Expand Up @@ -97,22 +99,40 @@ const SectionHeader: FC<HeaderProps> = ({
selectedSemester,
setSelectedSemester,
}) => {
const { t } = useTranslation();

const { token } = theme.useToken();
const primaryColor = token.colorPrimary;
const textColor = '#ffffff';

const [drawerOpen, setDrawerOpen] = useState(false);

const navTabs: MenuProps['items'] = [
{ key: 'all-courses', label: '所有課程', icon: <BookOutlined /> },
{ key: 'semester-compulsory', label: '學期必修', icon: <BookFilled /> },
{
key: 'all-courses',
label: t('all-courses'),
icon: <BookOutlined />,
},
{
key: 'semester-compulsory',
label: t('semester-compulsory'),
icon: <BookFilled />,
},
{
key: 'course-detective',
label: '課程偵探',
label: t('course-detective'),
icon: <FileSearchOutlined />,
},
{ key: 'selected-export', label: '已選匯出', icon: <FileDoneOutlined /> },
{ key: 'announcements', label: '公告', icon: <NotificationOutlined /> },
{
key: 'selected-export',
label: t('selected-export'),
icon: <FileDoneOutlined />,
},
{
key: 'announcements',
label: t('announcements'),
icon: <NotificationOutlined />,
},
];

const semesterCodeMap = {
Expand All @@ -127,11 +147,11 @@ const SectionHeader: FC<HeaderProps> = ({
.sort((a, b) => b.localeCompare(a))
.map((year) => ({
key: year,
label: `${year.slice(0, -1)} ${semesterCodeMap[parseInt(year.slice(-1))]}`,
label: `${year.slice(0, -1)} ${semesterCodeMap[parseInt(year.slice(-1)) as 1 | 2 | 3]}`,
value: year,
}));

const handleMenuClick = (e: any) => {
const handleMenuClick = (e: { key: string }) => {
setSelectedKeys([e.key]);
setDrawerOpen(false);
};
Expand All @@ -148,8 +168,8 @@ const SectionHeader: FC<HeaderProps> = ({
</Flex>
<Flex align={'center'} justify={'center'}>
<StyledSelect
value={selectedSemester === '' ? '載入中...' : selectedSemester}
onChange={(value) => setSelectedSemester(value)}
value={selectedSemester === '' ? t('loading') : selectedSemester}
onChange={(value) => setSelectedSemester(value as string)}
options={semesterOptions}
loading={!semesterOptions.length}
style={{ width: 120 }}
Expand All @@ -171,14 +191,14 @@ const SectionHeader: FC<HeaderProps> = ({
</MobileMenu>
</HeaderContainer>
<Drawer
title='中山大學選課小幫手'
title={t('selector-helper')}
placement='left'
onClose={() => setDrawerOpen(false)}
open={drawerOpen}
>
<Flex vertical={true} gap={20}>
<Select
value={selectedSemester === '' ? '載入中...' : selectedSemester}
value={selectedSemester === '' ? t('loading') : selectedSemester}
onChange={(value) => setSelectedSemester(value)}
options={semesterOptions}
loading={!semesterOptions.length}
Expand Down
37 changes: 37 additions & 0 deletions nsysu_selector_helper/src/i18n/i18nConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

import translationZH_TW from './locales/zh_TW/translation.json';

export const resources = {
zh_TW: {
translation: translationZH_TW,
},
zh: {
translation: translationZH_TW,
},
};

const initI18n = () => {
i18n
.use(initReactI18next)
.use(LanguageDetector)
.init({
resources,
fallbackLng: 'zh_TW',
debug: false,
detection: {
order: ['querystring', 'localStorage', 'htmlTag'],
lookupQuerystring: 'lang',
lookupLocalStorage: 'i18nextLng',
caches: ['localStorage'],
},
interpolation: {
escapeValue: false,
},
})
.then();
};

export default initI18n;
9 changes: 9 additions & 0 deletions nsysu_selector_helper/src/i18n/locales/zh_TW/translation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"loading": "載入中...",
"selector-helper": "中山大學課程選課小幫手",
"all-courses": "所有課程",
"semester-compulsory": "學期必修",
"course-detective": "課程偵探",
"selected-export": "已選匯出",
"announcements": "公告"
}
15 changes: 9 additions & 6 deletions nsysu_selector_helper/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.tsx';
import './index.css';
import initI18n from '@/i18n/i18nConfig.ts';

initI18n();

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App/>
<App />
</React.StrictMode>,
)
);
3 changes: 2 additions & 1 deletion nsysu_selector_helper/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

"@": ["./src"],
"@/*": ["./src/*"]
}
},
"allowSyntheticDefaultImports": true
},
"include": ["src"]
}
Loading

0 comments on commit 02fe08d

Please sign in to comment.