Skip to content

Commit

Permalink
feat: Automatically switch language when first visit (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuHoMan authored Nov 4, 2024
1 parent c4b0364 commit 009262e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
27 changes: 23 additions & 4 deletions packages/components/src/components/Layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { PropsWithChildren, useContext, useEffect } from 'react';
import { FloatButton, Layout as L } from 'antd';
import { MAIN_BG, Size } from '../../constants';
import { Language, MAIN_BG, Size } from '../../constants';
import { Header } from './header';
import { useLocale, useI18n } from '../../utils';
import {
useLocale,
useI18n,
getFirstVisitFromStorage,
setFirstVisitToStorage,
getLanguage,
} from '../../utils';
import { Progress } from './progress';
import { ConfigContext } from '../../config';

Expand All @@ -16,8 +22,21 @@ export const Layout = (props: PropsWithChildren<LayoutProps>): JSX.Element => {
const { children } = props;

useEffect(() => {
if (i18n.language !== locale) {
i18n.changeLanguage(locale);
let currentLocale = locale;
// Check if the user is visiting the site for the first time
const visited = getFirstVisitFromStorage();
if (!visited) {
setFirstVisitToStorage('1');
const targetLang = window.navigator.language.split('-')[0];
const userLang = getLanguage(targetLang);

if (Object.values(Language).includes(userLang)) {
currentLocale = userLang;
}
}

if (i18n.language !== currentLocale) {
i18n.changeLanguage(currentLocale);
}
}, [locale]);

Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/utils/locale.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import cn from 'antd/es/locale/zh_CN';
import en from 'antd/es/locale/en_GB';
import type { Locale } from 'antd/es/locale';
import { Language } from '../constants';

export function getLocale(locale: string): Locale {
const res = locale === 'cn' || locale === 'zh-CN' ? cn : en;
return res;
}

export function getLanguage(locale: string): Language {
const res = locale === 'cn' || locale === 'zh' ? Language.Cn : Language.En;
return res;
}
9 changes: 9 additions & 0 deletions packages/components/src/utils/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum Keys {
Locale = 'LOCALE',
ViewMode = 'VIEWMODE',
APILoaderMode4Dev = 'APILOADERMODE_DEV',
FirstVisit = 'FIRST_VISIT'
}

export function getStorage(key: string): string | null {
Expand Down Expand Up @@ -67,3 +68,11 @@ export function setAPILoaderModeToStorage(mode: APILoaderMode4Dev) {
export function getAPILoaderModeFromStorage() {
return getStorage(Keys.APILoaderMode4Dev) || APILoaderMode4Dev.Default;
}

export function getFirstVisitFromStorage() {
return getStorage(Keys.FirstVisit);
}

export function setFirstVisitToStorage(value: '1') {
setStorage(Keys.FirstVisit, value);
}

0 comments on commit 009262e

Please sign in to comment.